selenium自动化测试框架怎么写(知识总结Selenium云端测试)

在企业构建大规模Web应用时,开发人员往往着眼于如何改进现有功能集,以及提高应用的可扩展性。而对于质量检查人员,他们更关心的是,Web应用的整体性能、可用性、以及使用中的兼容性等方面。毕竟,如果您的Web应用产品是准备面对广大区域性用户、乃至全球的消费者的话,那么他们可能通过五花八门的浏览器、设备、操作系统、及其各种组合,来访问您所提供的服务。

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(1)

目前,针对此类需求最经济、最有效的方法便是:实施基于云的自动化浏览器测试。其中,业界最常用的手段便是采用selenium测试。下面,让我们一起深入了解采用Selenium云端测试对于跨浏览器性能检测的重要性。

本地测试的局限性

众所周知,针对本地托管网站的响应式测试,是成功实现在线业务的关键。为了确保网站的内容能够恰当地呈现在不同种类的设备、及其屏幕上,测试人员需要提供一台配置高端的计算机,并在上面安装不同的操作系统和Web浏览器版本(例如Chrome、Firefox、Safari等)的组合。显然,这种本地测试的方法不但费时费力,而且无法保证测试的覆盖率。测试人员亟待通过自动化测试,来覆盖各种可能性的测试用例。

Selenium简介与特点

作为一种非常流行的开源测试框架,Selenium可以被用来创建各种可扩展的测试用例。它不但能够模拟用户与目标网站之间的交互,而且可以使用由Java、Python、PHP、Ruby、C#等多种语言编写的测试脚本,实现自动化的跨浏览器测试。

归纳起来,Selenium框架的主要特点包括如下方面:

  • Selenium是开源的,可以被免费使用。
  • 诸如C#、Python、JavaScript、以及Ruby on Rails等流行编程语言,可以被无缝地用于Selenium框架。
  • Selenium拥有庞大且持续的社区,能够为用户提供支持。
  • 用户可以针对自动化的GUI测试,下载各种WebDriver,以支持诸如Chrome、Firefox、Microsoft Edge等时下流行的Web浏览器。
  • 诸如Hypothesis、Robot Framework等流行且适用于Python的框架,可以通过使用它的各种输入值,来执行测试。

更多有关Selenium的详细介绍

在云端使用Selenium测试的优点

总的说来,使用Selenium进行跨浏览器测试,可以在吞吐量、测试性能、并行性、覆盖范围等方面提供优势。具体而言,在云端使用Selenium测试的好处主要体现在如下方面:

  • 通过基于云的跨浏览器自动化测试平台,我们能够获得更广阔的浏览器与平台覆盖率。
  • SaaS(软件即服务)模型能够提供按资源使用付费的成本效益与扩展模式。
  • 云服务赋能了并行测试(请参见--https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/),且测试执行的速度更快。
  • 可以在新版、或旧版的Web浏览器、操作系统、以及设备上开展功能测试。
  • 通过利用错误跟踪、沟通和项目管理工具,来跟踪测试的整个进程,并实现团队成员的及时沟通。
  • 可利用Jenkins(请参见--https://www.lambdatest.com/blog/what-is-jenkins/)、TeamCity、Travis、Bamboo等CI/CD工具的优势,来实现构建、打包、以及测试过程的自动化。

下表总结了Selenium的功能类别与对应的工具:

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(2)

云端Selenium测试的准备

为了着手在云端开展Selenium测试,我们选择LambdaTest(请参见--https://www.lambdatest.com/)作为基于云的跨浏览器测试平台。您可以在该平台上执行以下任务:

  • 在线对2000多种真实的浏览器和操作系统,进行基于Selenium的自动化和实时交互式跨浏览器测试。
  • 只需更改最少量的代码,即可将既有的测试移植到LambdaTest平台上。
  • 利用并发会话和并行执行的方式,来更快获取测试结果。
  • 通过查看LambdaTest服务器上针对某项测试的调试日志,来跟踪自动化测试,并分析失败的原因。
  • 使用Remote Selenium WebDriver,为那些强大的编程语言(如C#、Python、Ruby on Rails等),生成并实现跨浏览器的功能。
  • 使用详细的报告机制,来分析待测系统的性能。

下面是使用LambdaTest在云端进行Selenium测试的具体步骤:

步骤1:建立帐户

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(3)

首先,您需要先在LambdaTest上完成注册。如上图所示,您可以通过访问注册页面--https://accounts.lambdatest.com/register,来创建一个新的帐户。完成后,请保存用户名和访问令牌,以便在登录LambdaTest仪表板时使用。

步骤2:选择定价方案

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(4)

根据测试的要求,您可以选择适当的定价方案。目前,LambdaTest有Lite、Live、Web与移动浏览器自动化、以及Web自动化四种方案可供选择。您虽然可以随时对已选定的方案进行升/降级,但是其更改只能反映在下一个结算周期中。有关这四种方案的详细说明,请参见定价页面--https://www.lambdatest.com/pricing。

步骤3:浏览器功能生成器(browser Capabilities Generator)

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(5)

由于要在云端执行测试,因此您需要在Selenium Grid云上配置各种属性,其中包括:选择编程语言,指定用于测试的操作系统、浏览器类型、分辨率、以及版本号。

如上图所示,您可以登录平台,使用功能生成器来生成相关功能,以便执行跨浏览器的各种测试。之后,您需要转到“自动化”选项卡,以监视自动化浏览器的测试状态。同时,每一个测试都将具有与之相关联的测试ID和内部版本ID。其格式为--https://automation.lambdatest.com/logs/?testID=< test-id >&build=< build-id >。

执行云端Selenium测试

有了前面的基础,下面让我们来看一个Python示例,它演示了如何使用LambdaTest上的远程Selenium Grid,在云端进行Selenium测试。

串行测试

串行测试(Serial testing)是一种以串行方式(即,逐个顺次进行)执行测试的实践。它会在上一个测试完成之后,再执行下一个测试。使用串行测试的缺点是:由于没有充分利用Selenium测试框架和云基础架构的资源,因此它增加了测试执行的时间。针对此类测试,我们来讨论如下的测试步骤:

  1. 在Firefox浏览器中打开DuckDuckGo。
  2. 找到搜索框。
  3. 输入查询的内容,即LambdaTest。
  4. 执行搜索操作。
  5. 释放资源。

具体实现代码为:

Python

1#PythonexampledemonstratesSeleniumtestingontheCloudusingLambdaTestplatform2importtime3importunittest4importselenium5importurllib36importtime7importwarnings8fromtimeimportsleep9fromseleniumimportwebdriver1011test_url="https://www.duckduckgo.com"1213#DesiredbrowsercapabilitiesgeneratedusingLambdaTestCapabilitiesGenerator14#https://www.lambdatest.com/capabilities-generator/1516desired_capabilities={17"build":"SeleniumtestingontheCloudusingLambdaTest",18"name":"SeleniumtestingontheCloudusingLambdaTest",19"platform":"Windows10",20"browserName":"Chrome",21"version":"72.0"22}2324classSeachTest(unittest.TestCase):25defsetUp(self):26print("Start-SetUp")2728#IgnoreResourceWarningrelatedwarnings29warnings.filterwarnings(action="ignore",message="unclosed",category=ResourceWarning)3031urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)32user_name="user-name"33app_key="app_key"3435#UsernameandAccessTokentousetheLambdaTestplatform36remote_url="https://" user_name ":" app_key "@hub.lambdatest.com/wd/hub"37self.driver=webdriver.Remote(command_executor=remote_url,desired_capabilities=desired_capabilities)3839print("End-SetUp")4041deftest_selenium_on_cloud(self):42print("Start-test_selenium_on_cloud")43self.driver.maximize_window()44self.driver.get(test_url)4546#time.sleep(5)47sleep(5)4849#LocatetheelementusingtheInspecttoolofthebrowser50elem=self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")51elem.send_keys("Lambdatest")5253#Executethesearch54elem.submit()5556#Sleepfor10secondsinordertoseetheresults57sleep(5)5859print("End-test_selenium_on_cloud")6061deftearDown(self):62print("Start-tearDown")63#Closethebrowser.64self.driver.quit()6566print("End-tearDown")6768if__name__=='__main__':69unittest.main()

而如下代码提供了:可登录平台的用户名和访问令牌的组合。

user_name="user-name"2app_key="app_key"34#UsernameandAccessTokentousetheLambdaTestplatform5remote_url="https://" user_name ":" app_key "@hub.lambdatest.com/wd/hub"

我们使用LambdaTest Capabilities Generator来生成浏览器和平台的功能,同时需要选择用于实现的编程语言,以及浏览器规格。在此,我们的Python代码为:

desired_capabilities={2"build":"SeleniumtestingontheCloud",3"name":"SeleniumtestingontheCloudusingLambdaTest",4"platform":"Windows10",5"browserName":"Chrome",6"version":"72.0"7}89………………………………………………………………………10………………………………………………………………………11self.driver=webdriver.Remote(command_executor=remote_url,desired_capabilities=desired_capabilities)

使用浏览器的检查工具,我们可以找到Web元素的详细信息,在该例子中,即为DuckDuckGo主页上的搜索框。

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(6)

我们在Selenium中、以及代码中都使用到了XPath元素的属性。

#LocatetheelementusingtheInspecttoolofthebrowser2elem=self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")3elem.send_keys("Lambdatest")

为了演示云端Selenium测试,我们使用了unittest框架,以及Selenium测试套件,从终端调用python命令—“python ”来执行。

您可以通过导航到相应的测试ID和内部版本ID,以检查该自动化测试的状态。

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(7)

并行测试

Selenium中的并行测试(Parallel testing)是指:通过并发运行自动化测试套件或用例,来减少测试的执行时间。虽然测试套件的组合是在多台服务器上被并行执行,但是每台服务器一次仍然只运行一个测试。

为了在LambdaTest上快速执行,我们在不同服务器上,以并发的方式来触发会话。如下图所示,我们调用了两个并发的会话。有关并行执行的测试案例、以及具体的定价方案,请参见:https://www.lambdatest.com/pricing。

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(8)

我们设计了在Chrome和IE两种浏览器中开展并行测试的案例,它们遵从上述串行测试的步骤。因此,在LambdaTest服务器上的并发会话为2比2。

在具体实现中,我们使用LambdaTest Capabilities Generator生成了针对Chrome和IE浏览器的两种浏览器功能。

其中,测试1的Chrome浏览器功能代码为:

desired_capabilities={2"build":"ParallelTesting-SeleniumtestingontheCloud[Chrome]",3"name":"ParallelTesting-SeleniumtestingontheCloud[Chrome]",4"platform":"Windows10",5"browserName":"Chrome",6"version":"72.0"7}

测试2的IE浏览器功能代码为:

desired_capabilities={2"build":"ParallelTesting-SeleniumtestingontheCloud[IE]",3"name":"ParallelTesting-SeleniumtestingontheCloud[IE]",4"platform":"Windows7",5"browserName":"InternetExplorer",6"version":"11.0",7"ie.compatibility":110018}

其余的实现代码与串行测试相同。下面展示的是针对IE浏览器的实现代码:

#PythonexampledemonstratesSeleniumtestingontheCloudusingLambdaTestplatform2importtime3importunittest4importselenium5importurllib36importtime7importwarnings8fromtimeimportsleep9fromseleniumimportwebdriver1011test_url="https://www.duckduckgo.com"1213#DesiredbrowsercapabilitiesgeneratedusingLambdaTestCapabilitiesGenerator14#https://www.lambdatest.com/capabilities-generator/1516desired_capabilities={17"build":"ParallelTesting-SeleniumtestingontheCloud[IE]",18"name":"ParallelTesting-SeleniumtestingontheCloud[ChromeIE]",19"platform":"Windows7",20"browserName":"InternetExplorer",21"version":"11.0",22"ie.compatibility":1100123}2425classSeachTest(unittest.TestCase):26defsetUp(self):27print("Start-SetUp")2829#IgnoreResourceWarningrelatedwarnings30warnings.filterwarnings(action="ignore",message="unclosed",category=ResourceWarning)3132urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)33#user_name="user-name"34#app_key="app_key"35user_name="himanshu.sheth@gmail.com"36app_key="fbI6kxucn5iRzwt5GWYiNvaPb4Olu9R8lwBsXWTSaIOebXn4x9"3738#UsernameandAccessTokentousetheLambdaTestplatform39remote_url="https://" user_name ":" app_key "@hub.lambdatest.com/wd/hub"40self.driver=webdriver.Remote(command_executor=remote_url,desired_capabilities=desired_capabilities)4142print("End-SetUp")4344deftest_selenium_on_cloud(self):45print("Start-test_selenium_on_cloud")46self.driver.maximize_window()47self.driver.get(test_url)4849#time.sleep(5)50sleep(5)5152#LocatetheelementusingtheInspecttoolofthebrowser53elem=self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")54elem.send_keys("Lambdatest")5556#Executethesearch57elem.submit()5859#Sleepfor10secondsinordertoseetheresults60sleep(5)6162print("End-test_selenium_on_cloud")6364deftearDown(self):65print("Start-tearDown")66#Closethebrowser.67self.driver.quit()6869print("End-tearDown")7071if__name__=='__main__':72unittest.main()

如下图所示,在并行执行中,针对不同浏览器的两个.py文件,都会在同一处被终端所调用。

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(9)

从LambdaTest的执行快照中,我们可以看出:并行测试的并发会话数越多,总体执行时间就越短。

selenium自动化测试框架怎么写(知识总结Selenium云端测试)(10)

小结

虽然Selenium是用于自动化Web浏览器测试的强大框架,但是如果我们将其用于本地基础架构的测试,那么测试设备的性能、可扩展性、以及吞吐量都会受到限制。因此,企业需要在云端针对Web应用、网站和移动应用进行Selenium测试。而并行测试的方式,往往能够加速整个执行的进程。为了获得更好的可扩展性和更优越的性能,我们可以使用LambdaTest之类的云平台,在Selenium中进行各种跨浏览器的功能性测试。

文章来源:https://developer.51cto.com/art/202012/636453.htm

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页