diff --git a/aem/src/main/scala/org/scalawebtest/aem/AemTweaks.scala b/aem/src/main/scala/org/scalawebtest/aem/AemTweaks.scala index 6070328..2910c25 100755 --- a/aem/src/main/scala/org/scalawebtest/aem/AemTweaks.scala +++ b/aem/src/main/scala/org/scalawebtest/aem/AemTweaks.scala @@ -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. @@ -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)) } diff --git a/core/src/main/scala/org/scalawebtest/core/BaseConfiguration.scala b/core/src/main/scala/org/scalawebtest/core/BaseConfiguration.scala new file mode 100755 index 0000000..4be4458 --- /dev/null +++ b/core/src/main/scala/org/scalawebtest/core/BaseConfiguration.scala @@ -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 +} diff --git a/core/src/main/scala/org/scalawebtest/core/Configuration.scala b/core/src/main/scala/org/scalawebtest/core/Configuration.scala deleted file mode 100755 index 50e48cc..0000000 --- a/core/src/main/scala/org/scalawebtest/core/Configuration.scala +++ /dev/null @@ -1,35 +0,0 @@ -package org.scalawebtest.core - -class Configuration() { - var configurations: Map[String, WebClientExposingDriver => Unit] = Map() - - //initialize with sensible default configuration - disableJavaScript() - swallowJavaScriptErrors() - disableCss() - - def enableJavaScript(throwOnError: Boolean): Unit = { - configurations += "enableJavaScript" -> - ((webDriver: WebClientExposingDriver) => webDriver.getOptions.setJavaScriptEnabled(true)) - configurations += "throwOnJSError" -> - ((webDriver: WebClientExposingDriver) => webDriver.getOptions.setThrowExceptionOnFailingStatusCode(throwOnError)) - } - - def disableJavaScript(): Unit = { - configurations += "enableJavaScript" -> ((webDriver: WebClientExposingDriver) => webDriver.getOptions.setJavaScriptEnabled(false)) - configurations += "throwOnJSError" -> ((webDriver: WebClientExposingDriver) => webDriver.getOptions.setThrowExceptionOnFailingStatusCode(false)) - } - - def throwOnJavaScriptError(): Unit = configurations += "throwOnJSError" -> - ((webDriver: WebClientExposingDriver) => webDriver.getOptions.setThrowExceptionOnFailingStatusCode(true)) - - def swallowJavaScriptErrors(): Unit = configurations += "throwOnJSError" -> - ((webDriver: WebClientExposingDriver) => webDriver.getOptions.setThrowExceptionOnFailingStatusCode(false)) - - def enableCss(): Unit = configurations += "enableCss" -> - ((webDriver: WebClientExposingDriver) => webDriver.getOptions.setCssEnabled(true)) - - def disableCss(): Unit = configurations += "enableCss" -> - ((webDriver: WebClientExposingDriver) => webDriver.getOptions.setCssEnabled(false)) - -} diff --git a/core/src/main/scala/org/scalawebtest/core/Specs.scala b/core/src/main/scala/org/scalawebtest/core/Specs.scala index 95516a7..a0e17e3 100755 --- a/core/src/main/scala/org/scalawebtest/core/Specs.scala +++ b/core/src/main/scala/org/scalawebtest/core/Specs.scala @@ -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 @@ -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 @@ -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. @@ -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) @@ -139,7 +124,9 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with } override def beforeEach(): Unit = { - navigateToUrl(url) + if (config.navigateToEnabled) { + navigateToUrl(url) + } } /** @@ -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 { diff --git a/integration_test/src/it/scala/org/scalawebtest/integration/extensions/aem/AemTweaksSpec.scala b/integration_test/src/it/scala/org/scalawebtest/integration/extensions/aem/AemTweaksSpec.scala index ef43221..20213e3 100755 --- a/integration_test/src/it/scala/org/scalawebtest/integration/extensions/aem/AemTweaksSpec.scala +++ b/integration_test/src/it/scala/org/scalawebtest/integration/extensions/aem/AemTweaksSpec.scala @@ -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(
  • @contains wcmmode
  • ) diff --git a/integration_test/src/it/scala/org/scalawebtest/integration/json/TrivialJsonSpec.scala b/integration_test/src/it/scala/org/scalawebtest/integration/json/TrivialJsonSpec.scala index 2358a2a..3ad1596 100755 --- a/integration_test/src/it/scala/org/scalawebtest/integration/json/TrivialJsonSpec.scala +++ b/integration_test/src/it/scala/org/scalawebtest/integration/json/TrivialJsonSpec.scala @@ -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") } } diff --git a/integration_test/src/it/scala/org/scalawebtest/integration/navigation/ChangePathSpec.scala b/integration_test/src/it/scala/org/scalawebtest/integration/navigation/ChangePathSpec.scala index 6914aed..2f2612b 100755 --- a/integration_test/src/it/scala/org/scalawebtest/integration/navigation/ChangePathSpec.scala +++ b/integration_test/src/it/scala/org/scalawebtest/integration/navigation/ChangePathSpec.scala @@ -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" } } diff --git a/integration_test/src/it/scala/org/scalawebtest/integration/navigation/DisableNavigateTo.scala b/integration_test/src/it/scala/org/scalawebtest/integration/navigation/DisableNavigateTo.scala new file mode 100755 index 0000000..8d3a431 --- /dev/null +++ b/integration_test/src/it/scala/org/scalawebtest/integration/navigation/DisableNavigateTo.scala @@ -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" + } +}