To
generate and to execute selenium test scripts using grid,we
need to know about below concepts:
- DesiredCapabilites
- RemoteWebDriver
DesiredCapabilites:
- DesiredCapabilites is a class in org.openqa.selenium.remote.DesiredCapabilities package
DesiredCapabilities
are options that you can use to customize and configure a browser session- It gives facility to set the properties of the browser. Such as to
- set BrowserName
- Platform
- Version of Browser etc
- We have to execute multiple TestCases on multiple Systems
- Different browser with different version and different Operating System
RemoteWebDriver:
- RemoteWebDriver
is used to set which node (or machine) that our test will run
against.
package com.selenium.grid; import java.net.MalformedURLException; import java.net.URL; import java.util.Scanner; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class Grid { static String browser = null; static RemoteWebDriver driver = null; public static void main(String[] args) throws MalformedURLException { // Please Enter Your browser value in Console System.out.println("Please Enter Your browser value in Console"); Scanner sc = new Scanner(System.in); browser = sc.next(); // Please give your node IP address URL // if your HUB and Node are // same machine, if // different in localhost // give IP address nodeURL = "http://localhost:5555/wd/hub"; if (browser.equalsIgnoreCase("firefox")) { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities = DesiredCapabilities.firefox(); capabilities.setBrowserName("firefox"); URL grid_url = new URL("http://localhost:5555/wd/hub"); driver = new RemoteWebDriver(grid_url, capabilities); } else if (browser.equalsIgnoreCase("Chrome")) { System.setProperty("webdriver.chrome.driver", "Give your chromedriver path"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities = DesiredCapabilities.chrome(); capabilities.setBrowserName("chrome"); URL grid_url = new URL("http://localhost:5555/wd/hub"); driver = new RemoteWebDriver(grid_url, capabilities); } // Accessing google driver.get("http://www.google.com"); } }