>本文最初发表在lambdatest上。感谢您支持使SitietPoint成为可能的合作伙伴。 硒是一种流行的自动化测试框架,主要用于跨浏览器测试。它是开源的,非常适合对Firefox,Chrome,Internet Explorer和Microsoft Edge等不同浏览器进行自动测试。 Selenium已成为著名的框架,并为其他测试框架(例如HP QTP(快速测试专业))和AKA HP UFT(统一功能测试)提供了激烈的竞争。该Selenium Webdriver的本教程将帮助您对Selenium Suite,Selenium Webdriver Architecture的组件的基本了解,并将向您展示如何运行自动化以测试使用Selenium Web Driver使用Google Chrome,Mozilla Firefox和Mozilla Firefox和Mozilla Firefox和Mozilla Firefox和Mozilla Firefox和Mozilla firefox,Mozilla Firefox和Mozilla Firefox和Mozilla firoge compatibility测试网站Internet Explorer。
硒是不同工具的组合,每个工具在自动化网络测试中起着至关重要的作用。让我们深入研究Webdriver教程。
>钥匙要点
>硒网格
> Selenium IDE是一个简单的Firefox插件,用于录制和播放脚本。但是,Selenium IDE只能用于编写自动化脚本以自动化简单用例的测试。硒IDE不支持有条件的语句,异常处理,循环,屏幕截图捕获等。对于自动化复杂用例,大多数开发人员和测试人员都喜欢选择脚本测试而不是记录和重播测试。硒测试套件由硒遥控器(RC)或Selenium webdriver组成。
> Selenium IDE可用于不同操作系统,即Windows,Linux,Mac OS等。可以在此处下载Firefox的Selenium IDE。
>硒遥控器(RC)硒服务器是Selenium RC的核心组件。 Selenium RC的一些核心功能/职责如下:
>包含核心硒框架,它主要负责在浏览器中注入相同
> Selenium Webdriver直接与浏览器通信,因此它不需要任何单独的组件,例如Selenium Server。它支持对软件测试仪的顶级编程语言,即C#,Ruby,Java,Python,Perl等。执行。您可以从下面提到的链接中下载Selenium webdriver:
> Selenium Webdriver体系结构
可以查看构成Selenium webdriver体系结构的主要块:
<span>from selenium import webdriver </span><span>from selenium.webdriver.common.keys import Keys </span> desired_cap <span>= { </span><span>'platform' : 'win10', </span><span>'browserName' : 'chrome', </span><span>'version' : "67.0", </span><span>} </span> url <span>= "https://username:acsessToken@{LMABDA GRID URL}/wd/hub" </span> driver <span>= webdriver.Remote( </span> desired_capabilities<span>=desired_cap, </span> command_executor<span>= url </span><span>) </span> driver<span>.implicitly_wait(1) </span>driver<span>.get("http://www.google.com/") </span>driver<span>.quit() </span>
JSON WIER协议
> JSON WIER协议
>浏览器驱动程序
>由于浏览器驱动程序可用于Chrome,Firefox,Internet Explorer,Safari和Microsoft Edge等流行浏览器,因此您可以使用其中任何一个用于执行交叉浏览器测试。应该注意的是,您无法在浏览器无法公开的浏览器上对网站进行交叉浏览器测试。
>
> Selenium webdriver In Action
>
> firefox webdriver示例<span>from selenium import webdriver </span><span>from selenium.webdriver.common.keys import Keys </span> desired_cap <span>= { </span><span>'platform' : 'win10', </span><span>'browserName' : 'chrome', </span><span>'version' : "67.0", </span><span>} </span> url <span>= "https://username:acsessToken@{LMABDA GRID URL}/wd/hub" </span> driver <span>= webdriver.Remote( </span> desired_capabilities<span>=desired_cap, </span> command_executor<span>= url </span><span>) </span> driver<span>.implicitly_wait(1) </span>driver<span>.get("http://www.google.com/") </span>driver<span>.quit() </span>
>让我们浏览代码。在我们执行任何操作之前,所有必要的模块是使用“import
>一旦浏览器上的所有操作都完成,ff_driver.close()将被调用以进行必要的清理并释放资源。
> 为了执行代码,您可以在Eclipse IDE中调用Ctrl F9或使用Python命令行选项来调整CTRL F9:>
> chrome webdriver示例
>我们的Selenium Webdriver教程中的上一节,我们使用了Firefox Webdriver。在本节中,我们会查看如果您使用Chrome浏览器进行测试,则需要的更改。 You should ensure that the standalone ChromeDriver binary (which is different than the Chrome browser binary) is available in the right path or it is present as a part of the webdriver.chrome.driver environment variable.如果您使用的是Windows,则需要首先从此处下载Chrome Webdriver。将Chromedriver.exe粘贴到您安装Python的位置(在我们的情况下,这是默认的安装路径,即C:Python27Scripts)。您必须加载相应的Webdriver [第8行],其余代码保持不变。以下是代码段,更改(Chrome Webdriver所需的)以不同的颜色突出显示:
<span>from selenium import webdriver </span><span>from selenium.webdriver.common.keys import Keys </span> desired_cap <span>= { </span><span>'platform' : 'win10', </span><span>'browserName' : 'chrome', </span><span>'version' : "67.0", </span><span>} </span> url <span>= "https://username:acsessToken@{LMABDA GRID URL}/wd/hub" </span> driver <span>= webdriver.Remote( </span> desired_capabilities<span>=desired_cap, </span> command_executor<span>= url </span><span>) </span> driver<span>.implicitly_wait(1) </span>driver<span>.get("http://www.google.com/") </span>driver<span>.quit() </span>
<span># Import the necessary modules for development </span><span>import time </span><span>import unit test </span><span>from selenium import web driver </span> <span># Invoke a new Firefox Instance </span>ff_driver <span>= webdriver.Firefox() </span> <span># Blocking wait of 30 seconds in order to locate the element </span>ff_driver<span>.implicitly_wait(30) </span>ff_driver<span>.maximize_window() </span> <span># Open the required page </span>ff_driver<span>.get("http://www.lambdatest.com") </span> <span># Sleep for 10 seconds in order to see the results </span>time<span>.sleep(10) </span> <span># Close the Browser instance </span>ff_driver<span>.close() </span>
>
<span># Import the necessary modules for development </span><span>import time </span><span>import unit test </span><span>from selenium import web driver </span> <span># Invoke a new Chrome Instance </span>ff_driver <span>= webdriver.Chrome() </span> <span># Blocking wait of 30 seconds in order to locate the element </span>ff_driver<span>.implicitly_wait(30) </span>ff_driver<span>.maximize_window() </span> <span># Open the required page </span>ff_driver<span>.get("http://www.lambdatest.com") </span> <span># Sleep for 10 seconds in order to see the results </span>time<span>.sleep(10) </span> <span># Close the Browser instance </span>ff_driver<span>.close() </span>
>遇到搜索条件后,执行(CTRL单击)操作,从而在“新标签”中打开该页面[第26〜30行]。 ActionChains模块用于执行该操作。输出在下面:
>我们已经使用了动作链,因为它很适合我们上面展示的示例。您可以在此处查看Selenium Webdriver模块下的不同模块(对于Python)。尽管我们使用Python语言进行实施,但您可以使用您选择的编程语言(C#,Java,Perl,Ruby,Php)。
本地webdriver - 不太刻板的方法
如果您计划在有限数量的“浏览器操作系统设备”组合中测试您的网站,则可以使用本地WebDriver进行测试。但是,如果组合的数量更多,则方法可能会步断。您不能为每种组合都有本地设置,因为这不是一种可扩展的方法,也可能是非常昂贵的。>
>使用lambdatest的远程webdriver运行硒脚本> 例如,如果需要在Ubuntu OS Chrome上执行测试,则服务器/集线器将通过注册的服务器注册的客户端/节点的功能进行扫描,并将请求转移到具有匹配功能的客户端。硒架构中只能有一台服务器和数量的客户端。有关远程驱动程序API的更多详细信息可以在lambdatest网站上的其他博客上获得。
>登录到lambdatest后,您应该通过访问lambdatest功能生成器来生成节点所需的功能。选择您喜欢的编程语言(在我们的情况下是Python)和适当的OS/浏览器组合。您可以在设置功能时启用屏幕截图和视频录制功能。如下所示,我们的要求是测试应在Windows 10机器上安装的Firefox(版本64.0)上执行。该要求的功能如下:
>下面的
是Lambdatest功能生成器的屏幕截图:<span>from selenium import webdriver </span><span>from selenium.webdriver.common.keys import Keys </span> desired_cap <span>= { </span><span>'platform' : 'win10', </span><span>'browserName' : 'chrome', </span><span>'version' : "67.0", </span><span>} </span> url <span>= "https://username:acsessToken@{LMABDA GRID URL}/wd/hub" </span> driver <span>= webdriver.Remote( </span> desired_capabilities<span>=desired_cap, </span> command_executor<span>= url </span><span>) </span> driver<span>.implicitly_wait(1) </span>driver<span>.get("http://www.google.com/") </span>driver<span>.quit() </span>
>由于我们将在Lambdatest服务器上使用Selenium Grid,因此我们需要修改代码以访问其云基础架构(也称为远程URL)。远程URL如下所示:
<span>from selenium import webdriver </span><span>from selenium.webdriver.common.keys import Keys </span> desired_cap <span>= { </span><span>'platform' : 'win10', </span><span>'browserName' : 'chrome', </span><span>'version' : "67.0", </span><span>} </span> url <span>= "https://username:acsessToken@{LMABDA GRID URL}/wd/hub" </span> driver <span>= webdriver.Remote( </span> desired_capabilities<span>=desired_cap, </span> command_executor<span>= url </span><span>) </span> driver<span>.implicitly_wait(1) </span>driver<span>.get("http://www.google.com/") </span>driver<span>.quit() </span>
@hub.lambdatest.com/wd/hub是在Lambdatest上执行测试的Lambdatest上的Selenium Grid设置。为了用户授权目的,将User_name(用于Lambdatest上的注册)和App_key(访问 - toke)附加到网格URL。串联字符串形成远程_url。现在,您已经知道了远程WebDriver API和功能生成器,让我们端口以适合这些要求的示例。
>parallel_test_example-1.py
<span># Import the necessary modules for development </span><span>import time </span><span>import unit test </span><span>from selenium import web driver </span> <span># Invoke a new Firefox Instance </span>ff_driver <span>= webdriver.Firefox() </span> <span># Blocking wait of 30 seconds in order to locate the element </span>ff_driver<span>.implicitly_wait(30) </span>ff_driver<span>.maximize_window() </span> <span># Open the required page </span>ff_driver<span>.get("http://www.lambdatest.com") </span> <span># Sleep for 10 seconds in order to see the results </span>time<span>.sleep(10) </span> <span># Close the Browser instance </span>ff_driver<span>.close() </span>
>将用户名和访问权限附加到烤架上。组合形成了执行测试案例执行的远程URL。
>
异常 - 列出在执行测试代码期间遇到的异常。>
>元数据 - 它包含有关执行测试的测试环境的详细信息。在我们的情况下,该测试是在安装在Windows 10机器上的Firefox 64.0上执行的。您甚至可以获取有关浏览器配置的详细信息。
parallel_test_example-2.py
<span>from selenium import webdriver </span><span>from selenium.webdriver.common.keys import Keys </span> desired_cap <span>= { </span><span>'platform' : 'win10', </span><span>'browserName' : 'chrome', </span><span>'version' : "67.0", </span><span>} </span> url <span>= "https://username:acsessToken@{LMABDA GRID URL}/wd/hub" </span> driver <span>= webdriver.Remote( </span> desired_capabilities<span>=desired_cap, </span> command_executor<span>= url </span><span>) </span> driver<span>.implicitly_wait(1) </span>driver<span>.get("http://www.google.com/") </span>driver<span>.quit() </span>
>通过调用Python命令在两个不同的终端上并行执行两个测试。
下面的
>
> selenium webdriver上的常见问题(常见问题解答)用于跨浏览器测试
>
>我如何使用selenium webdriver? 🎜>处理动态的网络元素在Selenium Webdriver中可能具有挑战性。但是,您可以使用各种策略来处理它们。一种常见的方法是使用硒的明确等待功能。这使您的脚本可以暂停一定时间,直到满足特定条件为止,例如DOM中的元素的存在。另一种方法是使用XPATH或使用通配符字符的CSS选择器来匹配元素属性的动态部分。>如何使用Selenium webdriver运行并行测试以更快地执行?运行并行测试在Selenium WebDriver中,您可以使用支持并行执行的测试框架,例如TestNG或Pytest。在testng中,您可以在testng.xml文件中的套件标签中设置并行属性,以将其指定为方法或测试,并将线程计数属性指定为要使用的线程数。在PYTEST中,您可以使用pytest -xdist插件,并使用-n选项指定命令行中的进程数。>如何使用Selenium WebDriver进行测试故障的屏幕截图?
>如何使用Selenium WebDriver?
> Selenium Webdriver提供了与下拉菜单交互的选择类。您可以通过将下拉元素传递给其构造函数来创建选择类的对象。然后,您可以使用selectbyvisibletext(),selectbyvalue()或selectbyIndex()方法选择一个选项。您还可以使用getOptions()方法在下拉列表中获取所有选项。如何使用selenium web driver?>我如何使用selenium webdriver?
selenium webdriver提供方法提供方法与cookie互动。您可以使用AddCookie()方法添加cookie,getCookienamed()方法获取特定cookie,getCookies()获取所有cookie的方法,以及deletecookie(),deletecookienamed(),deletealllookies()或deleteallcookies()方法删除cookie。>如何使用Selenium webdriver来处理Ajax调用?>在Selenium WebDriver中处理Ajax调用可能很棘手,因为它们是异步的,并且可能需要一些时间才能获取数据。您可以使用明确的等待预期的条件等待等待,直到AJAX调用完成并加载数据。处理键盘和鼠标事件的课程。您可以创建一个操作类的对象,并使用其方法,例如click(),doubleclick(),contextClick(),dragandDrop(),keydown(),keyup()等,以模拟各种键盘和鼠标操作。 🎜>以上是如何使用Selenium Webdriver进行交叉浏览器测试的详细内容。更多信息请关注PHP中文网其他相关文章!