Skip to content

Commit

Permalink
Merge pull request #4917 from rhwood/pickles
Browse files Browse the repository at this point in the history
Prevent potential NPE in Cucumber/Selenium tests
  • Loading branch information
rhwood committed Feb 19, 2018
2 parents b5fefe3 + 163deb5 commit 0e69ce7
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,94 +1,91 @@
package jmri.web;

import cucumber.api.java8.En;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.awt.GraphicsEnvironment;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.awt.GraphicsEnvironment;
import java.util.concurrent.TimeUnit;

/**
* Cucumber step defintions for Web Server Acceptance tests.
* Cucumber step definitions for Web Server Acceptance tests.
*
* @author Paul Bender Copyright (C) 2017
* @author Paul Bender Copyright (C) 2017
*/
public class WebServerAcceptanceSteps implements En {

private EventFiringWebDriver webDriver;

String[] firefoxtags = {"@webtest","@firefox"};
String[] chrometags = {"@webtest","@chrome"};
String[] tags = {"@webtest"};

public WebServerAcceptanceSteps(jmri.InstanceManager instance) {

Before(chrometags,()->{
private EventFiringWebDriver webDriver;

String[] firefoxtags = {"@webtest", "@firefox"};
String[] chrometags = {"@webtest", "@chrome"};
String[] tags = {"@webtest"};

public WebServerAcceptanceSteps(jmri.InstanceManager instance) {

Before(chrometags, () -> {
WebDriverManager.getInstance(ChromeDriver.class).setup();
});
Before(firefoxtags,()->{
});
Before(firefoxtags, () -> {
WebDriverManager.getInstance(FirefoxDriver.class).setup();
});
Given("^I am using firefox$", () -> {
if(GraphicsEnvironment.isHeadless()) {
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
webDriver = new EventFiringWebDriver(new FirefoxDriver(firefoxOptions));
} else {
webDriver = new EventFiringWebDriver(new FirefoxDriver());
}
webDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
});
});
Given("^I am using firefox$", () -> {
if (GraphicsEnvironment.isHeadless()) {
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
webDriver = new EventFiringWebDriver(new FirefoxDriver(firefoxOptions));
} else {
webDriver = new EventFiringWebDriver(new FirefoxDriver());
}
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
});

Given("^I am using chrome$", () -> {
if(GraphicsEnvironment.isHeadless()) {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
webDriver = new EventFiringWebDriver(new ChromeDriver(chromeOptions));
} else {
webDriver = new EventFiringWebDriver(new ChromeDriver());
}
webDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
});
Given("^I am using chrome$", () -> {
if (GraphicsEnvironment.isHeadless()) {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
webDriver = new EventFiringWebDriver(new ChromeDriver(chromeOptions));
} else {
webDriver = new EventFiringWebDriver(new ChromeDriver());
}
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
});

When("^I ask for the url (.*)$", (String url) -> {
webDriver.get(url);
});
When("^I ask for the url (.*)$", (String url) -> {
webDriver.get(url);
});

Then("^a page with title (.*) is returned$", (String pageTitle) -> {
WebDriverWait wait = new WebDriverWait(webDriver,10);
wait.until(new ExpectedCondition<Boolean>() {
// this ExpectedCondition code is derived from code posted by user
// Jeff Vincent to
// https://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds-in-java
@Override
public Boolean apply(WebDriver driver) {
String script = "if (typeof window != 'undefined' && window.document) { return window.document.readyState; } else { return 'notready'; }";
Boolean result;
try {
result = ((JavascriptExecutor) driver).executeScript(script).equals("complete");
} catch (Exception ex) {
result = Boolean.FALSE;
}
return result;
}
Then("^a page with title (.*) is returned$", (String pageTitle) -> {
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until((WebDriver driver) -> {
// Derived from code by Jeff Vincent at
// https://stackoverflow.com/a/26812386/176160
String script = "if (typeof window != 'undefined' && window.document) { return window.document.readyState; } else { return 'notready'; }";
Boolean result;
try {
result = ((JavascriptExecutor) driver).executeScript(script).equals("complete");
} catch (Exception ex) {
result = Boolean.FALSE;
}
return result;
});
Assert.assertEquals("Page Title", pageTitle, webDriver.getTitle());
});
Assert.assertEquals("Page Title",pageTitle,webDriver.getTitle());
});

After(tags,()->{
webDriver.close();
});
After(tags, () -> {
if (webDriver != null) {
webDriver.close();
}
});

}
}
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
package jmri.web;

import cucumber.api.java8.En;
import org.junit.Assert;

import jmri.web.server.WebServer;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Cucumber helper to handle starting and stoping the web server
* during web tests.
* Cucumber helper to handle starting and stopping the web server during web
* tests.
*
* @author Paul Bender Copyright (C) 2017
* @author Paul Bender Copyright (C) 2017
*/
public class WebServerScaffold implements En {

private WebServer server = null;
String[] tags = {"@webtest"};
private WebServer server = null;
String[] tags = {"@webtest"};

private final static Logger log = LoggerFactory.getLogger(WebServerScaffold.class);

public WebServerScaffold(jmri.InstanceManager instance) {
public WebServerScaffold(jmri.InstanceManager instance) {

Before(tags,()->{
jmri.util.JUnitUtil.resetProfileManager();
jmri.util.JUnitUtil.initShutDownManager();
jmri.util.JUnitUtil.initDebugPowerManager();
server = new WebServer(); // a webserver using default preferences.
server.start();
jmri.util.JUnitUtil.waitFor(()->{ return server.isStarted(); },"Server Failed to Start in time");
jmri.util.JUnitOperationsUtil.resetOperationsManager();
});
Before(tags, () -> {
jmri.util.JUnitUtil.resetProfileManager();
jmri.util.JUnitUtil.initShutDownManager();
jmri.util.JUnitUtil.initDebugPowerManager();
server = new WebServer(); // a webserver using default preferences.
server.start();
jmri.util.JUnitUtil.waitFor(() -> {
return server.isStarted();
}, "Server Failed to Start in time");
jmri.util.JUnitOperationsUtil.resetOperationsManager();
});

After(tags, ()->{
try {
try {
server.stop();
jmri.util.JUnitUtil.waitFor(()->{ return server.isStopped(); },"Server failed to Stop in time");
} catch(java.lang.NullPointerException npe) {
//npe.printStackTrace();
//Assert.fail("Null Pointer Exception while stopping web server:" + npe);
} catch(Exception ex) {
// Exception is thrown by the stop call above.
// if an Exception occurs here, we may want to raise a flag,
ex.printStackTrace();
Assert.fail("Exception occured during web server shutdown:" + ex);
}
} catch(java.lang.NullPointerException npe2) {
//npe2.printStackTrace();
//Assert.fail("Null Pointer Exception occured during teardown:" + npe2);
}
});
}
After(tags, () -> {
try {
try {
server.stop();
jmri.util.JUnitUtil.waitFor(() -> {
return server.isStopped();
}, "Server failed to Stop in time");
} catch (java.lang.NullPointerException npe) {
log.debug("NPE shutting down web server", npe);
//Assert.fail("Null Pointer Exception while stopping web server:" + npe);
} catch (Exception ex) {
// Exception is thrown by the stop call above.
// if an Exception occurs here, we may want to raise a flag,
log.error("Excecption shutting down web server", ex);
Assert.fail("Exception occured during web server shutdown:" + ex);
}
} catch (java.lang.NullPointerException npe2) {
log.debug("NPE shutting down web server", npe2);
//Assert.fail("Null Pointer Exception occured during teardown:" + npe2);
}
});
}
}
2 changes: 1 addition & 1 deletion nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
<compilation-unit>
<package-root>java/acceptancetest/step_definitions</package-root>
<unit-tests/>
<classpath mode="compile">target/classes:lib/webdrivermanager-2.0.1.jar:lib/junit-4.12.jar:lib/cucumber-java8-2.0.1.jar:lib/selenium-server-standalone-3.6.0.jar</classpath>
<classpath mode="compile">target/classes:lib/webdrivermanager-2.0.1.jar:lib/junit-4.12.jar:lib/cucumber-java8-2.0.1.jar:lib/selenium-server-standalone-3.6.0.jar:lib/slf4j-api-1.7.25.jar</classpath>
<source-level>1.8</source-level>
</compilation-unit>
</java-data>
Expand Down

0 comments on commit 0e69ce7

Please sign in to comment.