Showing posts with label selenium grid using java. Show all posts
Showing posts with label selenium grid using java. Show all posts

Saturday, 18 November 2017

Selenium grid parallel run with different browser in distributed system.


pre requisites: jdk,  eclipse with testng.


1. Download selenium server standalone jar
2. open command prompt and cd to the folder - where you have "selenium server standalone jar" available
ex:
cd E:\Selenium

3. start the hub from command prompt:
java -jar selenium-server-standalone.jar -role hub

4. run two nodes in diff vm or diff system (Replace localhost with hub system IP)
java -jar selenium-server-standalone.jar -role node -hub http://localhost:4444

5.verify your grid setup in below url.
http://localhost:4444/grid/console

5. Create one java project and convert into testng project.add selenium jar to buildpath

6. Update your testng.xml file with below code

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

  <test name="Democart">
    <classes>
       <class name="testng.GridParreleltests"/>
    </classes>
  </test>
</suite>

7. Crate one testng class(GridParreleltests) under grid package and write below codes.     note:- replace localhost with hub system IP.

package grid;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

public class GridParreleltests {

public static WebDriver driver;

@Test
  public void test1() throws MalformedURLException {

String hubUrl="http://localhost:4444/wd/hub";
DesiredCapabilities cap=  DesiredCapabilities.firefox();
  //cap.setPlatform(Platform.WINDOWS);
// cap.setVersion);
  cap.setBrowserName("firefox");
  System.out.println("before launch");
  driver =new RemoteWebDriver(new URL(hubUrl),cap);
  driver.get("http://www.google.com");
  System.out.println("after launch");

  }

@Test
public void test2() throws MalformedURLException {
String hubUrl="http://localhost:4444/wd/hub";
DesiredCapabilities cap=  DesiredCapabilities.chrome();
  //cap.setPlatform(Platform.WINDOWS);
// cap.setVersion);
  cap.setBrowserName("chrome");
  System.out.println("before launch");
  driver =new RemoteWebDriver(new URL(hubUrl),cap);
  driver.get("http://www.yahoo.com");
  System.out.println("after launch");
}
}

8. Right click on testng.xml and run as testng suite.