Why use Selenium Grid?
By: Yogi Is Ariyanto
Tags: Selenium, Selenium Test Automation, Selenium Grid, Selenium Webdriver, Web Automation Testing

Untuk setup automation testing untuk parallel execution menggunakan beda browser pada selenium Remotewebdriver dengan selenium grid. untuk menjalankan paralel test yang di perlu di perhatikan ada di:

File BaseTest pada setup Capability contoh seperti berikut:

package base; import driver.ThreadLocalDriver;

import org.openqa.selenium.Platform;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.firefox.FirefoxOptions;

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.Parameters; import java.net.URL;

import java.util.concurrent.TimeUnit; public class BaseTest extends DesiredCapabilities { @BeforeMethod

@Parameters(value = {"browser"})

public void preparation(String browser) throws Exception {

if (browser.equals("chrome")) {

new DesiredCapabilities();

DesiredCapabilities capability = chrome();

capability.setBrowserName("chrome");

capability.setPlatform(Platform.MAC);

ChromeOptions options = new ChromeOptions();

options.merge(capability);

ThreadLocalDriver.setTLDriver(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options));

} else if (browser.equals("firefox")) {

new DesiredCapabilities();

DesiredCapabilities capability = firefox();

capability.setBrowserName("firefox");

capability.setPlatform(Platform.MAC);

FirefoxOptions options = new FirefoxOptions();

options.merge(capability);

ThreadLocalDriver.setTLDriver(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options));

} ThreadLocalDriver.getTLDriver().manage().window().maximize();

ThreadLocalDriver.getTLDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @AfterMethod

public void stopServer() {

ThreadLocalDriver.getTLDriver().quit();

} }

Untuk selanjutnya setup di runner class seperti berikut:

ChromeRunner

package runner; import base.BaseTest;

import cucumber.api.CucumberOptions;

import cucumber.api.testng.CucumberFeatureWrapper;

import cucumber.api.testng.TestNGCucumberRunner;

import org.testng.annotations.*;

@CucumberOptions(

features = "src/main/resources/features",

glue = {"steps"},

tags = {"@test"}

)

public class ChromeRunner extends BaseTest { private TestNGCucumberRunner testNGCucumberRunner; @BeforeClass(alwaysRun = true)

public void setUpClass() {

System.out.println("Setup TestNG for Cucumber");

testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());

} @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")

public void feature(CucumberFeatureWrapper cucumberFeature) {

System.out.println("In Test TestNG for Cucumber");

testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());

} @DataProvider(parallel = true)

public Object[][] features() {

return testNGCucumberRunner.provideFeatures();

} @AfterClass(alwaysRun = true)

public void tearDownClass() {

System.out.println("Final Session TestNG for Cucumber");

testNGCucumberRunner.finish();

}

}

Firefox Runner

package runner; import base.BaseTest;

import cucumber.api.CucumberOptions;

import cucumber.api.testng.CucumberFeatureWrapper;

import cucumber.api.testng.TestNGCucumberRunner;

import org.testng.annotations.*;

@CucumberOptions(

features = "src/main/resources/features",

glue = {"steps"},

tags = {"@test"}

)

public class FirefoxRunner extends BaseTest { private TestNGCucumberRunner testNGCucumberRunner; @BeforeClass(alwaysRun = true)

public void setUpClass() {

System.out.println("Setup TestNG for Cucumber");

testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());

} @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")

public void feature(CucumberFeatureWrapper cucumberFeature) {

System.out.println("In Test TestNG for Cucumber");

testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());

} @DataProvider(parallel = true)

public Object[][] features() {

return testNGCucumberRunner.provideFeatures();

} @AfterClass(alwaysRun = true)

public void tearDownClass() {

System.out.println("Final Session TestNG for Cucumber");

testNGCucumberRunner.finish();

}

}

Untuk di file testng.xml yang tak kalah penting untuk settingan paralel execution berjalan seperti berikut:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Parallel test suite" parallel="tests" thread-count="2">

<test name="Regression 1">

<parameter name="browser" value="chrome"/>

<classes>

<class name="runner.ChromeRunner">

</class>

</classes>

</test>

<test name="Regression 2">

<parameter name="browser" value="firefox"/>

<classes>

<class name="runner.FirefoxRunner">

</class>

</classes>

</test>

</suite>

Output Result:

Pada codingan di atas, menggunakan class DesiredCapabilities untuk mengatur properti pada Selenium WebDriver. Properti pada Capability digunakan untuk konfigurasi browser seperti BrowserName BrowserVersion, lokasi script yang akan di Execute dan lainnya.