-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4917 from rhwood/pickles
Prevent potential NPE in Cucumber/Selenium tests
- Loading branch information
Showing
3 changed files
with
112 additions
and
108 deletions.
There are no files selected for viewing
137 changes: 67 additions & 70 deletions
137
java/acceptancetest/step_definitions/jmri/web/WebServerAcceptanceSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
|
||
} | ||
} | ||
} |
81 changes: 44 additions & 37 deletions
81
java/acceptancetest/step_definitions/jmri/web/WebServerScaffold.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters