在企業構建大規模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測試框架和雲基礎架構的資源,因此它增加了測試執行的時間。針對此類測試,我們來討論如下的測試步驟:
在Firefox瀏覽器中開啟DuckDuckGo。找到搜尋框。輸入查詢的內容,即LambdaTest。執行搜尋操作。釋放資源。具體實現程式碼為:
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 = "[email protected]" 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中進行各種跨瀏覽器的功能性測試。