Skip to main content

TestNG with Selenium Grid

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. 

  1. 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.
  2. In Second step, you need to declare the BrowserName for DesiredCapabilities. Name could be firefox, chrome and iexplore.
  3. 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);

            
  4. 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);
        
       //Invoke the RemoteWebdriver
        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;

public class YahooTitle {
    // Expected Title
    private String expectedTittle = "Yahoo";

    @Test
    public void verifyTitleTest() throws MalformedURLException {

        DesiredCapabilities cap = DesiredCapabilities.firefox();
        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/");

        // Get the actual title using selenium code
        String actualTittle = driver.getTitle();

        // Verify the page title using by comparison of Actual and Expected
        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- 

  1. To execute the Test using Selenium Grid, First invoke the Selenium Grid Hub.
  2. After invoke the Hub, Invoke the Selenium Grid Node.
  3. 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>
  4. 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

Popular posts from this blog

Reporting in Selenium

How do you Handle reporting in your project ? Method 1 : Maven - Testng - Surefire report The project will be created in Maven with  SureFire Plugin  and  SuiteXmlFile  tag in the POM.xml . The testng.xml will be called in the POM.            <plugin>                 <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-surefire-plugin</artifactId>            <version>2.18.1</version>                 <configuration>                     <suiteXmlFiles>                         <suiteXmlFile>                             ...

Handling Modal Dialog Window in Selenium Webdriver

Handling Modal Dialog Window in Selenium Webdriver In this tutorial we are going to see how to handle " Modal Dialog Window "  using Selenium Webdriver Disadvantage using Selenium Use Can't handle elements which is inside  Modal Dialog Window. Using Robot Class Using Robot Class we can handle  Modal Dialog Window. In this tutorial Open  http://vodkabears.github.io/remodal/# Click on  Show  button In the  Modal Dialog Window.  Click on Follow button  Source Code : import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; public class robot_demo { public static void main(String[] args) throws InterruptedException, AWTException { WebDrive...

Autonium

About Autonium Autonium is a test automation framework for web applications. This framework is built by using Java and Selenium. It uses Maven project structure. Easy to understand and use it. Having basic idea on Java, Selenium, Maven and TestNG is good enough to start with Autonium. Let’s start Automation with Autonium --------------------------------------------------- Advantages Provides support to all major browsers –  Firefox, Chrome, IE and Safari . Capability of executing scripts on  Remote Machines  by using Selenium Server. Users can make use of Selenium Web driver methods and Autonium methods. Autonium simplifies  Test data management . Test data can be read from properties files. Scripts can be developed without  hard coding  test data . Identifiers can also be read from properties files and  hard coding   of identifiers can be avoided. Test data and Identifier v...