Selenium - How to launch Chrome and Firefox browsers through Selenium Grid using RemoteWebDriver
Selenium - How to launch Chrome and Firefox browsers through Selenium Grid using RemoteWebDriver:
What is RemoteWebDriver?
In our previous post you have seen how to initialise Chrome\Firefox WebDriver in your local machine, but the limitation with that is you can launch the browser only on the machine on which you are running the code on and hence can utilise only one machine.
If you wish to run automation across multiple machines in parallel you will need to host a selenium standalone server (hub) on one of the machine and register the machines (hosting the browsers) as node to this hub. Now you can run the automation pointing to the hub URL and the hub intern takes care of sending the commands to the remote machines and for this you will need a RemoteWebDriver.
In short, RemoteWebDriver implements the WebDriver interface and is used to run automation on a remote machine.
The RemoteWebDriver has two parts, a server and a client. The RemoteWebDriver server listen on a particular port for the requests from a RemoteWebDriver client and on receiving the request forwards it to the browser (ChromeDriver\FirefoxDriver) on the remote machine (machine hosting the browser requested). The RemoteWebDriver client translates the requests to JSON payload and send it to the RemoteWebDriver server using JSON protocol the same way its done in local run.
Before we proceed further to the implementation part of it, I recommend you visit my previous two below mentioned posts. It will help you setup the Grid and download the required driver files.
Now to launch browsers via RemoteWebDriver, you will need to pass the browser name and the selenium server ip and port where you have setup your Grid. This can be on the same machine from where you are running your code or a different machine. Similarly the node registered on the grid can be on the same machine.
We will need to set the browser name in the capabilities on which you wish to execute your test and the Hub will send the request to the node which is hosting that browser. You can similarly set the platform and version if needed in the capability.
Other preferences and capabilities are optional and you can tweak them as per your need. The code is also available in my github repository at this location. Feel free to download the project and try running it in your local. Let me know if you face any issues.
What is RemoteWebDriver?
In our previous post you have seen how to initialise Chrome\Firefox WebDriver in your local machine, but the limitation with that is you can launch the browser only on the machine on which you are running the code on and hence can utilise only one machine.
If you wish to run automation across multiple machines in parallel you will need to host a selenium standalone server (hub) on one of the machine and register the machines (hosting the browsers) as node to this hub. Now you can run the automation pointing to the hub URL and the hub intern takes care of sending the commands to the remote machines and for this you will need a RemoteWebDriver.
In short, RemoteWebDriver implements the WebDriver interface and is used to run automation on a remote machine.
The RemoteWebDriver has two parts, a server and a client. The RemoteWebDriver server listen on a particular port for the requests from a RemoteWebDriver client and on receiving the request forwards it to the browser (ChromeDriver\FirefoxDriver) on the remote machine (machine hosting the browser requested). The RemoteWebDriver client translates the requests to JSON payload and send it to the RemoteWebDriver server using JSON protocol the same way its done in local run.
Before we proceed further to the implementation part of it, I recommend you visit my previous two below mentioned posts. It will help you setup the Grid and download the required driver files.
Now to launch browsers via RemoteWebDriver, you will need to pass the browser name and the selenium server ip and port where you have setup your Grid. This can be on the same machine from where you are running your code or a different machine. Similarly the node registered on the grid can be on the same machine.
We will need to set the browser name in the capabilities on which you wish to execute your test and the Hub will send the request to the node which is hosting that browser. You can similarly set the platform and version if needed in the capability.
Other preferences and capabilities are optional and you can tweak them as per your need. The code is also available in my github repository at this location. Feel free to download the project and try running it in your local. Let me know if you face any issues.
public static WebDriver initializeRemoteWebDriver(BrowserName browserName, String seleniumServerIP, int seleniumServerPort) { RemoteWebDriver driver = null; DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("browserName", browserName.toString().toLowerCase()); capabilities.setCapability("platform", "ANY"); capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true); switch (browserName) { case FIREFOX: FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.link.open_newwindow.restriction", 0); //To open new tab instead of new window on clicking on any link. capabilities.setCapability(FirefoxDriver.PROFILE, profile); capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.DISMISS); break; case CHROME: final ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("enable-automation"); chromeOptions.addArguments("--disable-infobars"); chromeOptions.addArguments("--disable-extensions"); chromeOptions.addArguments("--start-maximized"); Map prefs = new HashMap<String, Object>(); prefs.put("profile.default_content_setting_values.geolocation", 2); // 1:allow 2:block chromeOptions.setExperimentalOption("prefs", prefs); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.DISMISS); break; default: System.out.println("Initialize Remote Web Driver not implemented for " + browserName + " browser"); System.exit(1); } try { driver = new RemoteWebDriver(new URL("http://" + seleniumServerIP + ":" + seleniumServerPort + "/wd/hub"), capabilities); driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } catch (Exception ex) { System.out.printf(ex.getMessage()); } return driver; }
Comments
Post a Comment