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.

Webtable data fetch and verification selenium webdriver-java

//sample html content

<table name="t1" border="5">
<thead>
<tr>
<th>ID</th><th>Name</th><th>Dept</th>
</tr>
</thead>
<tbody>
<tr>
<td>123</td><td>Mohan</td><td>Admin</td>
</tr>
<tr>
<td>432</td><td>Sohan</td><td>Hr</td>
</tr>
<tr>
<td>543</td><td>Hanuman</td><td>Finance</td>
</tr>
<tr>
<td>765</td><td>Dinesh</td><td>It</td>
</tr>
</tbody>
</table>




//codes to fetch name and dept based on ID no

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class WebTable {
public static WebDriver driver;

  @Test
  public void test1() {
  driver=new FirefoxDriver();
  driver.get("file:///E:/Selenium/docs/sample.html");
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  List row=driver.findElements(By.xpath("//table[@name='t1']/tbody/tr"));
  System.out.println(row.size());
 
  List col=driver.findElements(By.xpath("//table[@name='t1']/tbody/tr[1]/td"));
  System.out.println(col.size());
  for(int i=1;i<=row.size();i++)
  {
  for(int j=1;j<=col.size();j++)
  {
String sData= driver.findElement(By.xpath("//table[@name='t1']/tbody/tr["+i+"]/td["+j+"]")).getText();

System.out.println(sData);
if(sData.equals("543"))
{
System.out.println(driver.findElement(By.xpath("//table[@name='t1']/tbody/tr["+i+"]/td["+j+1+"]")).getText());
System.out.println(driver.findElement(By.xpath("//table[@name='t1']/tbody/tr["+i+"]/td["+j+2+"]")).getText());
}
  }
  }
 
  }
}

Sikuli integration with Selenium webdriver-Java for upload functionality

Steps:

1. Capture 3 snapshot(Try it,Browse,Open) for below url using sikuli ide or snipping tool or  copy paste below images  in mentioned folder

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_create





2. Create a file test.txt in your default window folder (for me - desktop)

3. Add Sikulix jar and selenium jar in your project build path
4. create a testng class - SikuliProj in your project and run the test.

//Codes


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.testng.annotations.Test;

public class SikuliProj {

@Test
public void functionName() throws FindFailed, InterruptedException {


// Create a new instance of the Firefox driver

WebDriver driver = new FirefoxDriver();

//navigate to websie
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_create");

Screen sc=new Screen();

//Create object for all 3 captured images
Pattern image1 = new Pattern("E:\\test\\tryit.png");
Pattern image2 = new Pattern("E:\\test\\browse.png");
Pattern image3 = new Pattern("E:\\test\\open.png");

//Perform action using sikuli
sc.wait(image1,10);
sc.click(image1);
sc.wait(image2,5);
sc.click(image2);
Thread.sleep(5000);
sc.type("test.txt");
sc.wait(image3,5);
sc.click(image3);

  }

}