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

Testing Types

         Installation testing Main article: Installation testing An installation test assures that the system is installed correctly and working at actual customer's hardware. Compatibility testing Main article: Compatibility testing A common cause of software failure (real or perceived) is a lack of its compatibility with other application software, operating systems (or operating system versions, old or new), or target environments that differ greatly from the original (such as a terminal or GUI application intended to be run on the desktop now being required to become a web application, which must render in a web browser). For example, in the case of a lack of backward compatibility, this can occur because the programmers develop and test software only on the latest version of the target environment, which not all users may be running. This results in the unintended cons...

Clickjacking

Clickjacking Clickjacking  ( User Interface redress attack ,  UI redress attack ,  UI redressing ) is a  malicious technique  of tricking a  Web   user  into clicking on something different from what the user perceives they are clicking on, thus potentially revealing  confidential  information or taking control of their computer while clicking on seemingly innocuous  web pages . It is a  browser security  issue that is a  vulnerability  across a variety of  browsers  and  platforms . A clickjack takes the form of embedded  code  or a script that can execute without the user's knowledge, such as clicking on a  button  that appears to perform another function. [5]  The term "clickjacking" was coined by Jeremiah Grossman and Robert Hansen in 2008. [ citation needed ]  Clickjacking can be understood as an instance of the  confused deputy problem . [6] ...

Autoit Setup for Selenium Webdriver

Autoit Setup for Selenium Webdriver WHAT IS AUTOIT AutoIt  v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated  keystrokes ,  mouse movement  and window/control  manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required! In layman’s   term AutoIt is just another automation tool like Selenium but unlike Selenium it is used for Desktop Automation rather Web Automation.  It is a powerful tool and it just not automate desktop windows, button & form, it automates mouse movements & keystrokes too. Just like Selenium IDE, it also gives you the recording capability which generates the scripts for you to use the same script in you test. AUTOIT F...