Skip to content

Commit

Permalink
Make ScalaWebTest compatible with all implementations of Selenium Web…
Browse files Browse the repository at this point in the history
…Driver #74

 - correctly handle JSON responses in Chrome (stripping the HTML wrapper)
 - added basic tests, to verify functionality across all browsers
 - moved comments from HtmlUnitConfiguration to BaseConfiguration
 - with this commit the change should be good enough to be merged back to develop
  • Loading branch information
DaniRey committed May 28, 2019
1 parent ce0b12e commit d074e27
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ trait SeleniumChrome {
class ChromeRemoteWebDriver(remoteAddress: URL , capabilities: Capabilities ) extends RemoteWebDriver(remoteAddress, capabilities) {
override def getPageSource: String = {
super.getPageSource
.replaceFirst("""<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">""", "")
.replaceFirst("""<html.*><head.*></head><body.*><pre style="word-wrap: break-word; white-space: pre-wrap;">""", "")
.replaceFirst("""</pre></body></html>""", "")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,38 @@ import org.scalawebtest.core.IntegrationSpec
abstract class BaseConfiguration() {
var configurations: Map[String, WebDriver => Unit] = Map()

/**
* Enable JavaScript evaluation in the webDriver
* and choose whether to throw on JavaScript error
*/
def enableJavaScript(throwOnError: Boolean): Unit

/**
* Disable JavaScript evaluation as well as throwing on JavaScript error in the webDriver
*/
def disableJavaScript(): Unit

/**
* Throw on JavaScript Error. Preferably use [[HtmlUnitConfiguration.disableJavaScript()]], as the two configurations only make sense when combined.
*/
def throwOnJavaScriptError(): Unit

/**
* Silently swallow JavaScript errors. Preferably use [[HtmlUnitConfiguration.disableJavaScript()]], as the two configurations only make sense when combined.
*/
def swallowJavaScriptErrors(): Unit

/**
* Enable CSS evaluation in the webDriver.
*/
def enableCss(): Unit

/**
* Disable CSS evaluation in the webDriver.
*/
def disableCss(): Unit

def unimplementedConfiguration(webDriverName: String): Unit = throw new RuntimeException(s"This is not configurable when working with $webDriverName. Please choose a different browser/webDriver.")
protected def unimplementedConfiguration(webDriverName: String): Unit = throw new RuntimeException(s"This is not configurable when working with $webDriverName. Please choose a different browser/webDriver.")
}

abstract class LoginConfiguration extends BaseConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,27 @@ trait HtmlUnitConfiguration {
swallowJavaScriptErrors()
disableCss()

/**
* Enable JavaScript evaluation in the webDriver
* and choose whether to throw on JavaScript error
*/
override def enableJavaScript(throwOnError: Boolean): Unit = {
configurations += "enableJavaScript" ->
((webDriver: WebDriver) => asWebClientExposingDriverOrError(webDriver).getOptions.setJavaScriptEnabled(true))
configurations += "throwOnJSError" ->
((webDriver: WebDriver) => asWebClientExposingDriverOrError(webDriver).getOptions.setThrowExceptionOnScriptError(throwOnError))
}

/**
* Disable JavaScript evaluation as well as throwing on JavaScript error in the webDriver
*/
override def disableJavaScript(): Unit = {
configurations += "enableJavaScript" -> ((webDriver: WebDriver) => asWebClientExposingDriverOrError(webDriver).getOptions.setJavaScriptEnabled(false))
configurations += "throwOnJSError" -> ((webDriver: WebDriver) => asWebClientExposingDriverOrError(webDriver).getOptions.setThrowExceptionOnScriptError(false))
}

/**
* Throw on JavaScript Error. Preferably use [[HtmlUnitConfiguration.disableJavaScript()]], as the two configurations only make sense when combined.
*/
override def throwOnJavaScriptError(): Unit = configurations += "throwOnJSError" ->
((webDriver: WebDriver) => asWebClientExposingDriverOrError(webDriver).getOptions.setThrowExceptionOnScriptError(true))

/**
* Silently swallow JavaScript errors. Preferably use [[HtmlUnitConfiguration.disableJavaScript()]], as the two configurations only make sense when combined.
*/
override def swallowJavaScriptErrors(): Unit = configurations += "throwOnJSError" ->
((webDriver: WebDriver) => asWebClientExposingDriverOrError(webDriver).getOptions.setThrowExceptionOnScriptError(false))

/**
* Enable CSS evaluation in the webDriver.
*/
override def enableCss(): Unit = configurations += "enableCss" ->
((webDriver: WebDriver) => asWebClientExposingDriverOrError(webDriver).getOptions.setCssEnabled(true))

/**
* Disable CSS evaluation in the webDriver.
*/
override def disableCss(): Unit = configurations += "enableCss" ->
((webDriver: WebDriver) => asWebClientExposingDriverOrError(webDriver).getOptions.setCssEnabled(false))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.scalawebtest.integration.browser

import org.scalawebtest.integration.ScalaWebTestBaseSpec
import org.scalawebtest.integration.browser.behaviors.BrowserBehaviors

class HtmlUnitSpec extends ScalaWebTestBaseSpec with BrowserBehaviors {
path = "/"

"HtmlUnit" should behave like aWebBrowserWithElementLookup
it should behave like anHtmlGauge
it should behave like aJsonGauge
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.scalawebtest.integration.browser

import org.scalawebtest.core.browser.SeleniumChrome
import org.scalawebtest.integration.ScalaWebTestBaseSpec
import org.scalawebtest.integration.browser.behaviors.BrowserBehaviors

class SeleniumChromeSpec extends ScalaWebTestBaseSpec with SeleniumChrome with BrowserBehaviors {
path = "/"

"SeleniumChrome" should behave like aWebBrowserWithElementLookup()
it should behave like anHtmlGauge
it should behave like aJsonGauge
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.scalawebtest.integration.browser.behaviors

import org.scalawebtest.integration.ScalaWebTestBaseSpec

trait BrowserBehaviors extends WebBrowserBehavior with HtmlGaugeBehavior with JsonGaugeBehavior {
self: ScalaWebTestBaseSpec =>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.scalawebtest.integration.browser.behaviors

import org.scalawebtest.integration.ScalaWebTestBaseSpec

trait HtmlGaugeBehavior {
self: ScalaWebTestBaseSpec =>

def anHtmlGauge(): Unit = {
it should "be capable to fit a simple HTML Gauge" in {
navigateTo("/elementsList.jsp")
fits(
<ul>
<li><div>Link 2</div></li>
</ul>)
}
it should "be capable to fit an HTML Gauge containing a list of items" in {
fits(
<ul>
<li><div>Link 1</div></li>
<li><div>Link 2</div></li>
<li><div>Link 3</div></li>
</ul>)
}
it should "be capable to not fit an HTML Gauge, if the element is missing" in {
doesnt fit
<ul>
<li>
<p>Link1</p>
</li>
</ul>
}
it should "be capable to not fit an HTML Gauge, if the element order is wrong" in {
doesnt fit
<ul>
<li><div>Link 3</div></li>
<li><div>Link 1</div></li>
</ul>
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.scalawebtest.integration.browser.behaviors

import org.scalawebtest.integration.ScalaWebTestBaseSpec

trait JsonGaugeBehavior {

self: ScalaWebTestBaseSpec =>

//TODO: replace String comparison with JsonGauge, after Scala 2.10 support was removed from ScalaWebTest
def aJsonGauge(): Unit = {
def removeWhitespaces(s: String): String = {
s.replaceAll("\\s", "")
}

it should "be capable to handle JSON responses (some browsers wrap this in HTML and need special treatment)" in {
navigateTo("/jsonResponse.json.jsp")
removeWhitespaces(webDriver.getPageSource) shouldBe
removeWhitespaces("""
|{
| "name": "Dijkstra",
| "firstName": "Edsger",
| "yearOfBirth": 1930,
| "theories": [
| "shortest path",
| "graph theory"
| ],
| "isTuringAwardWinner": true,
| "universities": [
| {
| "name": "Universität Leiden",
| "begin": 1948,
| "end": 1956
| },
| {
| "name": "Mathematisch Centrum Amsterdam",
| "begin": 1951,
| "end": 1959
| },
| {
| "name": "Technische Universiteit Eindhoven",
| "begin": 1962,
| "end": 1984
| },
| {
| "name": "University of Texas at Austin",
| "begin": 1984,
| "end": 1999
| }
| ],
| "falseTheories": null
|}""".stripMargin)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.scalawebtest.integration.browser.behaviors

import org.scalatest.OptionValues
import org.scalawebtest.integration.ScalaWebTestBaseSpec

trait WebBrowserBehavior extends OptionValues {
self: ScalaWebTestBaseSpec =>
def aWebBrowserWithElementLookup(): Unit = {
it should "be capable to find a single element in an HTML document" in {
navigateTo("/")
find(cssSelector("h1")).value.text shouldBe "ScalaWebTest - Mock Server"
}
it should "be capable to find a list of elements in an HTML document" in {
navigateTo("/elementsList.jsp")
findAll(cssSelector("li")) should have size 3
}
}
}

0 comments on commit d074e27

Please sign in to comment.