https://medium.com/@naruapon/การสร้าง-selenium-grid-aba4356d6abe
ดาวน์โหลด Selenium Server
ตั้งค่า Hub — ตัวกลางในการควบคุม Node ต่าง ๆ ที่เชื่อมต่ออยู่
java -jar selenium-server-4.24.0.jar hub
java -jar selenium-server-4.24.0.jar standalone
java -jar selenium-server-4.24.0.jar standalone --max-sessions 5
ตรวจสอบสถานะของ Hub ได้โดยเปิดเบราว์เซอร์
http://localhost:4444/grid/console
ตั้งค่า Node — Node คือเครื่องที่รันการทดสอบบนเบราว์เซอร์ต่าง ๆ
java -jar selenium-server-4.24.0.jar -role node -hub http://<hub-ip>:4444/grid/register
java -jar selenium-server-4.24.0.jar -role node -hub http://x.x.x.x:4444/grid/register
-hub จะระบุที่อยู่ของ Hub ที่ Node จะเชื่อมต่อไป
กำหนดค่า Browser และ Platform
java -jar selenium-server-4.24.0.jar -role node -nodeConfig nodeConfig.json -hub http://<hub-ip>:4444/grid/register
java -jar selenium-server-4.24.0.jar -role node -nodeConfig nodeConfig.json -hub http://x.x.x.x:4444/grid/register
nodeConfig.json
{
"capabilities": [
{
"browserName": "firefox",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "chrome",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
}
],
"configuration": {
"nodeTimeout": 120,
"port": 5555,
"hubPort": 4444,
"hubHost": "localhost",
"maxSession": 5,
"register": true,
"registerCycle": 5000,
"nodeStatusCheckTimeout": 5000,
"role": "node"
}
}
เขียนสคริปต์ Selenium WebDriver เพื่อส่งการทดสอบไปยัง Grid
เดิม
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")
print(driver.title)
driver.quit()
ปรับแต่ง
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
hub_url = 'http://localhost:4444/wd/hub'
chrome_options = Options() # Remove the extra space
driver = webdriver.Remote(
command_executor=hub_url,
options=chrome_options
)
driver.get("http://www.google.com")
print(driver.title)
driver.quit()
ทำให้รันหลาย test case ด้วย unit test
import unittest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.firefox.options import Options as FirefoxOptions
class TestGoogleChrome(unittest.TestCase):
def setUp(self):
hub_url = 'http://localhost:4444/wd/hub'
chrome_options = Options()
self.driver = webdriver.Remote(
command_executor=hub_url,
options=chrome_options
)
def test_google_chrome(self):
self.driver.get("http://www.google.com")
print(f"Title in Chrome: {self.driver.title}")
self.assertIn("Google", self.driver.title)
def tearDown(self):
self.driver.quit()
class TestGoogleFirefox(unittest.TestCase):
def setUp(self):
hub_url = 'http://localhost:4444/wd/hub'
firefox_options = FirefoxOptions()
self.driver = webdriver.Remote(
command_executor=hub_url,
options=firefox_options
)
def test_google_firefox(self):
self.driver.get("http://www.google.com")
print(f"Title in Firefox: {self.driver.title}")
self.assertIn("Google", self.driver.title)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
Java เดิม
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LocalSeleniumTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
driver.quit();
}
}
Java ปรับแต่ง
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
class testGrid {
@Test
void test() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
}
รันหลายๆ การทดสอบ
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
@Execution(ExecutionMode.CONCURRENT)
public class testGrid {
@Test
void test() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test2() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test3() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.edge();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test4() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test5() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test6() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.edge();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test7() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test8() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test9() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.edge();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test10() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test11() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
void test12() {
String hubURL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.edge();
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
driver.get("http://www.google.com");
System.out.println("Title: " + driver.getTitle());
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
}
Robot Framework ร่วมกับ Selenium Grid
เพิ่มหากรันไม่ได้
pip install webdrivermanager
robotgrid.robot
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${HUB_URL} http://localhost:4444/wd/hub
${BROWSER} chrome
*** Test Cases ***
Test Google in Chrome
[Documentation] Test opening Google in Chrome through Selenium Grid
Open Browser http://www.google.com ${BROWSER} remote_url=${HUB_URL}
Title Should Be Google
[Teardown] Close Browser
browser เลือกเปลี่ยนได้ตามที่ hub และ node รองรับ เช่น chrome, firefox, edge, ie, safari