Monday, April 11, 2016

Executing Selenium Test Case using JAVA code from Jenkin using PhantomJS

1- First download required exe for winddow from below link

2- Create JAVA Project as shown in the image and Execute folowing code

i.e. Right Click -->Rus As ->Junit

import static;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class MyWebDriver {

private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();

public void setUp() throws Exception {

Capabilities caps = new DesiredCapabilities();
        ((DesiredCapabilities) caps).setJavascriptEnabled(true);
        ((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
        ((DesiredCapabilities) caps).setCapability(
        driver = new  PhantomJSDriver(caps);
baseUrl = "";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

public void testGoogleSearch() throws Exception {
//Note: I was not able to execute below code as it did not find"lst-ib")
/*driver.get(baseUrl + "/?gws_rd=ssl");

public void tearDown() throws Exception {
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {

private boolean isElementPresent(By by) {
try {
return true;
} catch (NoSuchElementException e) {
return false;

private boolean isAlertPresent() {
try {
return true;
} catch (NoAlertPresentException e) {
return false;

private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
} else {
return alertText;
} finally {
acceptNextAlert = true;


No comments: