本文共 22054 字,大约阅读时间需要 73 分钟。
Selenium 是用于测试 Web 应用程序用户界面 (UI) 的常用框架。它是一款用于运行端到端功能测试的超强工具。您可以使用多个编程语言编写测试,并且 Selenium 能够在一个或多个浏览器中执行这些测试。
用“一个工具”这样的字眼来形容Selenium并不恰当。Selenium是一套WEB应用的测试框架,为了满足不同的需要,它提供了几个组件形成了所谓的“Selenium”家族。其家族成员后面的文章中有介绍。
Jason Huggins,当时在ThoughtWorks工作,2004年发起了Selenium项目。Jason和他所在的团队采用Javascript编写一种测试工具来验证浏览器页面的行为;这个JavaScript类库就是Selenium core,同时也是seleniumRC、Selenium IDE的核心组件。Selenium由此诞生。
后来,在 Paul Hammant 加入团队后并指导开发第二种操作模式(后来成为“Selenium Remote Control”(RC))。
2007年,Jason Huggins加入Google。与其他人一样,Jennifer Bevan,他继续发展和稳定Selenium RC的功能。与此同时,ThoughtWorks的Simon Stewart开发了一个名为WebDriver的高级浏览器自动化工具。Simon希望能通过浏览器、操作系统的底层方法等一些手段来直接操作浏览器;这样就避免了在Java Script的沙箱环境里存在的那些限制了,Web Driver项目就此诞生!2009年,在Google测试自动化会议上,开发人员举行了一次会议,会议决定合并这两个项目,并称之为新项目Selenium Web Driver,即Selenium2.0。
2008年,Philippe Hanrigou(当时在ThoughtWorks)制作了“Selenium Grid”,它提供了一个中心,允许在任意数量的本地或远程系统上同时运行多个Selenium测试,从而最大限度地减少了测试执行时间。
Selenium 1 和 WebDriver 合并成一款性能更佳的产品 Selenium 2(或 Selenium WebDriver),该款产品发行于 2011 年。
Selenium 2 具有来自 WebDriver 的清晰面向对象 API,并能以最佳的方式与浏览器进行交互。Selenium 2 不使用 JavaScript 沙盒,它支持多种浏览器和多语言绑定。
用下面的图来说明 Selenium 1.x 各部分的功能
Selenium由几个组件组成,每个组件在帮助开发web应用程序测试自动化方面都扮演着特定的角色。
Selenium IDE | 是嵌入到Firefox 浏览器中的一个插件,实现简单的浏览器操作的录制与回放功能。 那么什么情况下用到它呢? 快速的创建 bug 重现脚本,在测试人员的测试过程中,发现了bug 之后可以通过 IDE 将重现的步骤录制下来,以帮助开发人员更容易的重现bug。 IDE 录制的脚本可以可以转换成多种语言,从而帮助我们快速的开发脚本。 |
Selenium Grid | 支持分布式测试,即可以在不同平台、不同浏览器的多台远程机器上同时运行Selenium测试脚本,从而提高测试效率,减少执行时间。 是一种自动化的测试辅助工具,Grid 通过利用现有的计算机基础设施,能加快WebApp的功能测试。利用Grid,可以很方便地同时在多台机器上和异构环境中并行运行多个测试事例。其特点为:
|
Selenium RC (Remote Control) | Selenium RC是一个用JAVA语言编写的服务端,可以处理测试脚本发送过来的HTTP请求,来操作浏览器。 是selenium 家族的核心工具,Selenium RC 支持多种不同的语言编写自动化测试脚本,通过selenium RC 的服务器作为代理服务器去访问应用从而达到测试的目的。 selenium RC 使用分Client Libraries 和Selenium Server。
|
Selenium Core | 是被Selenium Server 嵌入到浏览器页面中的。Selenium Core就是一堆JavaScript函数的集合,就是通过这些JavaScript函数,我们才可以实现用程序对浏览器进行操作。 |
Launcher | 用于启动浏览器,把Selenium Core 加载到浏览器页面当中,并把浏览器的代理设置为Selenium Server 的Http Proxy。 |
Selenium 2.x中包括了WebDriver。其实在Selenium2.0中主推的就是WebDriver这个模块。事实上WebDriver是Selenium RC的替代品,因为Selenium需要保留向下兼容性的原因,Selenium RC才没有被彻底的抛弃,如果使用Selenium开发一个新的自动化测试项目,那么我们强烈推荐使用Selenium2.0的WebDriver进行编码。
Selenium2.0 = Selenium1.0 + WebDriver
Selenium RC 的实质是在浏览器中运行一个JavaScript应用程序,使用每个浏览器内置的JavaScript解释器来解释和执行Selenium的命令集合。
Selenium WebDriver 与上述的Selenium RC是不同的。WebDriver针对不同的浏览器进行开发,取代了RC那样嵌入到被测试的WebApp程序中的JavaScript,使用了与浏览器更加集成的方式进行测试,避免了RC使用JavaScript而可能遇到的安全模型限制导致的问题等。
WebDriver是测试脚本的的核心。在测试脚本中,通过调用WebDriver对象的方法来操作浏览器。
简而言之,Selenium IDE是为了方便录制,Selenium Grid是为了提升执行效率,Selenium RC/WebDriver是脚本编写的核心。
Selenium RC的工作原理是,在测试脚本执行之前,需要启动Selenium服务端,通过注入JavaScript形成沙箱环境,在沙箱环境中完成测试脚本中指定的浏览器操作。
WebDriver是从浏览器外部来控制的,通过调用浏览器原生接口来驱动,完成页面操作。比如说,当我们的脚本操作Firefox浏览器的时候,WebDriver是用JavaScripts来调用API的,而当我们操作IE浏览器的时候,WebDriver就用C++了。
由于有一些页面元素在沙箱和浏览器上的展示有很大出入,因此调用浏览器原生接口或许是控制浏览器的最好方式了。但问题是,如果有新的浏览器问世,WebDriver API就无法支持,而Selenium RC可以。
Selenium RC与WebDriver合并之后,也就是Selenium2.0之后,对于主流的浏览器Chrome、IE、Firefox上的页面操作,可以基于各自的Driver文件,而无须启动服务端。同时,还支持RemoteWebDriver,使用方式与Remote Control一致。
2016年10月 Selenium 3.x 发布了。
现在,可以在Selenium官网下载最新的版本:
Selenium对多浏览器的支持:Chrome、IE、Firefox、Safari、Opera、Edge
Selenium 3 和 Selenium 4的异同
Selenium 3 | Selenium 4 |
The Browser interaction is based on JSON wire protocol due to which it needs encoding and decoding of the API 浏览器交互基于JSON-wire协议,因此需要对API进行编码和解码 | It follows the W3C standard Protocol due to which driver and the browser communication follows the standard procedure, does not require encoding and decoding of the API, so communication happens directly. 它遵循W3C标准协议,驱动程序和浏览器之间的通信遵循标准程序,不需要对API进行编码和解码,因此直接进行通信。 |
#1) Supports removed for some browsers: Now, they have removed the supports for the browsers Opera and Phantom JS. Opera users can use the Chrome browser, whereas Phantom JS users can use Chrome or Firefox in headless mode.
现在,已经删除了对浏览器Opera和Phantom JS的支持。Opera用户可以使用Chrome浏览器,而Phantom JS用户可以使用Chrome或Firefox。
#2) Optimized Selenium Grid: Selenium Grid was developed long back in 2011.
Selenium 4 has come up with new architecture to remove the issues which occurred earlier during installation and configuration, also during the connection between the hub and node machine.
Selenium4提出了新的体系结构,以消除先前在安装和配置期间,以及在Hub和Node机器之间的连接期间出现的问题。
Let’s understand in brief about the Selenium Grid, which contains two major components:
它用于在单个计算机系统上执行测试。网格(Grid)中可以有多个节点。
它是控制网络中所有机器的中心点,它只包含一个集线器(Hub),可以帮助将测试执行分配给不同的节点。
But in Selenium 4, Grid is very flexible. It allows us to test the cases against multiple browsers, browsers of different versions, and also on different Operating systems. Even now, there is no need for a setup to start hub and nodes individually once the user starts the server, the Grid automatically works as both nodes and hub.
但在Selenium4中,Grid 非常灵活。它允许我们在多个浏览器、不同版本的浏览器以及不同的操作系统上测试。即使现在,一旦用户启动服务器也不需要设置单独启动Hub和Node,Grid将自动作为Node和Hub工作。
It also supports advanced tools like Docker, AWS, Azure, and much more, useful in the DevOps process. Now Grid has a more user-friendly UI and contains relevant information related to the session, running, capacity, etc.
它还支持Docker、AWS、Azure等在DevOps过程中非常有用的高级工具。现在Grid有了更友好的用户界面,包含了与会话、运行、容量等相关的信息。
#3) Standardized Documentation: Documents play an essential role for any user, and Selenium documents were not updated since Selenium 2.0.
文档对任何用户都起着至关重要的作用,自Selenium 2.0以来,Selenium文档就没有更新过。
With Selenium 4, they have updated all the official documents related to Selenium that include web driver, Grid, and IDE to explain the latest changes.
Selenium4,已经更新了所有与Selenium相关的官方文档,包括web驱动程序、Grid和IDE,以解释了最新的更改。
#4) Improvement in Selenium 4 IDE: Selenium IDE is a record and playback tool that is used for user interaction with the browser, and the new version of IDE is available with more features
Selenium IDE是一个记录和回放工具,用于用户与浏览器的交互,新版本的IDE提供了更多的功能
These features include:
它改进了浏览器支持。现在有了新版本,任何浏览器供应商都可以轻松地插入最新的seleniumide。
CLI Runner将基于NodeJS,而不是基于HTML的Runner,它支持并行执行,并从中提供报告,其中包含通过/失败的测试用例总数以及所用的执行时间。
#5) Better Monitoring: Logging and request tracing process are now improved to make a better grip on the debugging process for automation engineers as it is not limited to DevOps only.
日志记录和请求跟踪过程现在得到了改进,以更好地控制自动化工程师的调试过程,因为它不仅限于DevOps。
Earlier, users can take a screenshot of the entire page as there was no provision to take the screenshot of the specific web element. But with Selenium 4, users can take the screenshot of a specific web element.
此前,用户可以截取整个页面的截图,因为没有规定截取特定web元素的截图。但是使用selenium4,用户可以截取特定web元素的屏幕截图。
Please find the below code:
|
Now, in Selenium 4, the user can open a URL in a new tab along with the existing tab.
现在,在Selenium4中,用户可以在新选项卡中打开URL以及现有选项卡。
For Example: If the user wants to open 2 URLs in two different tabs at the same time, the user can do that with the Selenium 4.
例如:如果用户希望同时在两个不同的选项卡中打开2个URL,那么用户可以使用Selenium4来完成这一操作。
Please find the below code for reference:
|
Using Selenium 4, the users can also open the new window on the same browser.
使用Selenium4,用户还可以在同一浏览器上打开新窗口。
For Example, if the user wants to access two applications in the same browser, the user can now do this.
例如,如果用户希望在同一浏览器中访问两个应用程序,那么用户现在可以这样做。
Please find the below code for reference:
|
Now with Selenium 4, users can achieve the coordinates, dimension, height, width, etc. as the location of the web elements or object.
现在使用selenium4,用户可以实现坐标、尺寸、高度、宽度等作为web元素或对象的位置。
Please find the below code for your reference:
|
These are also known as Friendly Locators, and this functionality is being added to find out the element which is present nearby to other web element or, we can say that it can find the web elements based on GUI location.
这些也被称为友好的定位器(Locators),并且这个功能被添加来找出存在于其他web元素附近的元素,或者,我们可以说它可以基于GUI位置来找到web元素。
There are five locators added in Selenium 4:
Note: All the above relative locators method support “withTagName” method.
The below example is for the toLeftof() and below() locators:
|
The below example is for the toRightOf() and above() locators:
|
In the new version of Selenium, they have made some internal changes in the API. Earlier in Selenium 3, the Chrome driver extends directly to the Remote Web Driver class. But now in Selenium 4, Chrome driver class extends to Chromium Driver. Chromium Driver class has some predefined methods to access the dev tool.
在Selenium的新版本中,对API进行了一些内部更改。在Selenium 3之前,Chrome driver 直接扩展到 Remote Web Driver类。但现在在Selenium4中,Chrome driver类扩展到了 Chromium Driver。Chromium Driver类有一些预定义的方法来访问开发工具。
Note: Chromium Driver extends the Remote Web driver class.
Selenium是一种广泛用于web应用程序自动化测试的工具。90%的公司将Selenium用于UI自动化。这是一个流行的选择,因为Selenium可以用许多编程语言编写,包括Java、JavaScript、C#和Python。
Selenium is a tool for automating everything you can do inside a web browser. It automates functional aspects of web-based applications and can run across all browsers and platforms.
Selenium是一个工具,它可以使您在web浏览器中所能做的一切自动化。它自动化了基于web的应用程序的功能,并且可以在所有浏览器和平台上运行。
Selenium是一个用于测试web应用程序的可移植框架。Selenium提供了一个用于编写功能测试的回放工具,无需学习测试脚本语言(seleniumide)。它还提供了一种特定于测试域的语言(Selenese),可以用许多流行的编程语言编写测试,包括C#、Groovy、Java、Perl、PHP、Python、Ruby和Scala。这些测试可以在大多数现代网络浏览器上运行。Selenium运行在Windows、Linux和macOS上。它是根据Apache许可证2.0发布的开源软件。
Selenium is not just a single tool or a utility. Rather, it’s a bundle of multiple test tools, referred to as a suite. Each of the tools is designed to meet specific criteria for testing and testing environments.
Selenium 不仅仅是一种工具或实用工具。相反,它是多个测试工具的集合(捆绑包),称为一个套件(Suite)。每种工具都是为了满足测试和测试环境的特定标准而设计的。
它包含下面的一些组成部分:
Selenium is a free open-source platform and supports a wide array of users and support communities. It’s compatible across systems, making it friendly for Windows, Linux, and Mac users. It supports Java, Python, Perl, PHP, C#, and Ruby programming languages.Since Selenium is compatible with cross-browser testing, it works on almost all common browsers including Chrome, Firefox, Edge, Internet Explorer and Safari. Another benefit is that Selenium Grid allows for parallel execution.
Selenium是一个免费的开源平台,支持广泛的用户和支持社区。它跨系统兼容,对Windows、Linux和Mac用户都很友好。它支持Java、Python、Perl、PHP、C#和Ruby编程语言。
由于Selenium与跨浏览器测试兼容,它几乎可以在所有常见浏览器上工作,包括Chrome、Firefox、Edge、ID和Safari。另一个好处是Selenium Grid允许并行执行。
WebDriver is a simple and concise programming interface. As an object-oriented and lightweight API, it drives the browser through interface-type reference variables.
WebDriver是一个简单而简洁的编程接口。作为一个面向对象的轻量级API,它通过接口类型引用变量驱动浏览器。
Selenium 4 is the latest version of Selenium WebDriver, which will be fully standardized with W3C. Since most browsers such as Chrome, Safari, Firefox, Internet Explorer, and Edge follow W3C standardization, browser drivers will interact with Selenium WebDriver in W3C standard protocol.
Selenium 4是Selenium WebDriver的最新版本,它将完全遵守W3C标准化。由于大多数浏览器(如Chrome、Safari、Firefox、Internet Explorer和Edge)都遵循W3C标准化,浏览器驱动程序将以W3C标准协议与Selenium WebDriver交互。
We have several methods of finding a (relative) element close by. These are:
我们有几种方法可以找到附近的(相对)元素。这些是:
There are three types of waits in WebDriver:
WebDriver中有三种类型的等待:
Here is a syntax for using the CSS selector to access the nth attribute: <type>:nth-child(n)
Ex: tr:nth-child(2)
下面是使用CSS选择器访问第n个属性的语法:<type>:第n个子级(n)
WebDriver provides an API to handle alert dialogs. Alerts cannot able to inspect if there is no Alert in the screen, you will get ‘NoAlertPresentException’
WebDriver提供了一个API来处理alert对话框。如果屏幕中没有警告信息是的Alerts无法进行检查时,将得到“NoAlertPresentException”异常信息。
The Alert interface contains a number of APIs to execute different actions, including:
Alert接口包含许多用于执行不同操作的APIs,包括:
Because the reference to an element is now stale the element will no longer appear on the page’s DOM. In other words, the element you located using the findElement method disappeared.
To resolve this, add exception handling to your action. If the exception is stale, locate the element after a short wait of 500 milliseconds and repeat these actions until the action or max iterations have been successful.
A framework is a charter of rules and best practices for the systemic resolution of a problem.
There are different kinds of automation frameworks:
Selenium only supports web applications and does not provide a way to automate Windows-based applications. However, the following approach can help.
Use the Java-based Robot class utility to simulate keyboard and mouse actions. The KeyPress and KeyRelease methods simulate the user pressing and releasing a specific key on the keyboard.
TestNG annotations prioritize the calling of a test method over others. Here are the ones to use with Selenium:
Listeners is an interface that modifies the behavior of the system allowing customization of reports and logs.
There are two primary types of Listeners, WebDriver Listeners, and TestNG Listeners.
Soft Assertion: A Soft Assert will not throw an exception when an assert fails and will continue with the next step.
Hard Assertion: A Hard Assert throws an Assert Exception immediately when an assert statement fails and test suite continues with next @Test.
转载地址:http://stvdi.baihongyu.com/