• 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 的先決條件

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

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

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

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

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

Selenium Grid Configuration 1

Selenium Grid 中的獨立模式配置

在最新版本的 Selenium Grid 中,無需分別啟動 Hub 和 Node。因此,可以在同一臺機器上同時觸發 Hub 和 Node。使用單個獨立命令,Hub 和 Node 將同時啟動。因此,獨立模式有助於節省資源和時間。

配置獨立模式的步驟如下所列 -

步驟 1 - 從以下連結下載 Selenium 獨立 Jar 並將其儲存在資料夾中 -

https://github.com/SeleniumHQ/.

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

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

步驟 3 - 透過開啟瀏覽器並輸入https://:4444再次檢查 Grid 狀態。

錯誤 - 無法訪問此站點將不再存在,我們將獲得顯示不同瀏覽器的 Grid 狀態。這將證明 Selenium Grid 已在獨立模式下觸發。

Selenium Grid Configuration 2

步驟 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();
   }
}

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 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 的獨立模式。

Selenium Grid 中的 Hub 和 Node 模式配置

在最新版本的 Selenium Grid 中,Hub 和 Node 模式需要在單獨的機器上觸發 Hub 和 Node。Hub 包含路由器、分發器、會話對映、新會話佇列和事件匯流排等元件。

配置 Hub 和 Node 模式的步驟如下所列 -

步驟 1 - 從以下連結下載 Selenium 獨立 Jar 並將其儲存在資料夾中 -

https://github.com/SeleniumHQ/selenium/releases.

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

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

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

步驟 3 - 透過開啟瀏覽器並輸入https://:4444檢查 Grid 狀態。

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

Selenium Grid Configuration 3

在同一臺機器上執行節點

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

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

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

透過開啟瀏覽器並輸入https://:4444再次檢查 Grid 狀態。

顯示的訊息 - Grid 尚未註冊任何節點將不存在,取而代之的是,節點將反映出來。

Selenium Grid Configuration 4

在不同的機器上執行節點

步驟 1 - 在另一臺機器上從以下連結下載 Selenium 獨立 Jar 並將其儲存在資料夾中 -

https://github.com/SeleniumHQ/selenium/releases.

步驟 2 - 導航到 Selenium 獨立 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 時收到的命令列日誌中提供。

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

步驟 3 - 透過開啟瀏覽器並輸入https://:4444再次檢查 Grid 狀態。

顯示的訊息 - Grid 尚未註冊任何節點將不存在,取而代之的是,節點將反映出來。

Selenium Grid Configuration 5

步驟 4 - 執行為獨立模式編寫的相同測試,該測試來自 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 中的分散式配置

當需要設定大量節點以構建大型 Grid 時,使用 Selenium Grid 的分散式模式,其中只有一個 Hub 和許多分佈在多臺機器上的節點。在這種情況下,Hub 和 Node 模式不是理想的選擇。

此外,在分散式模式下,每個元件(路由器、分發器、會話對映、新會話佇列和事件匯流排)都可以使用不同的命令單獨啟動。

配置分散式模式的步驟如下所列 -

步驟 1 - 從以下連結下載 Selenium 獨立 Jar 並將其儲存在資料夾中 -

https://github.com/SeleniumHQ/selenium/releases

步驟 2 - 從 Selenium 獨立 Jar 所儲存的資料夾位置,從終端執行以下命令以啟動事件匯流排 -

java -jar selenium-server-<version>.jar event-bus.

請注意在終端的命令列日誌中事件匯流排啟動時的IP地址。

步驟 3 − 在按照步驟 2 配置事件匯流排後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令以啟動 Session Map −

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

請注意在終端的命令列日誌中 Session Map 啟動時的IP地址。

步驟 4 − 在按照步驟 3 配置 Session Map 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令以啟動 Session Queue −

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

請注意在終端的命令列日誌中 Session Queue 啟動時的IP地址。

步驟 5 − 在按照步驟 4 配置 Session Queue 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令以啟動 Distributor −

java -jar selenium-server-<version>.jar distributor 
   --sessions http://{IP Address of Session Map}:5556 
   --sessionqueue http://{IP Address of Session Queue}:5559 --bind-bus false.

請注意在終端的命令列日誌中 Distributor 啟動時的IP地址。

步驟 6 − 在按照步驟 5 配置 Distributor 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令以啟動 Router −

java -jar selenium-server-<version>.jar router 
   --sessions http://{IP Address of Session Map}:5556 
   --distributor http://{IP Address of Distributor}:5553 
   --sessionqueue http://{IP Address of Session Queue}:5559.

請注意在終端的命令列日誌中 Router 啟動時的IP地址。

在按照步驟 6 配置 Router 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令 −

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

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

在不同的機器上執行節點

在按照步驟 6 配置 Router 後,導航到儲存 Selenium 獨立 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 和 Node 配置期間註冊 Hub 時收到的 Hub IP 地址。

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

這總結了我們關於 Selenium Grid - 配置教程的全面概述。我們從描述配置 Selenium Grid 的先決條件開始,並逐步介紹了 Selenium Grid 中獨立、Hub 和 Node 以及分散式模式配置的步驟。

這使您具備了 Selenium Grid 配置的深入知識。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。

廣告