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

API Testing With Selenium WebDriver

REST API Testing Framework We will be creating a simple Rest Testing Framework in Java and JUnit that could be used for any testing scenarios. Rest Testing Framework Overview The framework should be able to execute the basic REST operations (GET, POST, PUT, PATCH, DELETE) and perform the validations on the code, message, headers and body of the response. The completed code can be accessed from my  GitHub account   from where you can collect and make modifications based on your requirements. Design We will be having three classes for the framework (in package com.axatrikx.controller ) RestExecutor  : Performs the HTTP operations using Apache Http Client RestResponse  : A javabean class to hold our response values (code, message, headers, body) RestValidator  : Validates the response with the expected values The package  com.axatrikx.test  holds the test scripts. Note: I will be using ‘ json-server ‘ to fake the REST API. Its a real handy tool to r

ExtentReports in Selenium Webdriver

ExtentReports in Selenium Webdriver What is ExtentReport? ExtentReports  is a HTML reporting library for Selenium WebDriver for Java which is extremely easy to use and creates beautiful execution reports. It shows test and step summary, test steps and status in a toggle view for quick analysis Download Download the jar below: Download ExtentReports 1.4 (1623)    Snapshot of Extent report After Executing the Script   Program Steps:  We are going to write three different testcases. Pass Warning Fail TestCase with Pass Result Navigate to http://www.guvi.in Click on Sign-in Enter the credientials Check the URL is correct or not after login   TestCase with Warning Result Verify with the Wrong URL (static String Afterloginfail="http://www.guvi.in/ ")    TestCase with fail Result Click on Menu Select Tech Challenges Verify With wrong URL. Source Code: import  java.io.File; import  java.io.IOException; import   java.sql

How to Compare Two Images in Selenium

How to Compare Two Images in Selenium Some time in testing we have to do image comparision as verification point. So, in this blog I expalin how to compare two images in selenium. I have created one function for compare two images. you can use directly into your framework. first of all you have to import below packages into your code. import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; Now, you can use below function for comparison of two images. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public boolean mCompareImages ( BufferedImage img1 , BufferedImage img2 ) { boolean bCompareImage = true ; try { if ( img1 . getWidth () == img2 . getWidth () && img1 . getHeight () == img2 . getHeight ()) { for ( int i = 0 ; i < img1 . getWidth (); i ++) { for ( int j = 0 ; j < img1 . getHeight (); j ++) { if ( img1 . get