Selenium 简明教程

Selenium - Remote Control (RC)

Selenium 是一个用于测试 Web 应用程序的开源和可移植的自动化软件测试工具。它可以在不同的浏览器和操作系统中进行操作。Selenium 不仅是一个工具,而是一套工具,可帮助测试人员更高效地自动化基于 Web 的应用程序。Selenium 通常被称为 Selenium 工具套件。总共四个工具组合在一起称为 Selenium 组件。它们被称为:

Selenium is an open-source and a portable automated software testing tool for testing web applications. It has capabilities to operate across different browsers and operating systems. Selenium is not just a single tool but a set of tools that helps testers to automate web-based applications more efficiently. Selenium is often referred to as a suite of Selenium tools. There are a total of four tools which are together called the Selenium Components. They are known as −

  1. Selenium IDE

  2. Selenium RC

  3. Selenium Webdriver

  4. Selenium Grid

What is Selenium Remote Control?

Selenium Remote Control 是 Selenium 套件的一部分,包括客户端库和一个默认状态下打开和终止浏览器的服务器。它是一个用 Java 实现的服务器。它可以使用 HTTP 接收来自浏览器的命令。在 Selenium 的当前版本中,Selenium Remote Control 已过时,并且已被 Selenium 弃用。

Selenium Remote Control is a part of the Selenium suite and comprises the client libraries, and a server that opens and terminates the browser by default. It is a server implemented in Java. It can accept commands for browsers using the HTTP. In the current version of Selenium, Selenium Remote Control is outdated and it has been deprecated by Selenium.

Selenium Remote Control 中的自动化测试可以用任何编程语言开发,比如 Java、Python、C# 等。Selenium Remote Control 可用于编写自动测试以测试 web 应用程序。Selenium Remote Control 包含一个代理服务器,该服务器可以为浏览器提供权限,使其正常工作,就好像正在测试的应用程序在代理服务器的域中可用一样。要启动测试执行,我们首先必须创建 Selenium Remote Control 服务器的一个实例。

Automation tests in Selenium Remote Control can be developed in any programming languages like Java, Python, C#, and so on. Selenium Remote Control can be used to write automated tests for testing web applications. Selenium Remote Control comprises a proxy server which gives the provision to the browser to work as if that the application under test is available in the domain of the proxy server. To initiate test execution, we have to first create an instance of the Selenium Remote Control server.

Selenium Remote Control (RC) 是在 Selenium WebDriver(Selenium 2.0)出现之前长期存在的 Selenium 主项目。现在,由于 WebDriver 提供了更强大的功能,Selenium Remote Control 几乎不再使用了,但用户仍可继续使用 Selenium Remote Control 开发脚本。

Selenium Remote Control (RC) was the main Selenium project that sustained for a long time before Selenium WebDriver(Selenium 2.0) came into existence. Now. Selenium Remote Control is hardly in use, as WebDriver offers more powerful features, however users can still continue to develop scripts using Selenium Remote Control.

它允许我们借助编程语言(如 Java、C#、Perl、Python 和 PHP)的全部功能编写自动 Web 应用程序 UI 测试,以创建更复杂的测试,比如读取和写入文件、查询数据库和发送电子邮件测试结果。

It allows us to write automated web application UI tests with the help of full power of programming languages such as Java, C#, Perl, Python and PHP to create more complex tests such as reading and writing files, querying a database, and emailing test results.

Architecture of Selenium Remote Control

Selenium Remote Control 的架构在下面的图表中进行了解释 −

The architecture of Selenium Remote Control is described in the below diagram −

selenium remote control

Selenium Remote Control 架构并不直接。为了在 Selenium Remote Control 中触发测试,我们需要安装并在系统中设置 Remote Control Server。它与 Web 浏览器和命令类似,是一个桥梁。Selenium Remote Control 服务器将 Selenium Core(一种 JavaScript 程序)置于浏览器中。此后,Selenium Core 获取 Selenium Remote Control 服务器的消息,形式是 JavaScript 命令中的测试。浏览器执行从 Selenium Core 接收的命令,并将测试响应发回服务器。

Selenium Remote Control architecture is not straightforward. In order to trigger tests in Selenium Remote Control, we would need to install and set up the Remote Control Server in our system. It is similar to a bridge between the web browser and the commands. Selenium Remote Control server puts the Selenium Core(a program in JavaScript) within the browser. Post this, the Selenium Core gets the message from the Selenium Remote Control server as per tests in the form of JavaScript commands. The browsers perform the commands received from the Selenium Core, and send test responses back to the server.

Selenium Server 充当整个应用程序的中央处理器。它接收要执行的测试用例的脚本,解释命令并报告测试执行结果。RC Server 绑定 Selenium Core,并在浏览器中自动注入脚本。所有这些操作仅在测试程序使用客户端库 API 执行浏览器后才执行。服务器通过接收通过 HTTP 网络以 GET 和 POST 类型传递的 Selenese 命令工作,这使我们能够使用任何编程语言将这些命令传递给服务器。

The Selenium Server acts as the central processor for your entire application. It receives the script for the test cases to be executed, interprets the command and reports back the test execution result. RC server binds the Selenium Core and automatically injects the script in the browser. All these actions are performed only after the test program has executed the browser using a client library API. The server works by receiving selenese commands which are transmitted over HTTP network in the action type as GET and POST, this makes us use any programming language to be used to pass these commands to the server.

客户端库是测试人员用于创建测试脚本的特定于编程语言的库。这些库因语言而异。它们提供用于执行 Selenium 命令的函数。每个 Selenese 命令都有已经定义的相应函数。

The client libraries are the programming language-specific libraries which are used by testers to create the test scripts. These libraries are different for each language. They provide functions, which execute the selenium command. Each of the Selenese commands has corresponding functions already defined.

Limitations of Selenium Remote Control

Selenium Remote Control 的局限性在于它的复杂架构。此外,测试执行占用大量时间,而性能没有达到标准。这是因为 Selenium RC 以 JavaScript 命令作为指令发送到浏览器中。基于 Selenium RC 无法实现无头执行,而且其 API 缺乏面向对象性。

The limitation of Selenium Remote Control is because of its complicated architecture. Besides, the test execution takes a lot of time and performance is not up to the mark. This is because the Selenium RC takes JavaScript commands as directions to the browser. The headless execution can not be implemented using the Selenium RC and its APIs are less object-oriented.

Installing Selenium Remote Control with Java

在安装 Selenium Remote Control 之前,需要满足的先决条件 −

Prerequisites before installing the Selenium Remote Control −

Step 1 − 从以下链接下载并安装 Java −

Step 1 − Download and install Java from the below link −

要更详细地了解如何设置 Java,请参阅以下链接 −

To get a more detailed view on how set up Java, refer to the below link −

一旦成功安装 Java,我们可通过从命令提示符运行命令 java 来确认其安装。

Once we have successfully installed Java, we can confirm its installation by running the command: java, from the command prompt.

C:\java

它将在屏幕上显示以下信息 −

It will display the following information on the screen −

Usage: java [options] <mainclass> [args...]
           (to execute a class)
   or  java [options] -jar <jarfile> [args...]
           (to execute a jar file)
   or  java [options] -m <module>[/<mainclass>] [args...]
       java [options] --module <module>[/<mainclass>] [args...]
           (to execute the main class in a module)
   or  java [options] <sourcefile> [args]
           (to execute a single source-file program)

 Arguments following the main class, source file, -jar <jarfile>,
 -m or --module <module>/<mainclass> are passed as the arguments to
 main class.

 where options include:

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    --class-path <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -p <module path>
    --module-path <module path>...
                  A ; separated list of directories, each directory
                  is a directory of modules.
    --upgrade-module-path <module path>...
                  A ; separated list of directories, each directory
                  is a directory of modules that replace upgradeable
                  modules in the runtime image
    --add-modules <module name>[,<module name>...]
                  root modules to resolve in addition to the initial module.
                  <module name> can also be ALL-DEFAULT, ALL-SYSTEM,
                  ALL-MODULE-PATH.
    --enable-native-access <module name>[,<module name>...]
                  modules that are permitted to perform restricted native operations.
                  <module name> can also be ALL-UNNAMED.
    --list-modules
                  list observable modules and exit
    -d <module name>
    --describe-module <module name>
                  describe a module and exit
    --dry-run     create VM and load main class but do not execute main method.
                  The --dry-run option may be useful for validating the
                  command-line options such as the module system configuration.
    --validate-modules
                  validate all modules and exit
                  The --validate-modules option may be useful for finding
                  conflicts and other errors with modules on the module path.
    -D<name>=<value>
                  set a system property
    -verbose:[class|module|gc|jni]
                  enable verbose output for the given subsystem
    -version      print product version to the error stream and exit
    --version     print product version to the output stream and exit
    -showversion  print product version to the error stream and continue
    --show-version
                  print product version to the output stream and continue
    --show-module-resolution
                  show module resolution output during startup
    -? -h -help
                  print this help message to the error stream
    --help        print this help message to the output stream
    -X            print help on extra options to the error stream
    --help-extra  print help on extra options to the output stream
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:jdwp
                  see also -agentlib:jdwp=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
                  HiDPI scaled images are automatically supported and used
                  if available. The unscaled image filename, e.g. image.ext,
                  should always be passed as the argument to the -splash option.
                  The most appropriate scaled image provided will be picked up
                  automatically.
                  See the SplashScreen API documentation for more information
    @argument files
                  one or more argument files containing options
    --disable-@files
                  prevent further argument file expansion
    --enable-preview
                  allow classes to depend on preview features of this release
To specify an argument for a long option, you can use --<name>=<value> or
--<name> <value>.

Step 2 − 接下来,我们要通过运行以下命令来确认已安装的 Java 版本 −

Step 2 − Next, we would confirm the version of the Java installed by running the following command −

java –version

它将显示以下输出 −

It will show the following output −

openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment Homebrew (build 17.0.9+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.9+0, mixed mode, sharing)

执行的命令的输出表示系统中安装的 Java 版本是 17.0.9。

The output of the command executed signified that the java version installed in the system is 17.0.9.

使用 Java 安装 Selenium Remote Control 的步骤如下 −

The installation of Selenium Remote Control with Java is done by following the below steps −

Step 1 − 从以下链接下载 Selenium Java 客户端 −

Step 1 − Download Selenium Java client from the below link −

Step 2 − 提取 selenium-java 的 jar 文件。

Step 2 − Extract the jar file for selenium-java.

Step 3 − 安装像 IntelliJ 和 Eclipse 这样的代码编辑器来编写并运行 Selenium 测试。市面上有多种编辑器,比如:Eclipse、IntelliJ、Atom 等。使用这些编辑器,我们可以开始开展 Java 项目来启动我们的测试自动化。

Step 3 − Install any code editor like IntelliJ, Eclipse, and so on to write and run the Selenium test. There are several editors available in the market for example: Eclipse, IntelliJ, Atom, and so on. Using these editors, we can start working on a Java project to start our test automation.

要详细了解如何设置 IntelliJ,请参见以下链接 −

To get a more detailed view on how set up IntelliJ, refer to the below link −

Step 4 − 在已安装的编辑器内创建一个 Java 项目。

Step 4 − Create a java project within the installed editor.

Step 5 - 将 selenium-java.jar 文件添加到类路径和引用中。

Step 5 − Add the selenium-java.jar files to the class path and references.

Step 6 - 用 Java 创建一个 Selenium 测试。

Step 6 − Create a Selenium test in Java.

Step 7 - 下载并提取 Java jar 文件 - selenium-server- standalone.jar 到某个位置。

Step 7 − Download and extract the Java jar file - selenium-server- standalone.jar in a location.

Step 8 - 导航到提取 Java jar 文件 - selenium-server-standalone.jar 的位置。

Step 8 − Navigate to the location where the Java jar file - selenium-server-standalone.jar is extracted.

Step 9 - 在命令行中运行以下命令:

Step 9 − Run the below command in the command line −

java -jar selenium-server-standalone-<version-number>.jar.

Step 10 - 从编辑器或命令行执行在步骤 6 中开发的 Selenium 测试。

Step 10 − Execute the Selenium test developed in Step6 from the editor or from the command line.

至此,我们关于 Selenium - Remote Control (RC) 的教程全面结束。我们从描述什么是 Selenium Remote Control 开始,Selenium Remote Control 的架构是什么,Selenium Remote Control 的限制是什么以及如何使用 Java 安装 Selenium Remote Control。

This concludes our comprehensive take on the tutorial on Selenium - Remote Control (RC). We’ve started with describing what is Selenium Remote Control, what is the architecture of Selenium Remote Control, what are the limitations of Selenium Remote Control, and how to install Selenium Remote Control with Java.

这为你提供了有关 Selenium - Remote Control (RC) 的深入知识。明智的做法是继续实践你所学到的内容并探索与 Selenium 相关的其他内容,以加深你的理解并扩展你的视野。

This equips you with in-depth knowledge of the Selenium - Remote Control (RC). It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.