知识总结:Selenium云端测试

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

目前,针对此类需求最经济、最有效的方法便是:实施基于云的自动化浏览器测试。其中,业界最常用的手段便是采用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   # Python example demonstrates Selenium testing on the Cloud using LambdaTest platform   2   import time   3   import unittest   4   import selenium   5   import urllib3   6   import time   7   import warnings   8   from time import sleep   9   from selenium import webdriver   10       11   test_url = "https://www.duckduckgo.com"   12       13   # Desired browser capabilities generated using LambdaTest Capabilities Generator   14   # https://www.lambdatest.com/capabilities-generator/   15       16   desired_capabilities = {   17           "build" : "Selenium testing on the Cloud using LambdaTest",   18           "name" : "Selenium testing on the Cloud using LambdaTest",   19           "platform" : "Windows 10",   20           "browserName" : "Chrome",   21           "version" : "72.0"   22       }   23       24   class SeachTest(unittest.TestCase):   25       def setUp(self):   26           print("Start - SetUp")   27       28           # Ignore ResourceWarning related warnings   29           warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning)   30       31           urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)   32           user_name = "user-name"   33           app_key = "app_key"   34       35           # Username and Access Token to use the LambdaTest platform   36           remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"   37           self.driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=desired_capabilities)   38       39           print("End - SetUp")   40       41       def test_selenium_on_cloud(self):   42           print("Start - test_selenium_on_cloud")   43           self.driver.maximize_window()   44           self.driver.get(test_url)   45       46           # time.sleep(5)   47           sleep(5)   48       49           # Locate the element using the Inspect tool of the browser   50           elem = self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")   51           elem.send_keys("Lambdatest")   52       53           # Execute the search   54           elem.submit()   55       56           # Sleep for 10 seconds in order to see the results   57           sleep(5)   58       59           print("End - test_selenium_on_cloud")   60              61       def tearDown(self):   62           print("Start - tearDown")   63           # Close the browser.   64           self.driver.quit()   65       66           print("End - tearDown")   67       68   if __name__ == '__main__':   69       unittest.main()   

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

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

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

desired_capabilities = {   2           "build" : "Selenium testing on the Cloud",   3           "name" : "Selenium testing on the Cloud using LambdaTest",   4           "platform" : "Windows 10",   5           "browserName" : "Chrome",   6           "version" : "72.0"   7   }   8       9   ………………………………………………………………………   10   ………………………………………………………………………   11   self.driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=desired_capabilities)   

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

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

# Locate the element using the Inspect tool of the browser   2   elem = self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")   3   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浏览器功能代码为:

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

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

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

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

# Python example demonstrates Selenium testing on the Cloud using LambdaTest platform   2   import time   3   import unittest   4   import selenium   5   import urllib3   6   import time   7   import warnings   8   from time import sleep   9   from selenium import webdriver   10       11   test_url = "https://www.duckduckgo.com"   12       13   # Desired browser capabilities generated using LambdaTest Capabilities Generator   14   # https://www.lambdatest.com/capabilities-generator/   15       16   desired_capabilities = {   17       "build" : "Parallel Testing - Selenium testing on the Cloud [IE]",   18       "name" : "Parallel Testing - Selenium testing on the Cloud [ChromeIE]",   19          "platform" : "Windows 7",   20       "browserName" : "Internet Explorer",   21       "version" : "11.0",   22       "ie.compatibility" : 11001   23   }   24       25   class SeachTest(unittest.TestCase):   26       def setUp(self):   27           print("Start - SetUp")   28       29           # Ignore ResourceWarning related warnings   30           warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning)   31       32           urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)   33           # user_name = "user-name"   34           # app_key = "app_key"   35           user_name = "himanshu.sheth@gmail.com"   36           app_key = "fbI6kxucn5iRzwt5GWYiNvaPb4Olu9R8lwBsXWTSaIOebXn4x9"   37       38           # Username and Access Token to use the LambdaTest platform   39           remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"   40           self.driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=desired_capabilities)   41       42           print("End - SetUp")   43       44       def test_selenium_on_cloud(self):   45           print("Start - test_selenium_on_cloud")   46           self.driver.maximize_window()   47           self.driver.get(test_url)   48       49           # time.sleep(5)   50           sleep(5)   51       52           # Locate the element using the Inspect tool of the browser   53           elem = self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")   54           elem.send_keys("Lambdatest")   55       56           # Execute the search   57           elem.submit()   58       59           # Sleep for 10 seconds in order to see the results   60           sleep(5)   61       62           print("End - test_selenium_on_cloud")   63              64       def tearDown(self):   65           print("Start - tearDown")   66           # Close the browser.   67           self.driver.quit()   68       69           print("End - tearDown")   70       71   if __name__ == '__main__':   72       unittest.main()   

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

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

小结

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

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

了解更多
举报
评论 0