您需要知道的有关Selenium云端测试都在这里

译文
开发 前端
本文以实例的形式,介绍了如何使用LambdaTest之类的云平台,在Selenium中进行并行的跨浏览器功能性测试。

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

[[359356]]

目前,针对此类需求最经济、最有效的方法便是:实施基于云的自动化浏览器测试。其中,业界最常用的手段便是采用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测试,我们选择LambdaTest(请参见--https://www.lambdatest.com/)作为基于云的跨浏览器测试平台。您可以在该平台上执行以下任务:

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

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

步骤1:建立帐户

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

步骤2:选择定价方案

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

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

由于要在云端执行测试,因此您需要在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. 1   
  2. # Python example demonstrates Selenium testing on the Cloud using LambdaTest platform   
  3. 2   
  4. import time   
  5. 3   
  6. import unittest   
  7. 4   
  8. import selenium   
  9. 5   
  10. import urllib3   
  11. 6   
  12. import time   
  13. 7   
  14. import warnings   
  15. 8   
  16. from time import sleep   
  17. 9   
  18. from selenium import webdriver   
  19. 10   
  20.     
  21. 11   
  22. test_url = "https://www.duckduckgo.com"   
  23. 12   
  24.     
  25. 13   
  26. # Desired browser capabilities generated using LambdaTest Capabilities Generator   
  27. 14   
  28. # https://www.lambdatest.com/capabilities-generator/   
  29. 15   
  30.     
  31. 16   
  32. desired_capabilities = {   
  33. 17   
  34.         "build" : "Selenium testing on the Cloud using LambdaTest",   
  35. 18   
  36.         "name" : "Selenium testing on the Cloud using LambdaTest",   
  37. 19   
  38.         "platform" : "Windows 10",   
  39. 20   
  40.         "browserName" : "Chrome",   
  41. 21   
  42.         "version" : "72.0"   
  43. 22   
  44.     }   
  45. 23   
  46.     
  47. 24   
  48. class SeachTest(unittest.TestCase):   
  49. 25   
  50.     def setUp(self):   
  51. 26   
  52.         print("Start - SetUp")   
  53. 27   
  54.     
  55. 28   
  56.         # Ignore ResourceWarning related warnings   
  57. 29   
  58.         warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning)   
  59. 30   
  60.     
  61. 31   
  62.         urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)   
  63. 32   
  64.         user_name = "user-name"   
  65. 33   
  66.         app_key = "app_key"   
  67. 34   
  68.     
  69. 35   
  70.         # Username and Access Token to use the LambdaTest platform   
  71. 36   
  72.         remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"   
  73. 37   
  74.         self.driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=desired_capabilities)   
  75. 38   
  76.     
  77. 39   
  78.         print("End - SetUp")   
  79. 40   
  80.     
  81. 41   
  82.     def test_selenium_on_cloud(self):   
  83. 42   
  84.         print("Start - test_selenium_on_cloud")   
  85. 43   
  86.         self.driver.maximize_window()   
  87. 44   
  88.         self.driver.get(test_url)   
  89. 45   
  90.     
  91. 46   
  92.         # time.sleep(5)   
  93. 47   
  94.         sleep(5)   
  95. 48   
  96.     
  97. 49   
  98.         # Locate the element using the Inspect tool of the browser   
  99. 50   
  100.         elem = self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")   
  101. 51   
  102.         elem.send_keys("Lambdatest")   
  103. 52   
  104.     
  105. 53   
  106.         # Execute the search   
  107. 54   
  108.         elem.submit()   
  109. 55   
  110.     
  111. 56   
  112.         # Sleep for 10 seconds in order to see the results   
  113. 57   
  114.         sleep(5)   
  115. 58   
  116.     
  117. 59   
  118.         print("End - test_selenium_on_cloud")   
  119. 60   
  120.            
  121. 61   
  122.     def tearDown(self):   
  123. 62   
  124.         print("Start - tearDown")   
  125. 63   
  126.         # Close the browser.   
  127. 64   
  128.         self.driver.quit()   
  129. 65   
  130.     
  131. 66   
  132.         print("End - tearDown")   
  133. 67   
  134.     
  135. 68   
  136. if __name__ == '__main__':   
  137. 69   
  138.     unittest.main()   

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

  1. user_name = "user-name"   
  2. 2   
  3. app_key = "app_key"   
  4. 3   
  5.     
  6. 4   
  7. # Username and Access Token to use the LambdaTest platform   
  8. 5   
  9. remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"  

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

  1. desired_capabilities = {   
  2. 2   
  3.         "build" : "Selenium testing on the Cloud",   
  4. 3   
  5.         "name" : "Selenium testing on the Cloud using LambdaTest",   
  6. 4   
  7.         "platform" : "Windows 10",   
  8. 5   
  9.         "browserName" : "Chrome",   
  10. 6   
  11.         "version" : "72.0"   
  12. 7   
  13. }   
  14. 8   
  15.     
  16. 9   
  17. ………………………………………………………………………   
  18. 10   
  19. ………………………………………………………………………   
  20. 11   
  21. self.driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=desired_capabilities)   

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

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

  1. # Locate the element using the Inspect tool of the browser   
  2. 2   
  3. elem = self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")   
  4. 3   
  5. elem.send_keys("Lambdatest")   

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

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

并行测试

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

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

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

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

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

  1. desired_capabilities = {   
  2. 2   
  3. "build" : "Parallel Testing - Selenium testing on the Cloud [Chrome]",   
  4. 3   
  5. "name" : "Parallel Testing - Selenium testing on the Cloud [Chrome]",   
  6. 4   
  7. "platform" : "Windows 10",   
  8. 5   
  9. "browserName" : "Chrome",   
  10. 6   
  11. "version" : "72.0"   
  12. 7   
  13. }   

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

  1. desired_capabilities = {   
  2. 2   
  3. "build" : "Parallel Testing - Selenium testing on the Cloud [IE]",   
  4. 3   
  5. "name" : "Parallel Testing - Selenium testing on the Cloud [IE]",   
  6. 4   
  7. "platform" : "Windows 7",   
  8. 5   
  9. "browserName" : "Internet Explorer",   
  10. 6   
  11. "version" : "11.0",   
  12. 7   
  13. "ie.compatibility" : 11001   
  14. 8   
  15. }   

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

  1. # Python example demonstrates Selenium testing on the Cloud using LambdaTest platform   
  2. 2   
  3. import time   
  4. 3   
  5. import unittest   
  6. 4   
  7. import selenium   
  8. 5   
  9. import urllib3   
  10. 6   
  11. import time   
  12. 7   
  13. import warnings   
  14. 8   
  15. from time import sleep   
  16. 9   
  17. from selenium import webdriver   
  18. 10   
  19.     
  20. 11   
  21. test_url = "https://www.duckduckgo.com"   
  22. 12   
  23.     
  24. 13   
  25. # Desired browser capabilities generated using LambdaTest Capabilities Generator   
  26. 14   
  27. # https://www.lambdatest.com/capabilities-generator/   
  28. 15   
  29.     
  30. 16   
  31. desired_capabilities = {   
  32. 17   
  33.     "build" : "Parallel Testing - Selenium testing on the Cloud [IE]",   
  34. 18   
  35.     "name" : "Parallel Testing - Selenium testing on the Cloud [ChromeIE]",   
  36. 19   
  37.        "platform" : "Windows 7",   
  38. 20   
  39.     "browserName" : "Internet Explorer",   
  40. 21   
  41.     "version" : "11.0",   
  42. 22   
  43.     "ie.compatibility" : 11001   
  44. 23   
  45. }   
  46. 24   
  47.     
  48. 25   
  49. class SeachTest(unittest.TestCase):   
  50. 26   
  51.     def setUp(self):   
  52. 27   
  53.         print("Start - SetUp")   
  54. 28   
  55.     
  56. 29   
  57.         # Ignore ResourceWarning related warnings   
  58. 30   
  59.         warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning)   
  60. 31   
  61.     
  62. 32   
  63.         urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)   
  64. 33   
  65.         # user_name = "user-name"   
  66. 34   
  67.         # app_key = "app_key"   
  68. 35   
  69.         user_name = "himanshu.sheth@gmail.com"   
  70. 36   
  71.         app_key = "fbI6kxucn5iRzwt5GWYiNvaPb4Olu9R8lwBsXWTSaIOebXn4x9"   
  72. 37   
  73.     
  74. 38   
  75.         # Username and Access Token to use the LambdaTest platform   
  76. 39   
  77.         remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"   
  78. 40   
  79.         self.driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=desired_capabilities)   
  80. 41   
  81.     
  82. 42   
  83.         print("End - SetUp")   
  84. 43   
  85.     
  86. 44   
  87.     def test_selenium_on_cloud(self):   
  88. 45   
  89.         print("Start - test_selenium_on_cloud")   
  90. 46   
  91.         self.driver.maximize_window()   
  92. 47   
  93.         self.driver.get(test_url)   
  94. 48   
  95.     
  96. 49   
  97.         # time.sleep(5)   
  98. 50   
  99.         sleep(5)   
  100. 51   
  101.     
  102. 52   
  103.         # Locate the element using the Inspect tool of the browser   
  104. 53   
  105.         elem = self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")   
  106. 54   
  107.         elem.send_keys("Lambdatest")   
  108. 55   
  109.     
  110. 56   
  111.         # Execute the search   
  112. 57   
  113.         elem.submit()   
  114. 58   
  115.     
  116. 59   
  117.         # Sleep for 10 seconds in order to see the results   
  118. 60   
  119.         sleep(5)   
  120. 61   
  121.     
  122. 62   
  123.         print("End - test_selenium_on_cloud")   
  124. 63   
  125.            
  126. 64   
  127.     def tearDown(self):   
  128. 65   
  129.         print("Start - tearDown")   
  130. 66   
  131.         # Close the browser.   
  132. 67   
  133.         self.driver.quit()   
  134. 68   
  135.     
  136. 69   
  137.         print("End - tearDown")   
  138. 70   
  139.     
  140. 71   
  141. if __name__ == '__main__':   
  142. 72   
  143.     unittest.main()   

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

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

小结

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

原文标题:All You Need to Know for Selenium Testing on the Cloud,作者: Himanshu Sheth

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

 

责任编辑:华轩 来源: 51CTO
相关推荐

2022-09-22 08:00:00

API开发数据

2021-12-09 08:16:40

JVM参数系统

2021-12-27 08:00:00

Kubernetes容器安全

2020-06-02 07:00:00

会话安全黑客攻击

2017-08-29 11:21:03

微软

2021-11-01 08:00:00

Java异常处理开发

2021-07-01 09:00:00

安全数字化转型渗透

2021-05-17 07:04:07

动态代理面试

2020-08-12 09:32:31

小米MIUI

2019-11-04 09:07:48

DevOps互联网IT

2023-09-11 08:51:23

LinkedList双向链表线程

2021-06-17 13:40:47

区块链比特币公有链

2020-03-18 18:20:19

区块链数字货币比特币

2021-02-01 08:39:26

JTAG接口Jlink

2019-04-24 08:31:43

分布式限流kafka

2019-01-24 08:19:17

云服务多云云计算

2016-03-01 16:14:32

问问应用商店Android Wea

2018-11-28 10:39:01

5G网络运营商

2021-09-01 09:00:00

开发框架React 18

2020-12-08 11:08:55

时间复杂度软件
点赞
收藏

51CTO技术栈公众号