如何在不同的浏览器中设置Selenium网格以并行执行
到目前為止,Selenium是最常用的Web自動化測試工具。 如此受歡迎的原因之一是Selenium的自動跨瀏覽器測試功能。 硒自動化測試可以幫助您在所有主要瀏覽器,所有主要操作系統甚至移動設備瀏覽器上進行測試。 您可以在所有功能測試中獲得廣泛的瀏覽器覆蓋,從而確保為廣大潛在用戶提供完美的體驗。
但是,這是一個挑戰。 您只能在本地計算機上加載這么多不同的瀏覽器。 例如,如果您有一臺Windows計算機,則無法在Safari瀏覽器上進行測試,同樣,如果您有一臺Mac計算機,則將無法在IE或Edge瀏覽器上進行測試。 此外,在一臺機器上運行測試是一個耗時的過程。 如果您有多臺計算機,為什么要一次測試一臺呢?
理想的方案是在具有不同瀏覽器環境的互連機器網絡上進行測試,該瀏覽器環境同時并行運行多個測試用例,并將總測試時間減少數倍。 在這里,我還包括跨瀏覽器測試所花費的時間。
這正是Selenium Grid的基礎。 與Selenium WebDriver允許您以順序方式執行自動跨瀏覽器測試不同 ,Selenium Grid設置將允許您同時在不同的瀏覽器/瀏覽器版本中運行測試用例。
什么是硒網格?
在開始Selenium Grid設置之前,有必要了解Selenium Grid的基礎知識。
因此,Selenium Grid基本上允許我們針對不同的瀏覽器在不同的操作系統上運行自動化測試。 而且這也是并行的。 Selenium自動化測試工具將幫助您更快地進行跨瀏覽器自動化測試,并有效利用內部硬件。
Selenium Grid由兩個概念組成:
- 集線器:它是Selenium Grid設置體系結構的中心,用于管理測試機器的網絡。 網絡中只有一個集線器被分配給DesiredCapabilities測試(操作系統,瀏覽器,瀏覽器版本),然后集線器會找到與給定配置匹配的測試。
- 節點:節點是執行先前在集線器上加載的測試的測試計算機。 可以有多個節點配置了不同的操作系統和不同的瀏覽器。 節點在運行集線器的同一平臺上運行不是強制性的。
配置Selenium Grid設置以并行執行的基礎結構僅涉及兩個步驟:
使用命令提示符進行Selenium Grid設置
Selenium Grid設置的第一步是創建一個集線器。 您必須確保系統安裝了Java Runtime Environment(JRE)或更好的Java Development Kit(JDK)。 盡管大多數人建議使用最新的JDK,但我更喜歡早先經過測試和測試的版本,例如JDK SE 08或09。如果愿意,可以選擇最新的版本。 以下是后續步驟–
- 下載并解壓縮Selenium Standalone服務器JAR文件。 您可以從此處下載它們。
- 接下來,打開命令提示符或終端,然后導航到保存Selenium Standalone Server jar文件的目錄。
- 運行命令“ java -jar selenium-server-standalone-3.141.59.jar -role hub”。 默認情況下,此命令將在端口4444上啟動Selenium Grid集線器。 您還可以通過定向到http:// localhost:4444 / grid / console來進行檢查 。
由于已經創建了集線器,因此為并行執行而設置Selenium Grid的下一步將涉及啟動節點。 首先,轉到要在其中設置節點的其他計算機。 同樣,這些機器應具有JDK / JRE設置。
在節點機器上,打開命令提示符或終端,然后導航到保存瀏覽器驅動程序文件的目錄。
輸入以下命令,以便在Selenium Grid設置中配置Chrome驅動程序以并行執行 。
“java -Dwebdriver.chrome.driver= "C:\chromedriver.exe" -jar selenium-server-standalone- 3.141 . 59 .jar -role webdriver -hub http: //10.0.0.22:4444/grid/register -port 4546”- 在上面的命令中,我已將計算機IP以及集線器實際運行的端口4444一起使用。 我還選擇了要我的節點注冊的端口4546。 您可以選擇任何空閑端口來注冊節點。
同樣,您可以為其他瀏覽器注冊其他節點。 例如,這是將Firefox(Gecko)驅動程序和Internet Explorer驅動程序配置到Selenium Grid設置以并行執行的方法。
在Selenium Grid設置中配置Firefox(Gecko)驅動程序:
“java -Dwebdriver.gecko.driver= "C:\geckodriver.exe" -jar selenium-server-standalone- 3.141 . 59 .jar -role webdriver -hub http: //10.0.0.22:4444/grid/register -port 5566”在Selenium Grid設置中配置IE(Internet Explorer)驅動程序:
“java -Dwebdriver.ie.driver= "C:\IEDriverServer.exe" -jar selenium-server-standalone- 3.141 . 59 .jar -role webdriver -hub http: //10.0.0.22:4444/grid/register -port 4547”注意:請確保為不同的節點選擇不同的端口以與在端口4444上運行的單個集線器連接。此外,在IE上運行測試之前,請確保瀏覽器縮放正好是100%,否則可能顯示錯誤。
您可以通過上面的命令注意到,我們正在端口4546上運行chrome驅動程序的第一個節點,firefox驅動程序的第二個節點在端口5566上運行,而IE瀏覽器的第三個節點在端口4547上運行。這是輸出屏幕截圖供進一步參考:
現在,我們已經配置了Selenium Grid設置,下一步就是為您的跨瀏覽器測試套件執行Selenium自動化測試。
使用Selenium Grid執行自動化測試以并行執行
現在該進行一些自動化的跨瀏覽器測試了! 我將運行一個自動化腳本,演示Selenium Grid的并行執行。 該腳本可以在Chrome,Firefox和Internet Explorer上并行運行,這些瀏覽器在不同的端口上注冊并附加到單個集線器。
這是我使用Selenium Grid執行自動化測試以在不同瀏覽器中并行執行的示例代碼。
package DemoAutomation; ? import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; ? import org.openqa.selenium.By; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Parameters; import org.testng.annotations.Test; ?? public class Selenium_MultiBrowser_Test { WebDriver driver; String nodeURL; ????@Parameters ({ "Port" }) @BeforeMethod () public void setUp(String Port) throws MalformedURLException { if (Port.equalsIgnoreCase( "4546" )) { nodeURL = " http://10.0.0.22:4546/wd/hub " ; System.out.println( "Chrome Browser Initiated" ); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setBrowserName( "chrome" ); capabilities.setPlatform(Platform.WINDOWS); ????????????driver = new RemoteWebDriver( new URL(nodeURL),capabilities); ????????????driver.get( " https://www.apple.com/ " ); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait( 10 , TimeUnit.SECONDS); } ????????else if (Port.equalsIgnoreCase( "5566" )) { nodeURL = " http://10.0.0.22:5566/wd/hub " ; System.out.println( "Firefox Browser Initiated" ); DesiredCapabilities capabilities1 = DesiredCapabilities.firefox(); capabilities1.setBrowserName( "firefox" ); capabilities1.setPlatform(Platform.WINDOWS); ????????????????driver = new RemoteWebDriver( new URL(nodeURL),capabilities1); ????????????????driver.get( " https://www.apple.com/ " ); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait( 10 , TimeUnit.SECONDS); } ????????else ????????????if (Port.equalsIgnoreCase( "4547" )) { nodeURL = " http://10.0.0.22:4547/wd/hub " ; System.out.println( "Internet Browser Initiated" ); DesiredCapabilities capabilities2 = DesiredCapabilities.internetExplorer(); capabilities2.setBrowserName( "internet explorer" ); capabilities2.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true ); capabilities2.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true ); capabilities2.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true ); "ignoreProtectedModeSettings" capabilities2.setCapability( "ignoreProtectedModeSettings" , true ); "nativeEvents" capabilities2.setCapability( "nativeEvents" , false ); capabilities2.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "" ); capabilities2.setCapability(InternetExplorerDriver.LOG_LEVEL, "DEBUG" ); ?????????????????????????capabilities2.setPlatform(Platform.WINDOWS); ????????????driver = new RemoteWebDriver( new URL(nodeURL),capabilities2); ????????????driver.get( " https://www.apple.com/ " ); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait( 10 , TimeUnit.SECONDS); } } ????@Test public void appleSite() throws InterruptedException { try { ????????driver.findElement(By.xpath( "//*[@id=\'ac-globalnav\']/div/ul[2]/li[3]" )).click(); Thread.sleep( 2000 ); ?driver.findElement(By.cssSelector( "#chapternav > div > ul > li.chapternav-item.chapternav-item-ipad-air > a > figure" )).click(); Thread.sleep( 2000 ); ?driver.findElement(By.linkText( "Why iPad" )).click(); Thread.sleep( 2000 ); } ????????catch (Exception e) { System.out.println(e.getMessage()); } } ?????@AfterMethod () public void tearDown() { driver.quit(); System.out.println( "Browser Closed" ); } }上面是用XML文件配置的Java類文件,該XML文件包括在Java文件中傳遞的參數值,并且還有助于創建一組可以并行運行的不同類。
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE suite SYSTEM " http://testng.org/testng-1.0.dtd " > <suite thread-count= "3" name= "BlogSuite" parallel= "tests" > ? <test name= "Chrome Test" > <parameter name= "Port" value= "4546" /> <classes> < class name= "DemoAutomation.Selenium_MultiBrowser_Test" /> ??????</classes> </test> ??<test name= "Firefox Test" > <parameter name= "Port" value= "5566" /> <classes> < class name= "DemoAutomation.Selenium_MultiBrowser_Test" /> ??????</classes> </test> ????<test name= "Internet Explorer Test" > <parameter name= "Port" value= "4547" /> <classes> < class name= "DemoAutomation.Selenium_MultiBrowser_Test" /> ??????</classes> </test> ????</suite>輸出畫面:
在上面的代碼中,我使用了DesiredCapabilities類,該類將幫助您設置Selenium WebDriver的屬性。 這些屬性可用于配置要在其上運行腳本的瀏覽器實例,例如Br??owserName和BrowserVersion。
注意:但是,該腳本只能在測試計算機上當前安裝的瀏覽器和瀏覽器版本上運行。 假設,如果您嘗試在Chrome 72上運行測試,而測試計算機上僅安裝了Chrome 74,那么腳本將顯示錯誤。 當您選擇與測試計算機的操作系統相比不同的操作系統時,會發生相同的情況。
此外,每次啟動新的操作系統時,在新的Mac和Windows環境中進行投資都是相當昂貴的。
因此,如果您希望在不同的瀏覽器,瀏覽器版本和操作系統上執行自動跨瀏覽器測試,那么我建議您尋找一種可在云上提供Selenium Grid設置的工具,例如LambdaTest。
在LambdaTest Selenium Grid Cloud上運行自動化測試
讓我們嘗試在LambdaTest Selenium Grid上運行相同的測試用例。 您可以使用LambdaTest 所需功能生成器來配置所需功能對象。 手動完成后,這將節省您在Selenium Grid設置中花費的大量時間。
使用LambdaTest,您只需要創建一個遠程服務器,在腳本中添加LambdaTest用戶名,訪問密鑰和Grid URL,就可以了。
下面是TestNG框架中的Java代碼,在這種情況下會更有利:
package lambdatest; import java.net.MalformedURLException; import java.net.URL; ? import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; ?? public class SampleTest { public String username = "enteryourusernamehere" ; public String accesskey = "enteryourkeyhere" ; public RemoteWebDriver driver = null ; public String gridURL = "@hub.lambdatest.com/wd/hub" ; boolean status = false ; ?@BeforeTest @org .testng.annotations.Parameters(value={ "browser" , "version" , "platform" }) public void setUp(String browser, String version, String platform) throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); "browserName" capabilities.setCapability( "browserName" , browser); capabilities.setCapability( "version" , version); capabilities.setCapability( "platform" , platform); // If this cap isn't specified, it will just get the any available one capabilities.setCapability( "build" , "Selenium Grid" ); capabilities.setCapability( "name" , "Sample Test" ); capabilities.setCapability( "network" , true ); // To enable network logs capabilities.setCapability( "visual" , true ); // To enable step by step screenshot capabilities.setCapability( "video" , true ); // To enable video recording capabilities.setCapability( "console" , true ); // To capture console logs try { driver = new RemoteWebDriver( new URL( " https:// " + username + ":" + accesskey + gridURL), capabilities); } catch (MalformedURLException e) { System.out.println( "Invalid grid URL" ); } catch (Exception e) { System.out.println(e.getMessage()); } } ?@Test public void testSimple() throws Exception { try { ????????????driver.get( " https://www.apple.com/ " ); driver.manage().window().maximize(); ????????????driver.findElement(By.xpath( "//*[@id=\'ac-globalnav\']/div/ul[2]/li[3]" )).click(); Thread.sleep( 2000 ); ?driver.findElement( By.cssSelector( "#chapternav > div > ul > li.chapternav-item.chapternav-item-ipad-air > a" )).click(); Thread.sleep( 2000 ); ?driver.findElement(By.linkText( "Why iPad" )).click(); Thread.sleep( 2000 ); ??????????} catch (Exception e) { System.out.println(e.getMessage()); } } ???@AfterTest public void tearDown() throws Exception { if (driver != null ) { ((JavascriptExecutor) driver).executeScript( "lambda-status=" + status); driver.quit(); } } }現在,我們將使用包含Java文件中傳遞的參數值的XML文件運行此Java類文件,還有助于創建一組不同的類,使用這些類,我們可以使用Selenium Grid進行自動化測試以在以下位置并行執行不同的配置。
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE suite SYSTEM " http://testng.org/testng-1.0.dtd " > <suite thread-count= "3" name= "BlogSuite" parallel= "tests" > ?<test name= "FirefoxTest" > <parameter name= "browser" value= "firefox" /> <parameter name= "version" value= "62.0" /> <parameter name= "platform" value= "WIN8" /> <classes> < class name= "lambdatest.SampleTest" /> </classes> </test> ?<test name= "ChromeTest" > <parameter name= "browser" value= "chrome" /> <parameter name= "version" value= "70.0" /> <parameter name= "platform" value= "WIN10" /> <classes> < class name= "lambdatest.SampleTest" /> </classes> </test> ?<test name= "SafariTest" > <parameter name= "browser" value= "safari" /> <parameter name= "version" value= "11.0" /> <parameter name= "platform" value= "macos High Sierra" /> <classes> < class name= "lambdatest.SampleTest" /> </classes> </test> ? </suite>輸出畫面:
現在,我們已經成功地使用Selenium Grid執行了自動化測試,可以在不同的瀏覽器以及不同的操作系統上并行執行,而無需創建集線器并在不同端口上啟動節點。 當測試在Selenium Grid上運行時,您可以看到測試的實時視頻流以及各種其他詳細信息,例如命令,其中包括腳本傳遞的每個命令的屏幕截圖,以及引發的日志和異常。
您還可以創建自己的團隊,并以團隊形式運行自動跨瀏覽器兼容性測試腳本。 這些測試結果對于團隊中添加的每個成員都是可見的。 此外,通過我們的單擊集成,您的隊友可以將在跨瀏覽器測試期間發現的所有錯誤直接記錄到Jira,Trello,Asana,Mantis,GitHub等各種項目管理平臺上。此外,LambdaTest還與CI / CD集成與您的自動化測試工具(例如CircleCI,Jenkins,TravisCI等)互補的平臺。請查看所有LambdaTest集成 。
結論
如果手動完成,Selenium Grid設置可能會很困難。 如果Selenium Grid的主要目的是并行運行測試并在不同的配置上進行測試,則可以在LambdaTest上執行相同的測試,而無需花費時間和資源來創建Selenium Grid Hub和Selenium Grid節點。 因此,請通過在此在線Grid上運行自動化測試來嘗試一下,并在注釋框中告知我們有關使用LambdaTest的反饋的信息。 測試愉快!
由
在LambdaTest擔任自動化專家,最近開始了專業之旅。 Java測試自動化中的Excel。
翻譯自: https://www.javacodegeeks.com/2019/07/setup-selenium-grid-parallel-execution-different-browsers.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的如何在不同的浏览器中设置Selenium网格以并行执行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: db2分页sql_停止尝试使用内部DB框
- 下一篇: 通心粉用冷水还是热水煮 通心粉的正确煮法