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