• Selenium Video Tutorials

Selenium Grid - 測試執行



最新版本的 Selenium Grid 與舊版本的 Selenium Grid 有很多區別。舊版本的 Selenium Grid 只有 Hub 和 Node 兩種模式,而最新版本的 Selenium Grid 支援三種模式:獨立模式、Hub 和 Node 模式以及分散式模式。

以前的 Selenium Grid 版本僅包含 Hub 和 Node 兩種模式,而最新版本的 Selenium Grid 有六個元件,如路由器、分發器、節點、會話佇列、會話對映和事件匯流排。

Selenium Grid 是一種工具,可以將測試分佈到多個物理或虛擬機器上,以便我們可以並行(同時)執行指令碼。它極大地加快了測試過程,並在多個瀏覽器和平臺上執行測試,併為我們提供關於產品的快速準確的反饋。

Selenium Grid 允許我們執行多個 WebDriver 或 Selenium Remote Control 例項,允許並行執行使用相同程式碼庫的測試,因此程式碼不需要存在於它們執行的系統上。selenium-server-standalone 包含 Hub、WebDriver 和 Selenium RC,用於在網格中執行指令碼。

Selenium Grid 中測試執行的先決條件

步驟 1 - 在系統中安裝 Java(版本高於 8),並使用命令檢查它是否存在:java -version。如果安裝已成功完成,則將顯示已安裝的 Java 版本。

步驟 2 - 透過開啟瀏覽器並輸入以下內容來檢查網格狀態

  • 對於 UI 版本,鍵入https://:4444

  • 對於非 UI 版本,鍵入https://:4444/status

在這兩種情況下,我們都會收到錯誤 - 無法訪問此網站。因為 Selenium Grid 尚未啟動。

Selenium Grid Test Execution 1

Selenium Grid 中的測試執行

在 Hub 和 Node 模式下在 Selenium Grid 中設定測試執行的步驟如下所示:

步驟 1 - 從以下連結下載 Selenium Standalone Jar 並將其儲存到資料夾中:https://github.com/SeleniumHQ/selenium/releases

步驟 2 - 從儲存 Selenium Standalone Jar 的資料夾位置,從終端執行以下命令:

java -jar selenium-server-<version>.jar hub.

請注意 - 命令列日誌中顯示的 Hub IP 地址在註冊 Hub 時收到。

在下圖中,它顯示了 Hub 的成功觸發。

Selenium Grid Test Execution 2

步驟 3 - 透過開啟瀏覽器並輸入以下內容來檢查網格狀態:https://:4444

錯誤 - 無法訪問此網站將不再存在,並且我們將獲得顯示訊息的網格狀態 - 網格尚無註冊節點。這是因為節點尚未註冊。

Selenium Grid Test Execution 3

在同一臺機器上執行節點

按照上述步驟 1、2 和 3 配置 Hub 後,導航到儲存 Selenium Standalone Jar 的資料夾位置,並透過開啟另一個新終端視窗執行以下命令:

java -jar selenium-server-<version>.jar node.

這將有助於在同一臺機器上啟動節點。

在不同的機器上執行節點

步驟 1 - 在另一臺機器上從以下連結下載 Selenium Standalone Jar 並將其儲存到資料夾中:https://github.com/SeleniumHQ/selenium/releases

步驟 2 - 導航到儲存 Selenium Standalone Jar 的資料夾位置,並透過開啟終端視窗執行以下命令:

java -jar selenium-server-<version>.jar node 
   --detect-drivers true --publish-events tcp://{hub IP Address}:4442 
   --subscribe-events tcp://{hub IP Address}:4443.

請注意 - Hub IP 地址將在步驟 3 中獲得的註冊 Hub 時收到的命令列日誌中提供。

這將有助於在不同的機器上啟動節點。

在下圖中,它顯示了從同一臺機器成功註冊節點。

Selenium Grid Test Execution 4

步驟 3 - 透過開啟瀏覽器並輸入以下內容再次檢查網格狀態:https://:4444

顯示的訊息 - 網格尚無註冊節點將不存在,取而代之的是,節點將反映出來。

Selenium Grid Test Execution 5

步驟 4 -

Base.java中的程式碼實現

package BaseClass;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Base {
   public WebDriver setBrowser(String browserName) throws MalformedURLException {
      WebDriver driver = null;
      DesiredCapabilities dc = new DesiredCapabilities();

      if(browserName.equalsIgnoreCase("chrome")) {
         dc.setBrowserName("chrome");
      } else if(browserName.equalsIgnoreCase("edge")) {
         dc.setBrowserName("MicrosoftEdge");
      }

      // Initiate RemoteWebDriver
      driver = new RemoteWebDriver(new URL("https://:4444"),dc);
      return driver;
   }
}

TestOne.java中的程式碼實現

package Grid;

import BaseClass.Base;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;

public class TestOne extends Base {
   public WebDriver driver = null;
   @Test
   public void testOne() {
   
      // launch application
      driver.get("https://tutorialspoint.tw/selenium/practice/links.php");

      // get page title
      System.out.println("Page title is: " + driver.getTitle() + " obtained from testOne");
   }
   @BeforeMethod
   public void setup() throws MalformedURLException {
      driver = setBrowser("chrome");
   }

   @AfterMethod
   public void tearDown() {
   
      // quitting browser
      driver.quit();
   }
}

TestTwo.java中的程式碼實現

package Grid;

import BaseClass.Base;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;

public class TestTwo extends Base {
   public WebDriver driver = null;
   
   @Test
   public void testTwo() {
   
      // launch application
      driver.get("https://tutorialspoint.tw/selenium/practice/links.php");

      // get page title
      System.out.println("Page title is: " + driver.getTitle() + " obtained from testTwo");
   }
   
   @BeforeMethod
   public void setup() throws MalformedURLException {
      driver = setBrowser("edge");
   }
   
   @AfterMethod
   public void tearDown() {
   
      // quitting browser
      driver.quit();
   }
}

testng.xml 檔案中的配置。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Grid Test">
   <test thread-count = "5" name="Test">
      <classes>
         <class name="Grid.TestOne" />
         <class name="Grid.TestTwo"/>
      </classes>
   </test>
</suite>

步驟 5 - 從 testng.xml 檔案執行測試。

它將顯示以下輸出

Page title is: Selenium Practice - Links obtained from testOne
Page title is: Selenium Practice - Links obtained from testTwo

===============================================
Grid Test
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

在上面的示例中,我們使用 Selenium Grid 的 Hub 和 Node 模式執行了測試執行。

這總結了我們關於 Selenium Grid - 測試執行教程的全面內容。我們從描述在 Selenium Grid 中執行測試執行的先決條件開始,以及如何在 Selenium Grid 中實際執行測試執行。

這使您能夠深入瞭解 Selenium Grid - 測試執行。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。

廣告