How to integrate Sikuli script with Selenium WebDriver (WebDriver and Sikuli Integration)
Sikuli is a robust and powerful tool to automate and tests user interfaces screenshots. The core of Sikuli Script is written in Java, which means you can use Sikuli Script as a standard JAVA library in your program.
Include sikuli-script.jar, the same way selenium-server-standalone-2.25.0.jar, selenium-java-2.25.0.jar is included, in the CLASSPATH of your Java project.

Get sikuli-script.jar from your Sikuli IDE installation path.
Sikuli Script is packed in a JAR file - sikuli-script.jar.
- Sikuli-IDE/sikuli-script.jar
package com.demo;
import org.openqa.selenium.By;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.util.Date;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.sikuli.script.App;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.SeleneseTestBase;
public class TestBaseSikuli extends SeleneseTestBase{
public static WebDriver driver;
Screen screen = new Screen(); //Create and initialise an instance of Screen object
//public static String URL_var;
public boolean isImagePresent(Pattern pattern){
boolean status = false;
screen = new Screen();
try {
screen.find(pattern);
status = true;
} catch (FindFailed e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return status;
}
@BeforeTest()
public void setupBeforeTest() throws Exception{
driver = new FirefoxDriver();
System.out.println("Driver started " +driver);
String URL = "http://www.yatra.com/";
System.out.println("GetURL " +URL);
long start = System.currentTimeMillis(); //locate element load time
driver.get(URL);
driver.manage().window().maximize();
long finish = System.currentTimeMillis();
long TotalTime = finish - start;
System.out.println("Total Time for page load - "+TotalTime);
}
@Test()
public void BusBooking() throws FindFailed {
//Add image path
Pattern Header = new Pattern("C:\\Screenshots\\HeaderHome.png");
screen.wait(Header, 10); //Wait 10s for image
if (screen.exists(Header) != null) //Check the image exits
System.out.println("Element exists" +Header);
else
System.out.println("Element" +Header+ "doesnot exists");
//Add image path
Pattern PlanTable = new Pattern("C:\\Screenshots\\PlanTable.png");
if (screen.exists(PlanTable) != null) //Check the image exits
System.out.println("Element exists" +PlanTable);
else
System.out.println("Element" +PlanTable+ "doesnot exists");
//Add image path
Pattern Footer = new Pattern("C:\\Screenshots\\Footer.png");
if (screen.exists(Footer) != null); //Check the image exits
System.out.println("Element exists" +Footer);
//Add image path
Pattern Signin = new Pattern("C:\\Screenshots\\SignIn.png");
if (screen.exists(Signin) != null); //Check the image exits
System.out.println("Element exists" +Signin);
Util.WaitForElementPresentByXpath(driver, ".//*[@id='menu_bus']/a");
driver.findElement(By.xpath(".//*[@id='menu_bus']/a")).click(); //Click on bus menu
Pattern PlanTravel = new Pattern("C:\\Screenshots\\Travel.png");
screen.wait(PlanTravel, 10); //wait for GUI image
if (screen.exists(PlanTravel) != null); //Check the image exits
System.out.println("Element exists" +PlanTravel);
//Filling the bus details
driver.findElement(By.xpath(".//*[@id='DDLSource']")).clear();
driver.findElement(By.xpath(".//*[@id='DDLSource']")).sendKeys("Mumbai");
driver.findElement(By.xpath(".//*[@id='DDLDestination']")).clear();
driver.findElement(By.xpath(".//*[@id='DDLDestination']")).sendKeys("Goa");
driver.findElement(By.id("datePickerDepart_dom1")).click();
driver.findElement(By.linkText(String.valueOf(Util.GetDay()))).click();
driver.findElement(By.id("searchBtn")).click();
/*Pattern BusResultTable = new Pattern("C:\\Screenshots\\BusResultTable.png");
screen.wait(BusResultTable);
if (screen.exists(BusResultTable) != null)
System.out.println("Element Exists" +BusResultTable);*/
Pattern BusResultHeader = new Pattern("C:\\Screenshots\\BusResultHeader.png"); //Add image
if (screen.exists(BusResultHeader) != null) //Check the image exits
System.out.println("Element Exists" +BusResultHeader);
Util.WaitForElementPresentByXpath(driver, "//*[@id='modify']/ul[1]/li[3]/div/span");
driver.findElement(By.xpath("//*[@id='modify']/ul[1]/li[3]/div/span")).click(); //Click on Modify button
System.out.println("Modify clicked");
}
@Test()
public void FlightBooking() throws FindFailed, InterruptedException {
driver.findElement(By.xpath("//*[@id='menu_flights']/a")).click(); //Click on flights tab
//Add image path
Pattern FlightTable = new Pattern("C:\\Screenshots\\PlanTable.png");
if (screen.exists(FlightTable) != null) //Check the image exits
System.out.println("Element exists" +FlightTable);
else
System.out.println("Element" +FlightTable+ "doesnot exists");
//Filling Flight details
driver.findElement(By.xpath("//*[@id='BE_flight_form']/div/div[1]/span[2]/a")).click();
driver.findElement(By.id("BE_flight_origin_city")).clear();
driver.findElement(By.id("BE_flight_origin_city")).sendKeys("Mumbai");
String OrgCity = driver.findElement(By.id("BE_flight_origin_city")).getText();
driver.findElement(By.id("BE_flight_origin_city")).equals(OrgCity);
System.out.println(OrgCity);
if (driver.findElement(By.id("BE_flight_origin_city")).equals(OrgCity))
driver.findElement(By.id("BE_flight_arrival_city")).clear();
driver.findElement(By.id("BE_flight_arrival_city")).sendKeys("Goa");
String ArrCity = driver.findElement(By.id("BE_flight_arrival_city")).getText();
driver.findElement(By.id("BE_flight_arrival_city")).equals(ArrCity);
System.out.println("Arriving city" +ArrCity);
if (driver.findElement(By.id("BE_flight_arrival_city")).equals(ArrCity))
Util.WaitForElementPresentByid(driver, "BE_flight_depart_date");
driver.findElement(By.id("BE_flight_depart_date")).click();
driver.findElement(By.linkText(String.valueOf(Util.GetDay()))).click();
driver.findElement(By.id("BE_flight_flsearch_btn")).click();
Util.WaitForElementPresentByid(driver, "quitQbReg");
driver.findElement(By.id("quitQbReg")).click();
//Check GUI Elements
Pattern ResultStep = new Pattern("C:\\Screenshots\\ResultStep.png");
if (screen.exists(ResultStep) != null) //Check the image exits
System.out.println("Element exists" +ResultStep);
else
System.out.println("Element" +ResultStep+ "doesnot exists");
Pattern FlightResultHeader = new Pattern("C:\\Screenshots\\FlightResultHeader.png");
if (screen.exists(FlightResultHeader) != null) //Check the image exits
System.out.println("Element exists" +FlightResultHeader);
else
System.out.println("Element" +FlightResultHeader+ "doesnot exists");
Pattern ResultsLeftPanel = new Pattern("C:\\Screenshots\\ResultsLeftPanel.png");
if (screen.exists(ResultsLeftPanel) != null) //Check the image exits
System.out.println("Element exists" +ResultsLeftPanel);
else
System.out.println("Element" +ResultsLeftPanel+ "doesnot exists");
driver.findElement(By.xpath("//*[@id='frescoHeader']/div/a")).click(); //Click on Logo
}
@Test()
public void FlightHotelBooking() throws Exception {
driver.findElement(By.xpath("//*[@id='menu_flights-hotels']/a")).click();
Pattern FlightHotel = new Pattern("C:\\Screenshots\\FlightHotel.png");
if(isImagePresent(FlightHotel)){
System.out.println("Element exists" +FlightHotel);
}
else{
System.out.println("Element" +FlightHotel+ "doesnot exists");
}
driver.findElement(By.xpath("//*[@id='frescoHeader']/div/a")).click();
}
@AfterTest()
public void kill() throws Exception{
driver.close(); //closing the driver and killing all open processes
driver.quit();
System.out.println("Browser Killed");
}
}
No comments:
Post a Comment