We will use the Selenium Grid with TestNG. To run the TestCases with Selenium Grid on remote machine. You need to use RemoteWebDriver .
What is RemoteWebDriver?
RemoteWebDriver is an implementation class of theWebDriver interface that a test script developer can use to execute their test scripts via the RemoteWebDriver server on a remote machine.
There are two parts to RemoteWebDriver; a server and a client
The RemoteWebDriver server is a component that listens on a port for various requests from a RemoteWebDriver client. Once it receives the requests, it forwards them to any of the following: Firefox Driver, IE Driver, or Chrome Driver, whichever is asked.
How can you create the Object of RemoteWebDriver ?
To create the Object of RemoteWebDriver or invoke the RemoteWebDriver. We need to follow the below mentioned steps.
- First you need to create the object of DesiredCapabilities and assign the Browser to it. Browser could be firefox, chrome and IE. We are using firefox in our example.
- In Second step, you need to declare the BrowserName for DesiredCapabilities. Name could be firefox, chrome and iexplore.
- In third step you need to declare the Platform for DesiredCapabilities. It could be andriod, linux, iOS and Windows. Because we can run our tests on server as well so we will chose ANY in the option.
All code will be look like this -
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
cap.setPlatform(Platform.ANY);
- In Last we need to create the Object of Remote webdriver and pass the URL of selenium Hub and DesiredCapabilities object to it as parameter. It will look like-
public void searchTest() throws MalformedURLException{
//Declare desiredcapabilities
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox"); //chrome, iexplore
cap.setPlatform(Platform.ANY);
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox"); //chrome, iexplore
cap.setPlatform(Platform.ANY);
//Invoke the RemoteWebdriver
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
}
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
}
Execute Testcase using Selenium Grid
Now we will see, How can we execute the Test Cases using TestNG and Selenium Grid.
TestCase - Open the Yahoo.com and verify the Page Title.
We will create the Test Script for above Test Case and execute it using, Selenium Grid.
You know, To run Tests using Selenium Grid, We need RemoteWebDriver and you have seen how can we invoke the RemoteWebDriver in Selenium.
Now lets see the Test Script code for the above Test Case.
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
public class YahooTitle {
// Expected Title
private String expectedTittle = "Yahoo";
private String expectedTittle = "Yahoo";
@Test
public void verifyTitleTest() throws MalformedURLException {
public void verifyTitleTest() throws MalformedURLException {
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox"); // chrome, iexplore
cap.setPlatform(Platform.ANY);
cap.setBrowserName("firefox"); // chrome, iexplore
cap.setPlatform(Platform.ANY);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
// Open the website
driver.get("https://in.yahoo.com/");
driver.get("https://in.yahoo.com/");
// Get the actual title using selenium code
String actualTittle = driver.getTitle();
String actualTittle = driver.getTitle();
// Verify the page title using by comparison of Actual and Expected
Assert.assertEquals(expectedTittle, actualTittle);
driver.close();
Assert.assertEquals(expectedTittle, actualTittle);
driver.close();
}
}
In the above code, first we are invoking the RemoteWebDriver and later on we are using simple selenium code.
Now let's see how can we execute this-
- To execute the Test using Selenium Grid, First invoke the Selenium Grid Hub.
- After invoke the Hub, Invoke the Selenium Grid Node.
- Now modify the testng.xml file, according to your test case and package. For example, we are using below mentiuoned xml in testng.xml file.<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Grid Test" >
<test name="Verify Yahoo Title"><classes>
<class name="com.SeleniumBix.SampleTests.YahooTitle" />
</classes>
</test></suite> - Now run your Test Case using testng.xml .
You will see, No browser will open and close but your test has been executed. So that you will not be able to see the execution of Test case but you can see the Test Result on eclipse console and in the TestNG test report.
Comments
Post a Comment