Hi Friends,
Very first time I have written selenium test using selenium 3.0.1, java maven project, and geckodriver. It's just about to search on Google
geckodriver version : geckodriver-v0.11.1-linux64.tar.gz
package com.continuous.integration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class TestNGSeleniumSimpleSearchExample {
private WebDriver driver;
@BeforeSuite
public void initDriver() throws Exception {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
System.out.println("You are testing in firefox");
if(System.getProperty("os.name").contains("Windows")){
System.out.println("You are testing in window machine");
System.setProperty("webdriver.gecko.driver","C:\\FirFoxDriver\\geckodriver.exe");
}else if (System.getProperty("os.name").contains("Linux")){
System.out.println("You are testing in linux machine");
System.setProperty("webdriver.gecko.driver","/home/amit/lib/geckodriver");
}
driver = new FirefoxDriver(capabilities);
}
@Test
public void searchTestNGInGoogle() {
final String searchKey = "Miracle Software Systems";
System.out.println("Search " + searchKey + " in google");
driver.navigate().to("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
System.out.println("Enter " + searchKey);
element.sendKeys(searchKey);
System.out.println("submit");
element.submit();
(new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase()
.startsWith(searchKey.toLowerCase());
}
});
System.out.println("Got " + searchKey + " results");
}
@AfterSuite
public void quitDriver() throws Exception {
driver.quit();
}
}
Above code was running successfully in window machine but getting error when Jenkins download the code from git-hub for build in ubuntu machine. selenium test case are failing during build and Firefox is not opening to execute test case. Getting the following error
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
You are testing in firefox
You are testing in linux machine
1481838007110 geckodriver INFO Listening on 127.0.0.1:23688
Dec 15, 2016 4:40:07 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
1481838007421 mozprofile::profile INFO Using profile path /tmp/rust_mozprofile.FxYAfbEMlLVM
1481838007429 geckodriver::marionette INFO Starting browser /usr/bin/firefox
1481838007436 geckodriver::marionette INFO Connecting to Marionette on localhost:38660
Error: GDK_BACKEND does not match available displays
Dec 15, 2016 4:41:08 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to original OSS JSON Wire Protocol.
1481838068100 mozprofile::profile INFO Using profile path /tmp/rust_mozprofile.x1wSNsGEUPLN
1481838068119 geckodriver::marionette INFO Starting browser /usr/bin/firefox
1481838068126 geckodriver::marionette INFO Connecting to Marionette on localhost:37894
Error: GDK_BACKEND does not match available displays
Dec 15, 2016 4:42:08 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to straight W3C remote end connection
1481838128770 mozprofile::profile INFO Using profile path /tmp/rust_mozprofile.0ncCoCZy6JRT
1481838128771 geckodriver::marionette INFO Starting browser /usr/bin/firefox
1481838128815 geckodriver::marionette INFO Connecting to Marionette on localhost:37893
Error: GDK_BACKEND does not match available displays
Tests run: 7, Failures: 1, Errors: 0, Skipped: 2, Time elapsed: 184.28 sec <<< FAILURE! - in TestSuite
initDriver(com.continuous.integration.TestNGSeleniumSimpleSearchExample) Time elapsed: 183.988 sec <<< FAILURE!
org.openqa.selenium.SessionNotCreatedException:
Unable to create new remote session. desired capabilities = Capabilities [{marionette=true, firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@76508ed1, browserName=firefox, moz:firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@76508ed1, version=, platform=ANY}], required capabilities = Capabilities [{}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'amit-virtual-machine', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.2.0-27-generic', java.version: '1.8.0_112'
Driver info: driver.version: FirefoxDriver
at com.continuous.integration.TestNGSeleniumSimpleSearchExample.initDriver(TestNGSeleniumSimpleSearchExample.java:30)
Results :
Failed tests:
TestNGSeleniumSimpleSearchExample.initDriver:30 » SessionNotCreated Unable to ...
Tests run: 7, Failures: 1, Errors: 0, Skipped: 2
[ERROR] There are test failures.
Please refer to /var/lib/jenkins/workspace/JavaProject/target/surefire-reports for the individual test results.
[JENKINS] Recording test results
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ ContinuousIntegration ---
[INFO] Building jar: /var/lib/jenkins/workspace/JavaProject/target/ContinuousIntegration-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ ContinuousIntegration ---
[INFO] Installing /var/lib/jenkins/workspace/JavaProject/target/ContinuousIntegration-0.0.1-SNAPSHOT.jar to /var/lib/jenkins/.m2/repository/com/continuous/integration/ContinuousIntegration/0.0.1-SNAPSHOT/ContinuousIntegration-0.0.1-SNAPSHOT.jar
[INFO] Installing /var/lib/jenkins/workspace/JavaProject/pom.xml to /var/lib/jenkins/.m2/repository/com/continuous/integration/ContinuousIntegration/0.0.1-SNAPSHOT/ContinuousIntegration-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:16 min
[INFO] Finished at: 2016-12-15T16:43:14-05:00
[INFO] Final Memory: 25M/59M
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /var/lib/jenkins/workspace/JavaPro
Kindly Help
Regards
Amit