Thursday, September 19, 2019

Post#73.Selenium Grid


Selenium Grid

Selenium Grid is a part of the Selenium Suite that specializes on running multiple tests across different browsers, operating systems, and machines in parallel.

When to Use Selenium Grid

  • ·       Run your tests against different browsers, operating systems, and machines all at the same time
  • ·       Save time in the execution of your test suites

The Hub

·       The hub is the central point where you load your tests into.
·       There should only be one hub in a grid.
·       The hub is launched only on a single machine, say, a computer whose O.S is Windows 7 and whose browser is IE.
·       The machine containing the hub is where the tests will be run, but you will see the browser being automated on the node.

The Nodes

·       Nodes are the Selenium instances that will execute the tests that you loaded on the hub.
·       There can be one or more nodes in a grid.
·       Nodes can be launched on multiple machines with different platforms and browsers.
·       The machines running the nodes need not be the same platform as that of the hub.

Configure Grid

Configuring the Hub
Step-1
Download the selenium-server-standalone jar file
Step 2 − Start the Hub by launching the Selenium Server using the following command.
java -jar selenium-server-standalone-3.14.0.jar -role hub
The hub should successfully launch.



Step 3 − Now open the browser and navigate to the URL http//localhost:4444 from the Hub


Step 4 − Now click on the 'console' link and click 'view config'. The config of the hub would be displayed as follows.



Configuring the Nodes
Node1: Chrome
java -Dwebdriver.chrome.driver=D:\BrowserDrivers\chromedriver.exe -jar selenium-server-standalone-3.14.0.jar -role node -hub http://192.168.0.102:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5557
Node-2 IE
java -Dwebdriver.ie.driver=D:\BrowserDrivers\IEDriverServer.exe -jar selenium-server-standalone-3.14.0.jar -role node -hub http://192.168.0.102:4444/grid/register -browser browserName=ie,platform=WINDOWS -port 6667
Node-3 Firefox
java -Dwebdriver.gecko.driver=D:\BrowserDrivers\geckodriver.exe -jar selenium-server-standalone-3.14.0.jar -role node -hub http://192.168.0.102:4444/grid/register -browser browserName=firefox,platform=WINDOWS -port 3337
That’s all. Node registration is done.
Refresh the browser

Designing Test Scripts and prepare the XML file

To design test scripts that will run on the grid, we need to use DesiredCapabilites and the RemoteWebDriver objects.
·       DesiredCapabilites is used to set the type of browser and OS that we will automate
·       RemoteWebDriver is used to set which node (or machine) that our test will run against.
Go to the Grid's web interface and hover on an image of the browser that you want to automate. Take note of the platform and the browserName 

-Now set the type of browser and OS that we will automate through DesiredCapabilities class

DesiredCapabilities capability = DesiredCapabilities.firefox();
                capability.setBrowserName("firefox");
                capability.setPlatform(Platform.VISTA);

- Then Set which node (or machine) that our test will run against through RemoteWebDriver Class


WebDriver driver = new RemoteWebDriver(new URL(nodeUrl),capability);


Test Script:
package pack2;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;

public class TestNGClass {
   public WebDriver driver;
   public String URL, Node;
   protected ThreadLocal<RemoteWebDriver> threadDriver = null;
  
   @Parameters("browser")
   @BeforeTest
   public void launchapp(String browser) throws MalformedURLException {
      String URL = "http://www.calculator.net";
     
      if (browser.equalsIgnoreCase("firefox")) {
         System.out.println(" Executing on FireFox");
         String Node = "http://192.168.0.102:3337/wd/hub";
         DesiredCapabilities cap = DesiredCapabilities.firefox();
         cap.setBrowserName("firefox");
        
         driver = new RemoteWebDriver(new URL(Node), cap);
         // Puts an Implicit wait, Will wait for 10 seconds before throwing exception
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
         // Launch website
         driver.navigate().to(URL);
         driver.manage().window().maximize();
      } else if (browser.equalsIgnoreCase("chrome")) {
         System.out.println(" Executing on CHROME");
         DesiredCapabilities cap = DesiredCapabilities.chrome();
         cap.setBrowserName("chrome");
         String Node = "http://192.168.0.102:5557/wd/hub";
         driver = new RemoteWebDriver(new URL(Node), cap);
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
         // Launch website
         driver.navigate().to(URL);
         driver.manage().window().maximize();
      } else if (browser.equalsIgnoreCase("ie")) {
         System.out.println(" Executing on IE");
         DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
         cap.setBrowserName("internet explorer");
         String Node = "http://192.168.0.102:6667/wd/hub";
         driver = new RemoteWebDriver(new URL(Node), cap);
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
         // Launch website
         driver.navigate().to(URL);
         driver.manage().window().maximize();
      } else {
         throw new IllegalArgumentException("The Browser Type is Undefined");
      }
   }
  
   @Test
   public void calculatepercent() {
      // Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();      
     
      // Click on Percent Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click();
     
      // Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");
     
      // Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");
     
      // Click Calculate Button
      // driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr/td[2]/input")).click();
      // Get the Result Text based on its xpath
      String result =
         driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/span/font/b")).getText();
     
      // Print a Log In message to the screen
      System.out.println(" The Result is " + result);
     
      if(result.equals("5")) {
         System.out.println(" The Result is Pass");
      } else {
         System.out.println(" The Result is Fail");
      }
   }
  
   @AfterTest
   public void closeBrowser() {
      driver.quit();
   }
}

The Browser parameter will be passed using XML. Create an XML under the project folder as below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Suite" parallel = "tests">

   <test name = "FirefoxTest">
   <parameter name = "browser" value = "firefox" />
      <classes>
         <class name = "pack2.TestNGClass" />
      </classes>
   </test>

   <test name = "ChromeTest">
   <parameter name = "browser" value = "chrome" />
      <classes>
         <class name = "pack2.TestNGClass" />
      </classes>
   </test>

   <test name = "IETest">
   <parameter name = "browser" value = "ie" />
      <classes>
         <class name = "pack2.TestNGClass" />
      </classes>
   </test>
  
</suite>

You are done with all configurations. Now execute your tests and analyze the reports.

1 comment: