当前位置: 首页 > 科技观察

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

时间:2023-03-20 13:07:23 科技观察

关于Selenium云测试,你需要知道的都在这里对于质检员来说,他们更关心Web应用程序的整体性能、可用??性和兼容性。毕竟,如果您的Web应用程序产品准备面向大量区域用户甚至全球消费者,那么他们可能会通过各种各样的浏览器、设备、操作系统及其各种组合来访问您提供的网页。服务。目前,解决此类需求最经济有效的方法是实现基于云的自动化浏览器测试。其中,业界最常用的方法是使用Selenium测试。接下来,让我们进一步了解使用Selenium云测试进行跨浏览器性能测试的重要性。本地测试的局限性众所周知,本地托管网站的响应式测试是在线业务成功的关键。为了保证网站内容能够在不同类型的设备和屏幕上正常呈现,测试人员需要提供一台高端电脑,并安装不同的操作系统和浏览器版本(如Chrome、Firefox、Safari等).).显然,这种本地测试的方式不仅费时费力,而且不能保证测试的覆盖率。测试人员迫切需要通过自动化测试来覆盖各种可能的测试用例。Selenium简介及特点作为一个非常流行的开源测试框架,Selenium可以用来创建各种可扩展的测试用例。它不仅可以模拟用户与目标网站的交互,还可以使用Java、Python、PHP、Ruby、C#等语言编写的测试脚本,实现自动化的跨浏览器测试。总结起来,Selenium框架的主要特点包括以下几个方面:Selenium是开源的,可以免费使用。C#、Python、JavaScript和RubyonRails等流行的编程语言可以与Selenium框架无缝结合使用。Selenium拥有一个庞大且持续不断的社区来支持其用户。用户可以下载各种WebDrivers进行自动化GUI测试,以支持流行的Web浏览器,如Chrome、Firefox和MicrosoftEdge。流行的Python友好框架,如Hypothesis、RobotFramework等,可用于通过使用其各种输入值来执行测试。更多关于Selenium的细节在云端使用Selenium测试的优势一般来说,使用Selenium进行跨浏览器测试可以在吞吐量、测试性能、并行度、覆盖率等方面提供优势。具体来说,在云端使用Selenium测试的好处主要体现在以下几个方面:通过基于云端的跨浏览器自动化测试平台,可以获得更广泛的浏览器和平台覆盖。SaaS(软件即服务)模型可以提供一种经济高效且可扩展的模型,该模型为资源使用付费。云服务支持并行测试(参见--https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/),测试执行速度更快。可以在新版本或旧版本的网络浏览器、操作系统和设备上执行功能测试。通过使用错误跟踪、沟通和项目管理工具,跟踪测试的全过程,并实现团队成员之间的及时沟通。可以利用Jenkins(见--https://www.lambdatest.com/blog/what-is-jenkins/)、TeamCity、Travis、Bamboo等CI/CD工具的优势实现构建,封装、测试过程自动化。下表总结了Selenium的功能类别和对应的工具:云端Selenium测试准备为了在云端开始Selenium测试,我们选择LambdaTest(见--https://www.lambdatest.com/)作为云端-基于跨浏览器的测试平台。您可以在此平台上执行以下任务:针对2,000多个真实浏览器和操作系统在线进行基于Selenium的自动化和实时交互式跨浏览器测试。只需最少的代码更改,即可将现有测试移植到LambdaTest平台。利用并发会话和并行执行来更快地获得测试结果。通过查看LambdaTest服务器上测试的调试日志来跟踪自动化测试并分析失败。使用RemoteSeleniumWebDriver为那些强大的编程语言(如C#、Python、RubyonRails等)生成并实现跨浏览器的功能。使用详细的报告机制来分析被测系统的性能。下面是使用LambdaTest在云端测试Selenium的具体步骤:第一步:创建账号首先需要在LambdaTest上完成注册。如上所示,您可以通过访问注册页面——https://accounts.lambdatest.com/register创建一个新帐户。完成后,保存用户名和访问令牌,以便在登录LambdaTest仪表板时使用。第二步:选择定价方案根据测试的要求,您可以选择合适的定价方案。目前,LambdaTest有Lite、Live、Web和移动浏览器自动化,以及Web自动化可供选择。您可以随时升级/降级您选择的计划,但更改只会反映在下一个计费周期。请参阅定价页面--https://www.lambdatest.com/pricing以了解四个计划的详细说明。第三步:浏览器能力生成器(BrowserCapabilitiesGenerator)由于测试会在云端进行,所以需要在SeleniumGrid云端配置各种属性,包括:选择编程语言,指定测试的操作系统,浏览器类型,分辨率和版本号。如上图所示,您可以登录平台,使用函数生成器生成相关函数,以进行各种跨浏览器测试。之后,您需要转到“自动化”选项卡以监控自动化浏览器的测试状态。此外,每个测试都会有一个与之关联的测试ID和构建ID。它的格式是——https://automation.lambdatest.com/logs/?testID=&build=。在云中执行Selenium测试有了前面的基础,让我们看一个Python示例,该示例演示如何在LambdaTest上使用远程Selenium网格在云中执行Selenium测试。串行测试串行测试是以串行方式(即一个接一个)执行测试的实践。它会在上一个测试完成后执行下一个测试。使用串行测试的缺点是它会增加测试执行时间,因为Selenium测试框架和云基础设施的资源没有得到充分利用。对于此类测试,让我们讨论以下测试步骤:在Firefox浏览器中打开DuckDuckGo。找到搜索框。输入查询内容,LambdaTest。执行搜索操作。释放资源。具体实现代码为:Python1#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("启动设置")2728#IgnoreResourceWarningrelatedwarnings29warnings.filterwarnings(action="ignore",message="unclosed",category=ResourceWarning)3031urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)32user_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()以下代码提供:用户名和可以登录平台的accesstoken的组合user_name="user-name"2app_key="app_key"34#UsernameandAccessTokentousetheLambdaTestplatform5remote_url="https://"+user_name+":"+app_key+"@hub.lambdatest.com/wd/hub"我们使用LambdaTestCapabilitiesGenerator生成浏览器和平台capabilities,以及用于实现的编程语言的选择和浏览器规范。在这里,我们的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和代码中都使用XPath元素的属性。#LocatetheelementusingtheInspecttoolofthebrowser2elem=self.driver.find_element_by_xpath("//*[@id='search_form_input_homepage']")3elem.send_keys("Lambdatest")为了演示云Selenium测试,我们使用单元测试框架和Selenium测试套件,从终端调用python命令-“python”来执行。您可以通过导航到相应的测试ID和构建ID来检查自动化测试的状态。并行测试Selenium中的并行测试是指通过同时运行自动化测试套件或用例来减少测试的执行时间。尽管测试套件的组合是在多台服务器上并行执行的,但每台服务器仍然一次只运行一个测试。为了在LambdaTest上快速执行,我们在不同的服务器上同时触发会话。如下图所示,我们调用了两个并发会话。并行执行的测试用例及具体定价方案请参考:https://www.lambdatest.com/pricing。我们在Chrome和IE两种浏览器中设计了并行测试用例,它们遵循上述串行测试步骤。因此,LambdaTest服务器上有2到2个并发会话。在具体实现中,我们使用LambdaTestCapabilitiesGenerator为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}rest实现代码与串口测试相同。下面展示的是针对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="未关闭",category=ResourceWarning)3132urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)33#user_name="用户名"34#app_key="app_key"35user_name="himanshu.sheth@gmail.com"36app_key="fbI6kxucn5iRzwt5GWYiNvaPb4Olu9R8xebXb3"#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文件从LambdaTest的执行快照,我们可以看出可以看出并行测试的并发会话数越大,整体执行时间越短。总结虽然Selenium是一个强大的Web浏览器自动化测试框架,但是如果我们用它来测试本地基础设施,测试设备的性能、可扩展性和吞吐量都会受到限制。因此,企业需要对云中的Web应用程序、网站和移动应用程序进行Selenium测试。并行测试的方式往往可以加快整个执行过程。为了获得更好的扩展性和优越的性能,我们可以使用LambdaTest等云平台在Selenium中进行各种跨浏览器的功能测试。原标题:云上Selenium测试你需要知道的一切,作者:HimanshuSheth