Selenium 简明教程

Selenium WebDriver vs RC

Selenium Webdriver 和 Selenium RC 之间存在许多差异。虽然这两种工具都有助于使用各种编程语言创建自动化测试,但它们在许多方面是不同的。

There exist many differences between Selenium Webdriver and Selenium RC. Though both these tools help in creating automation tests using various programming languages, they are different in many ways.

Selenium WebDriver

Selenium WebDriver 是 Selenium Remote Control 的继任者,它直接向浏览器发送命令并检索结果。让我们现在讨论一下 Selenium Webdriver 的架构。

Selenium WebDriver is the successor to Selenium Remote Control which sends commands directly to the browser and retrieves results. Let us now discuss the architecture of Selenium Webdriver.

Selenium WebDriver Architecture

Selenium Web Driver 架构在简化图表中如下所示:

Selenium Web Driver architecture in a simplified diagram is described below −

selenium vs rc 1

从 Selenium 4 版本开始,整个架构完全兼容 W3C - 万维网联盟,这意味着 Selenium 4 遵循 W3C 提供的所有标准和指南。我们可以从以下链接了解有关 W3C 的更多信息:

From the Selenium 4 version, the entire architecture is fully compatible with W3C - World Wide Consortium meaning Selenium 4 follows all the standards and guidelines given by W3C. We can get more information about W3C from the below link −

Selenium Webdriver 架构很简单。它不需要任何代理服务器,并且直接与浏览器通信。只需要一个 IDE 来开发用于测试的代码,以及一个用于执行这些测试的浏览器。

Selenium Webdriver architecture is straightforward. It does not require any proxy server and communicates directly with the browser. Only an IDE is required to develop the code for the tests, and a browser on which those tests would be performed.

Selenium WebDriver API 允许浏览器和浏览器驱动程序之间进行交互。该架构由四层组成,即 Selenium 客户端库、W3C 协议、浏览器驱动程序和浏览器。由于浏览器、浏览器驱动程序和 Selenium webdriver 符合 W3C 协议,因此客户端库和浏览器驱动程序之间的交互更加高效、快速、可靠和稳定。

Selenium WebDriver API enables interaction between browsers and browser drivers. This architecture consists of four layers namely the Selenium Client Library, W3C Protocol, Browser Drivers and Browsers. Since the browsers, browser drivers, and Selenium webdriver are compliant with W3C protocols, hence the interaction between the client libraries and the browser drivers are more efficient, faster, reliable, and stable.

  1. Selenium Client Library consists of languages like Java, Ruby, Python, C# and so on. A test case written in any language is used to send the command to interact with the browsers.

  2. After the code is triggered, it will be converted to Json or other standard similar formats by the client as per the W3C protocol.

  3. W3C protocol is used for the task of transferring information from the server to the client. The browser drivers act as a link between the client and browser. The browser drivers have the serialized request which is actually performed on the browsers. Browser drivers interact with their respective browsers and execute the commands by interpreting Json. As soon as the browser drivers get any instructions, they run them on the browsers. Then the response is given back in the form of HTTP response.

  4. The browser drivers also serialize the response it receives in a standardized format as per W3C protocols and send it back to the client. Then the client would deserialize the responses it received to confirm if there is a successful execution of the command request.

让我们考虑一下下面的代码块:

Let’s consider the below block of code −

WebDriver driver = new ChromeDriver();
driver.get (“https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php“);

一旦我们运行此代码块,整个代码将通过 HTTP 以 URL 的形式按照 W3C 协议转换为 JSON 或任何其他标准格式。转换后的 URL 将被馈送到 ChromeDriver。

Once we run this block of code, the entire code will be converted to JSON or any other standard formats as per W3C protocols over HTTP as a URL. The converted URL will be fed to the ChromeDriver.

浏览器驱动程序利用 HTTP 服务器从 HTTP 获取请求。当浏览器驱动程序获得 URL 时,它通过 HTTP 将请求传递给其浏览器。它将触发在浏览器上执行 Selenium 指令的事件。

The browser driver utilizes HTTP server to get the request from HTTP. As the browser driver gets the URL, it passes the request to its browser via HTTP. It will trigger the event of executing the Selenium instructions on the browser.

现在,如果请求是 POST,它将在浏览器上触发一个操作。如果它是 GET 请求,那么响应将在浏览器端生成。最后,它将通过 HTTP 传递给浏览器驱动程序。然后,浏览器驱动程序将其发送到 UI。

Now if the request is that of POST, it will trigger an action on the browser. If it’s a GET request, then the response will be produced at the browser end. Finally it will be passed over HTTP to the browser driver. The browser driver will in turn send it to the UI.

Selenium Remote Control (RC)

Selenium Remote Control 是用 Java 实现的服务器。Selenium *R*emote *C*ontrol (RC) 是允许进行简单浏览器操作和线性执行的旗舰测试框架。它可以使用 HTTP 接受浏览器的命令。Selenium Remote Control 有一个 Selenium Remote Control 服务器和一个 Selenium Remote Control 客户端。在当前版本的 Selenium 中,Selenium RC 已过时并且已被 Selenium 弃用。

Selenium Remote Control is a server implemented in Java. Selenium *R*emote *C*ontrol (RC) was the flagship testing framework that allowed more than simple browser actions and linear execution. It can accept commands for browsers using the HTTP. Selenium Remote Control has a Selenium Remote Control server and a Selenium Remote Control client. In the current version of Selenium, Selenium RC is outdated and it has been deprecated by Selenium.

Selenium Remote Control Architecture

让我们现在讨论一下 Selenium Remote Control 的架构。Selenium Remote Control 架构在简化图表中如下所示:

Let us now discuss the architecture of Selenium Remote Control. Selenium Remote Control architecture in a simplified diagram is described below −

selenium vs rc 2

Selenium Remote Control 架构并不直接。为了在 Selenium Remote Control 中触发测试,我们需要在系统中安装并设置远程控制服务器。它类似于 Web 浏览器和命令之间的桥梁。Selenium Remote Control 服务器将 Selenium Core(JavaScript 中的一个程序)放入浏览器中。在此之后,Selenium Core 以 JavaScript 命令的形式按照测试从 Selenium Remote Control 服务器获取消息。浏览器执行从 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 Remote Control 构建其上的架构使测试执行速度变慢,因为它基于 JavaScript 语言的 Selenium Core。

Thus the architecture on which Selenium Remote Control is built makes the test execution slow, as it is based on the Selenium Core, which is a JavaScript language.

这总结了 Selenium WebDriver 架构的总体说明。

This sums up the overall explanation of the Selenium WebDriver Architecture.

Difference Between Selenium WebDriver and Remote Control (RC)

以下列出了 Selenium Remote Control 与 Selenium Webdriver 之间的基本差异:

The basic difference between the Selenium Remote Control and Selenium Webdriver are listed below −

Features

Selenium Webdriver

Selenium RC

Architecture

Simple and straightforward

Complex with client and server

Server

No server is required to start test execution

A server is required to start test execution

API

Has stronger APIs.

Has weeker APIs.

Recording

Cannot be used for recording purposes

Can be used for recording purposes.

Headless

Supports headless execution using the HTMLUnit browser

No support for headless execution.

iPhone/Android

Capable of testing mobile devices using the Android Driver and iPhone Driver

Not capable of testing iPhone/Android mobile devices

Report

Not capable of report generation by default

Report generation in html format is available by default

Performance

Fast as it communicates straight with the browser

Not as fast as the Selenium webdriver because it does not communicate straight with the browser.

OOP

Based entirely on OOP

Based not entirely on OOP

Browser Support

Not readily stable while supporting new browsers

Stable while supporting new browsers

User Interaction

Capable of handling cursor and mouse actions

Not capable of handling cursor and mouse actions

Ease

More complicated to understand with respect to developing tests

Less complicated to understand with respect to developing tests