您需要知道的有關(guān)Selenium云端測(cè)試都在這里
譯文【51CTO.com快譯】在企業(yè)構(gòu)建大規(guī)模Web應(yīng)用時(shí),開發(fā)人員往往著眼于如何改進(jìn)現(xiàn)有功能集,以及提高應(yīng)用的可擴(kuò)展性。而對(duì)于質(zhì)量檢查人員,他們更關(guān)心的是,Web應(yīng)用的整體性能、可用性、以及使用中的兼容性等方面。畢竟,如果您的Web應(yīng)用產(chǎn)品是準(zhǔn)備面對(duì)廣大區(qū)域性用戶、乃至全球的消費(fèi)者的話,那么他們可能通過(guò)五花八門的瀏覽器、設(shè)備、操作系統(tǒng)、及其各種組合,來(lái)訪問您所提供的服務(wù)。
目前,針對(duì)此類需求最經(jīng)濟(jì)、最有效的方法便是:實(shí)施基于云的自動(dòng)化瀏覽器測(cè)試。其中,業(yè)界最常用的手段便是采用Selenium測(cè)試。下面,讓我們一起深入了解采用Selenium云端測(cè)試對(duì)于跨瀏覽器性能檢測(cè)的重要性。
本地測(cè)試的局限性
眾所周知,針對(duì)本地托管網(wǎng)站的響應(yīng)式測(cè)試,是成功實(shí)現(xiàn)在線業(yè)務(wù)的關(guān)鍵。為了確保網(wǎng)站的內(nèi)容能夠恰當(dāng)?shù)爻尸F(xiàn)在不同種類的設(shè)備、及其屏幕上,測(cè)試人員需要提供一臺(tái)配置高端的計(jì)算機(jī),并在上面安裝不同的操作系統(tǒng)和Web瀏覽器版本(例如Chrome、Firefox、Safari等)的組合。顯然,這種本地測(cè)試的方法不但費(fèi)時(shí)費(fèi)力,而且無(wú)法保證測(cè)試的覆蓋率。測(cè)試人員亟待通過(guò)自動(dòng)化測(cè)試,來(lái)覆蓋各種可能性的測(cè)試用例。
Selenium簡(jiǎn)介與特點(diǎn)
作為一種非常流行的開源測(cè)試框架,Selenium可以被用來(lái)創(chuàng)建各種可擴(kuò)展的測(cè)試用例。它不但能夠模擬用戶與目標(biāo)網(wǎng)站之間的交互,而且可以使用由Java、Python、PHP、Ruby、C#等多種語(yǔ)言編寫的測(cè)試腳本,實(shí)現(xiàn)自動(dòng)化的跨瀏覽器測(cè)試。
歸納起來(lái),Selenium框架的主要特點(diǎn)包括如下方面:
- Selenium是開源的,可以被免費(fèi)使用。
- 諸如C#、Python、JavaScript、以及Ruby on Rails等流行編程語(yǔ)言,可以被無(wú)縫地用于Selenium框架。
- Selenium擁有龐大且持續(xù)的社區(qū),能夠?yàn)橛脩籼峁┲С帧?/li>
- 用戶可以針對(duì)自動(dòng)化的GUI測(cè)試,下載各種WebDriver,以支持諸如Chrome、Firefox、Microsoft Edge等時(shí)下流行的Web瀏覽器。
- 諸如Hypothesis、Robot Framework等流行且適用于Python的框架,可以通過(guò)使用它的各種輸入值,來(lái)執(zhí)行測(cè)試。
在云端使用Selenium測(cè)試的優(yōu)點(diǎn)
總的說(shuō)來(lái),使用Selenium進(jìn)行跨瀏覽器測(cè)試,可以在吞吐量、測(cè)試性能、并行性、覆蓋范圍等方面提供優(yōu)勢(shì)。具體而言,在云端使用Selenium測(cè)試的好處主要體現(xiàn)在如下方面:
- 通過(guò)基于云的跨瀏覽器自動(dòng)化測(cè)試平臺(tái),我們能夠獲得更廣闊的瀏覽器與平臺(tái)覆蓋率。
- SaaS(軟件即服務(wù))模型能夠提供按資源使用付費(fèi)的成本效益與擴(kuò)展模式。
- 云服務(wù)賦能了并行測(cè)試(請(qǐng)參見--https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/),且測(cè)試執(zhí)行的速度更快。
- 可以在新版、或舊版的Web瀏覽器、操作系統(tǒng)、以及設(shè)備上開展功能測(cè)試。
- 通過(guò)利用錯(cuò)誤跟蹤、溝通和項(xiàng)目管理工具,來(lái)跟蹤測(cè)試的整個(gè)進(jìn)程,并實(shí)現(xiàn)團(tuán)隊(duì)成員的及時(shí)溝通。
- 可利用Jenkins(請(qǐng)參見--https://www.lambdatest.com/blog/what-is-jenkins/)、TeamCity、Travis、Bamboo等CI/CD工具的優(yōu)勢(shì),來(lái)實(shí)現(xiàn)構(gòu)建、打包、以及測(cè)試過(guò)程的自動(dòng)化。
下表總結(jié)了Selenium的功能類別與對(duì)應(yīng)的工具:
云端Selenium測(cè)試的準(zhǔn)備
為了著手在云端開展Selenium測(cè)試,我們選擇LambdaTest(請(qǐng)參見--https://www.lambdatest.com/)作為基于云的跨瀏覽器測(cè)試平臺(tái)。您可以在該平臺(tái)上執(zhí)行以下任務(wù):
- 在線對(duì)2000多種真實(shí)的瀏覽器和操作系統(tǒng),進(jìn)行基于Selenium的自動(dòng)化和實(shí)時(shí)交互式跨瀏覽器測(cè)試。
- 只需更改最少量的代碼,即可將既有的測(cè)試移植到LambdaTest平臺(tái)上。
- 利用并發(fā)會(huì)話和并行執(zhí)行的方式,來(lái)更快獲取測(cè)試結(jié)果。
- 通過(guò)查看LambdaTest服務(wù)器上針對(duì)某項(xiàng)測(cè)試的調(diào)試日志,來(lái)跟蹤自動(dòng)化測(cè)試,并分析失敗的原因。
- 使用Remote Selenium WebDriver,為那些強(qiáng)大的編程語(yǔ)言(如C#、Python、Ruby on Rails等),生成并實(shí)現(xiàn)跨瀏覽器的功能。
- 使用詳細(xì)的報(bào)告機(jī)制,來(lái)分析待測(cè)系統(tǒng)的性能。
下面是使用LambdaTest在云端進(jìn)行Selenium測(cè)試的具體步驟:
步驟1:建立帳戶
首先,您需要先在LambdaTest上完成注冊(cè)。如上圖所示,您可以通過(guò)訪問注冊(cè)頁(yè)面--https://accounts.lambdatest.com/register,來(lái)創(chuàng)建一個(gè)新的帳戶。完成后,請(qǐng)保存用戶名和訪問令牌,以便在登錄LambdaTest儀表板時(shí)使用。
步驟2:選擇定價(jià)方案
根據(jù)測(cè)試的要求,您可以選擇適當(dāng)?shù)亩▋r(jià)方案。目前,LambdaTest有Lite、Live、Web與移動(dòng)瀏覽器自動(dòng)化、以及Web自動(dòng)化四種方案可供選擇。您雖然可以隨時(shí)對(duì)已選定的方案進(jìn)行升/降級(jí),但是其更改只能反映在下一個(gè)結(jié)算周期中。有關(guān)這四種方案的詳細(xì)說(shuō)明,請(qǐng)參見定價(jià)頁(yè)面--https://www.lambdatest.com/pricing。
步驟3:瀏覽器功能生成器(Browser Capabilities Generator)
由于要在云端執(zhí)行測(cè)試,因此您需要在Selenium Grid云上配置各種屬性,其中包括:選擇編程語(yǔ)言,指定用于測(cè)試的操作系統(tǒng)、瀏覽器類型、分辨率、以及版本號(hào)。
如上圖所示,您可以登錄平臺(tái),使用功能生成器來(lái)生成相關(guān)功能,以便執(zhí)行跨瀏覽器的各種測(cè)試。之后,您需要轉(zhuǎn)到“自動(dòng)化”選項(xiàng)卡,以監(jiān)視自動(dòng)化瀏覽器的測(cè)試狀態(tài)。同時(shí),每一個(gè)測(cè)試都將具有與之相關(guān)聯(lián)的測(cè)試ID和內(nèi)部版本ID。其格式為--https://automation.lambdatest.com/logs/?testID=< test-id >&build=< build-id >。
執(zhí)行云端Selenium測(cè)試
有了前面的基礎(chǔ),下面讓我們來(lái)看一個(gè)Python示例,它演示了如何使用LambdaTest上的遠(yuǎn)程Selenium Grid,在云端進(jìn)行Selenium測(cè)試。
串行測(cè)試
串行測(cè)試(Serial testing)是一種以串行方式(即,逐個(gè)順次進(jìn)行)執(zhí)行測(cè)試的實(shí)踐。它會(huì)在上一個(gè)測(cè)試完成之后,再執(zhí)行下一個(gè)測(cè)試。使用串行測(cè)試的缺點(diǎn)是:由于沒有充分利用Selenium測(cè)試框架和云基礎(chǔ)架構(gòu)的資源,因此它增加了測(cè)試執(zhí)行的時(shí)間。針對(duì)此類測(cè)試,我們來(lái)討論如下的測(cè)試步驟:
- 在Firefox瀏覽器中打開DuckDuckGo。
- 找到搜索框。
- 輸入查詢的內(nèi)容,即LambdaTest。
- 執(zhí)行搜索操作。
- 釋放資源。
具體實(shí)現(xiàn)代碼為:
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()
而如下代碼提供了:可登錄平臺(tái)的用戶名和訪問令牌的組合。
- 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來(lái)生成瀏覽器和平臺(tái)的功能,同時(shí)需要選擇用于實(shí)現(xiàn)的編程語(yǔ)言,以及瀏覽器規(guī)格。在此,我們的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元素的詳細(xì)信息,在該例子中,即為DuckDuckGo主頁(yè)上的搜索框。
我們?cè)赟elenium中、以及代碼中都使用到了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測(cè)試,我們使用了unittest框架,以及Selenium測(cè)試套件,從終端調(diào)用python命令—“python
您可以通過(guò)導(dǎo)航到相應(yīng)的測(cè)試ID和內(nèi)部版本ID,以檢查該自動(dòng)化測(cè)試的狀態(tài)。
并行測(cè)試
Selenium中的并行測(cè)試(Parallel testing)是指:通過(guò)并發(fā)運(yùn)行自動(dòng)化測(cè)試套件或用例,來(lái)減少測(cè)試的執(zhí)行時(shí)間。雖然測(cè)試套件的組合是在多臺(tái)服務(wù)器上被并行執(zhí)行,但是每臺(tái)服務(wù)器一次仍然只運(yùn)行一個(gè)測(cè)試。
為了在LambdaTest上快速執(zhí)行,我們?cè)诓煌?wù)器上,以并發(fā)的方式來(lái)觸發(fā)會(huì)話。如下圖所示,我們調(diào)用了兩個(gè)并發(fā)的會(huì)話。有關(guān)并行執(zhí)行的測(cè)試案例、以及具體的定價(jià)方案,請(qǐng)參見:https://www.lambdatest.com/pricing。
我們?cè)O(shè)計(jì)了在Chrome和IE兩種瀏覽器中開展并行測(cè)試的案例,它們遵從上述串行測(cè)試的步驟。因此,在LambdaTest服務(wù)器上的并發(fā)會(huì)話為2比2。
在具體實(shí)現(xiàn)中,我們使用LambdaTest Capabilities Generator生成了針對(duì)Chrome和IE瀏覽器的兩種瀏覽器功能。
其中,測(cè)試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
- }
測(cè)試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
- }
其余的實(shí)現(xiàn)代碼與串行測(cè)試相同。下面展示的是針對(duì)IE瀏覽器的實(shí)現(xiàn)代碼:
- # 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()
如下圖所示,在并行執(zhí)行中,針對(duì)不同瀏覽器的兩個(gè).py文件,都會(huì)在同一處被終端所調(diào)用。
從LambdaTest的執(zhí)行快照中,我們可以看出:并行測(cè)試的并發(fā)會(huì)話數(shù)越多,總體執(zhí)行時(shí)間就越短。
小結(jié)
雖然Selenium是用于自動(dòng)化Web瀏覽器測(cè)試的強(qiáng)大框架,但是如果我們將其用于本地基礎(chǔ)架構(gòu)的測(cè)試,那么測(cè)試設(shè)備的性能、可擴(kuò)展性、以及吞吐量都會(huì)受到限制。因此,企業(yè)需要在云端針對(duì)Web應(yīng)用、網(wǎng)站和移動(dòng)應(yīng)用進(jìn)行Selenium測(cè)試。而并行測(cè)試的方式,往往能夠加速整個(gè)執(zhí)行的進(jìn)程。為了獲得更好的可擴(kuò)展性和更優(yōu)越的性能,我們可以使用LambdaTest之類的云平臺(tái),在Selenium中進(jìn)行各種跨瀏覽器的功能性測(cè)試。
原文標(biāo)題:All You Need to Know for Selenium Testing on the Cloud,作者: Himanshu Sheth
【51CTO譯稿,合作站點(diǎn)轉(zhuǎn)載請(qǐng)注明原文譯者和出處為51CTO.com】