InterviewSolution
| 1. |
How to build a Selenium Grid? |
|
Answer» To execute scripts two important LIBRARY packages DesiredCapabilities object and RemoteWebDriver object are required. DesiredCapabilities library is used to set browser type and the OS of Node. Use the below code to import the DesiredCapabilities object. import org.openqa.selenium.remote.DesiredCapabilities;The RemoteWebDriver object is used to select the Node on which TEST will be executed. Use the below code to import RemoteWebDriver object. import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.RemoteWebDriver;You can mouse HOVER on the logos of browser present in the Selenium Hub console to get the details like browserName and platform of the Node. Use below code to setup grid package TestGrid; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import java.net.MalformedURLException; import java.net.URL; import org.junit.Assert; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class TestGrid { static WebDriver driver; static String nodeUrl; @BeforeTest public void setup() throws MalformedURLException { nodeUrl = "http://192.168.2.11:4444/wd/hub"; DesiredCapabilities capabilities = DesiredCapabilities.CHROME(); capabilities.setBrowserName("chrome"); capabilities.setPlatform(Platform.WINDOWS); driver = new RemoteWebDriver(new URL(nodeUrl), capabilities); } @Test public void simpleTest() { driver.get("https://nikasio.com"); } @AfterTest public void afterTest() { driver.quit(); } }Here the test is divided into three test annotations @BeforeTest, @Test, and @AfterTest. In @BeforeTest, DesiredCapabilities and RemoteWebDriver objects are used to configure Node. In @Test directed Node to navigate to nikasio.com's home page.In @AfterTest, directed Node to quite the browser instance. On execution, you will get below output. May 18, 2018 4:15:07 PM org.openqa.selenium.remote.ProtocolHandshake createSession
|
|