Skip to content

Commit

Permalink
execute navigateTo automatically for every test #11 -
Browse files Browse the repository at this point in the history
 simplified implementation and providing more control on navigateTo execution
  • Loading branch information
Daniel.Rey committed Oct 24, 2016
1 parent 3c2c483 commit 363a618
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 69 deletions.
4 changes: 2 additions & 2 deletions aem/src/main/scala/org/scalawebtest/aem/AemTweaks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package org.scalawebtest.aem

import org.scalawebtest.aem.WcmMode._
import org.scalawebtest.core.{Configuration, FormBasedLogin, IntegrationSpec, WebClientExposingDriver}
import org.scalawebtest.core._

/**
* Extend this trait to inherit useful default configuration for AEM projects.
Expand All @@ -29,7 +29,7 @@ trait AemTweaks {
override val loginPath = "/libs/granite/core/content/login.html"

trait AemConfig {
self: Configuration =>
self: BaseConfiguration =>
def setWcmMode(wcmMode: WcmMode) = configurations += "wcmMode" ->
((webDriver: WebClientExposingDriver) => setWcmModeCookie(wcmMode))
}
Expand Down
83 changes: 83 additions & 0 deletions core/src/main/scala/org/scalawebtest/core/BaseConfiguration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.scalawebtest.core

class BaseConfiguration() {
var configurations: Map[String, WebClientExposingDriver => Unit] = Map()

//initialize with sensible default configuration
disableJavaScript()
swallowJavaScriptErrors()
disableCss()

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

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

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

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

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

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

}

class LoginConfiguration extends BaseConfiguration
class Configuration extends BaseConfiguration {
//initialize with sensible default configuration
var navigateToEnabled = true
var navigateToEnforced = false

/**
* Enabling navigateTo is the default. [[org.scalatest.BeforeAndAfterEach.beforeEach]] Test navigateTo is called with the current value of [[IntegrationSpec.path]]
*/
def enableNavigateTo(): Unit = navigateToEnabled = true

/**
* Disables the navigateTo call, which otherwise happens in [[org.scalatest.BeforeAndAfterEach.beforeEach]]
*/
def disableNavigateTo(): Unit = navigateToEnabled = false

/**
* By default navigateTo only navigates to the configured path, if it isn't the same as the currentPage.
* By enabling enforceNavigateTo, [[org.scalatest.selenium.WebBrowser.goTo()]] is always called.
*/
def enforceNavigateTo(): Unit = navigateToEnforced = true

/**
* This is the default behavior. NavigateTo only calls [[org.scalatest.selenium.WebBrowser.goTo()]], if the
* currentPage isn't the same as the configured path.
*/
def doNotEnforceNavigateTo(): Unit = navigateToEnforced = false
}
35 changes: 0 additions & 35 deletions core/src/main/scala/org/scalawebtest/core/Configuration.scala

This file was deleted.

43 changes: 16 additions & 27 deletions core/src/main/scala/org/scalawebtest/core/Specs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.util.logging.Level
import org.openqa.selenium.Cookie
import org.scalatest._
import org.scalatest.concurrent.Eventually
import org.scalatest.selenium.{Page, WebBrowser}
import org.scalatest.selenium.WebBrowser
import org.slf4j.LoggerFactory

import scala.collection.mutable.ListBuffer
Expand All @@ -29,22 +29,7 @@ abstract class FlatSpecBehavior extends FlatSpec with Matchers with Inspectors

abstract class FreeSpecBehavior extends FreeSpec with Matchers with Inspectors

abstract class IntegrationFlatSpec extends FlatSpecBehavior with IntegrationSpec {
/**
* As the IntegrationSpec will "go to" the defined url, as part of [[BeforeAndAfterEach.beforeEach()]], changing the path
* within the test is too late. To change the path for a test and all its successors, you can use afterChangingPathTo in between tests,
* providing the new path as parameter.
*/
def afterChangingPathTo(newPath: String): Unit = {
val timesChanged = pathChangeCounter.getOrElse(path, 0)
//calculate a postfix to make the test name unique
val postfix = "_" * timesChanged
registerTest("change path to " + newPath + " " + postfix)(path = newPath)
pathChangeCounter += newPath -> (timesChanged + 1)
}

var pathChangeCounter: Map[String, Int] = Map()
}
abstract class IntegrationFlatSpec extends FlatSpecBehavior with IntegrationSpec

abstract class IntegrationFreeSpec extends FreeSpecBehavior with IntegrationSpec

Expand All @@ -64,7 +49,7 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with
* Cookies cannot be set in this configuration. The webDriver
* has to open a connection, before it can set cookies
*/
val loginConfig = new Configuration
val loginConfig = new LoginConfiguration
/**
* Configuration applied after login.
* Cookies may be added here.
Expand Down Expand Up @@ -105,7 +90,7 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with
*/
def afterLogin() = {}

def applyConfiguration(config: Configuration): Unit = {
def applyConfiguration(config: BaseConfiguration): Unit = {
config.configurations.values.foreach(configFunction =>
try {
configFunction(webDriver)
Expand Down Expand Up @@ -139,7 +124,9 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with
}

override def beforeEach(): Unit = {
navigateToUrl(url)
if (config.navigateToEnabled) {
navigateToUrl(url)
}
}

/**
Expand All @@ -164,18 +151,20 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with

/**
* To navigate to a path during. For most case calling the setter on path is a better solution.
* Only use this function, if you have to change the path during a test.
* For all other cases use "path = \"/xyz\"" or afterChangingPathTo("xyz")
*
* Please be aware that having to change the path during a test is usually a sign, that your test
* tests multiple things. Try to focus on one thing per test.
* Only use this function, if you group tests, which work on different paths/urls. As soon as you
* introduce navigateTo for one test, you should call it in all succeeding tests. This to make sure,
* that a test may be executed on its own.
*/
def navigateTo(path: String): Unit = {
def navigateTo(newPath: String): Unit = {
path = newPath
navigateToUrl(s"$host$projectRoot$path")
}

private def navigateToUrl(targetUrl: String): Unit = {
if (pageSource == null || !(targetUrl equals webDriver.getCurrentUrl)) {
if (targetUrl.isEmpty) {
throw new RuntimeException("NavigateTo was called without path being defined. Either set config.disableNavigateTo or define path")
}
if (pageSource == null || !(targetUrl equals webDriver.getCurrentUrl) || config.navigateToEnforced) {
logger.info("Going to " + targetUrl)
go to targetUrl
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class AemTweaksSpec extends ScalaWebTestBaseSpec with AemTweaks {
config.enableJavaScript(throwOnError = true)
config.setWcmMode(DISABLED)

path = "/cookieVisualizing.jsp"

"When wcmmode DISABLED is select webBrowser" should "send the according cookie" in {
navigateTo("/cookieVisualizing.jsp")
implicitlyWait(Span(1, Seconds))
eventually(timeout(Span(1, Seconds))) {
fit(<li>@contains wcmmode</li>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ package org.scalawebtest.integration.json
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class TrivialJsonSpec extends ScalaWebTestBaseSpec {
path = "/jsonResponse.json.jsp"

"A computer scientist" should "have a firstname" in {
navigateTo("/jsonResponse.json.jsp")
webDriver.getPageSource should include("Edsger")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ package org.scalawebtest.integration.navigation

import org.scalawebtest.integration.ScalaWebTestBaseSpec

/**
* Using navigateTo to change the "test" path is discouraged,
* because navigateTo has to be called in all succeeding tests (although it will only navigateTo another page if needed),
* or
*/
class ChangePathSpec extends ScalaWebTestBaseSpec {
path = "/a.jsp"

"When url is defined it" should "automatically navigate to page A" in {
webDriver.findElementByTagName("h1").getText shouldEqual "a"
}
afterChangingPathTo("/b.jsp")
it should "navigate to page B" in {
navigateTo("/b.jsp")
webDriver.findElementByTagName("h1").getText shouldEqual "b"
}
afterChangingPathTo("/a.jsp")
it should "navigate to page A again" in {
navigateTo("/a.jsp")
webDriver.findElementByTagName("h1").getText shouldEqual "a"
}
afterChangingPathTo("/b.jsp")
it should "navigate to page B again" in {
navigateTo("/b.jsp")
webDriver.findElementByTagName("h1").getText shouldEqual "b"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.scalawebtest.integration.navigation

import org.scalawebtest.core.IntegrationFlatSpec

class DisableNavigateTo extends IntegrationFlatSpec{
path = "/a.jsp"
config.disableNavigateTo()

"When navigateTo is disabled" should "remain on about:blank" in {
webDriver.getCurrentUrl shouldEqual "about:blank"
}
}

0 comments on commit 363a618

Please sign in to comment.