Modifying the existing test script to use Selenium Grid

So far, we have seen test scripts that run on our local machines or on Selenium Standalone servers. Executing test scripts on Selenium Grid is very similar to executing tests on Remote WebDriver, except that you will also mention the platform details for Grid.

Let's look at a test script that uses the Remote WebDriver server:

public class SearchTest {

WebDriver driver;

@BeforeMethod
public void setup() throws MalformedURLException {

DesiredCapabilities caps = new DesiredCapabilities();

caps.setBrowserName("chrome");
caps.setPlatform(Platform.MAC);

driver = new RemoteWebDriver(new URL("http://192.168.0.101:1111/wd/hub"), caps);
driver.get("http://demo-store.seleniumacademy.com/");

}

@Test
public void searchProduct() {

// find search box and enter search string
WebElement searchBox = driver.findElement(By.name("q"));

searchBox.sendKeys("Phones");

WebElement searchButton =
driver.findElement(By.className("search-button"));

searchButton.click();

assertThat(driver.getTitle())
.isEqualTo("Search results for: 'Phones'");
}

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

Now try executing the preceding test script and observe the log output of the hub and the node. The output log of the hub is as follows:

The sequence of steps that happens at the hub end is as follows:

  1. The hub gets a request to create a new session for platform=MAC, browserName=chrome.
  2. It verifies the available nodes that match the capabilities request.
  3. If available, it creates a new session with the node host; if not, it rejects the request from the test script, saying that the desired capabilities don't match any of the registered nodes.
  4. If a session is created with the node host in the preceding step, create a new test-slot session and hand over the test script to the node. Similarly, the output you should see in the console log of the hub is as follows:

The sequence of steps performed on the node is as follows:

  1. The node host creates a new session with the requested desired capabilities. This will launch the browser.
  2. It executes the test script's steps on the launched browser.
  3. It ends the session and forwards the result to the hub, which in turn sends it to the test script.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.102.225