Skip to content

Commit

Permalink
execute navigateTo automatically for every test #11 -
Browse files Browse the repository at this point in the history
the path can now be defined as part of the spec. The IntegrationSpec will then execute "go to" before each test, using the stored url (defined as $host$projectRoot$path). If some tests need to change the path, they might do so by calling the afterChangingPathTo method.
  • Loading branch information
Daniel.Rey committed Oct 23, 2016
1 parent 6cc8180 commit 3c2c483
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 25 deletions.
59 changes: 52 additions & 7 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.WebBrowser
import org.scalatest.selenium.{Page, WebBrowser}
import org.slf4j.LoggerFactory

import scala.collection.mutable.ListBuffer
Expand All @@ -29,7 +29,22 @@ 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
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 IntegrationFreeSpec extends FreeSpecBehavior with IntegrationSpec

Expand All @@ -56,6 +71,18 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with
*/
val config = new Configuration

/**
* Stores the path with prefixed [[IntegrationSettings.host]] and [[IntegrationSpec.projectRoot]] as url.
* Before each test [[WebBrowser.goTo()]] with the currently stored url will be executed, but only
* if [[WebClientExposingDriver.getCurrentUrl]] doesn't equal url.
*/
def path_=(path: String): Unit = {
url = s"$host$projectRoot$path"
}

def path = url.replaceFirst(host, "").replaceFirst(projectRoot, "")

private var url: String = ""

/**
* Override to encode your project specific login mechanism.
Expand Down Expand Up @@ -94,15 +121,15 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with
java.util.logging.Logger.getLogger("net.lightbody").setLevel(Level.OFF)
}

override def afterEach() {
override def afterEach(): Unit = {
cookiesToBeDiscarded.foreach(cookie => delete cookie cookie.getName)
cookiesToBeDiscarded.clear()
}

/**
* Overwrite beforeLogin() and afterLogin() for test-specific tasks
*/
override def beforeAll() {
override def beforeAll(): Unit = {
beforeLogin()
avoidLogSpam()
applyConfiguration(config)
Expand All @@ -111,14 +138,18 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with
afterLogin()
}

override def beforeEach(): Unit = {
navigateToUrl(url)
}

/**
* Sets a cookie for the current test. Any cookie set through this method
* is discarded after a test.
*/
def setCookieForSingleTest(name: String, value: String) {
val cookie: Cookie = new Cookie(name, value)
webDriver.manage().addCookie(cookie)
add cookie (name, value)
add cookie(name, value)
cookiesToBeDiscarded += cookie
}

Expand All @@ -131,11 +162,25 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with
afterLogin()
}

def navigateTo(path: String) {
val targetUrl: String = s"$host$path"
/**
* 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.
*/
def navigateTo(path: String): Unit = {
navigateToUrl(s"$host$projectRoot$path")
}

private def navigateToUrl(targetUrl: String): Unit = {
if (pageSource == null || !(targetUrl equals webDriver.getCurrentUrl)) {
logger.info("Going to " + targetUrl)
go to targetUrl
} else {
logger.info("Remaining at " + targetUrl + " without refreshing page")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait ScalaWebTestBaseSpec extends IntegrationFlatSpec with AemTweaks with FormB
override val host = "http://localhost:8080"
override val loginPath = "/fakeLogin.jsp"

override val projectRoot = "/"
override val projectRoot = ""

override def loginTimeout = Timeout(5 seconds)
}
3 changes: 2 additions & 1 deletion integration_test/src/it/scala/org/scalawebtest/integration/gauge/ContainsSpec.scala
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import org.scalawebtest.core.gauge.Gauge.fits
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class ContainsSpec extends ScalaWebTestBaseSpec {
path = "/navigation.jsp"

"Contains" should "loosely match attributes" in {
navigateTo("/navigation.jsp")
fits(
<nav>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import org.scalawebtest.core.gauge.Gauge.{doesnt, fits}
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class ElementOrderSpec extends ScalaWebTestBaseSpec {
path = "/textElementOrder.jsp"

"Text element order" should "have a correct main navigation" in {
navigateTo("/textElementOrder.jsp")
fits(
<div>
<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import org.scalawebtest.core.gauge.Gauge.fits
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class ElementsListSpec extends ScalaWebTestBaseSpec {
path = "/elementsList.jsp"

"List" should "contain three items" in {
navigateTo("/elementsList.jsp")
fits(
<ul>
<li class="list_item"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import org.scalawebtest.core.gauge.Gauge._
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class ExactSpec extends ScalaWebTestBaseSpec {
path = "/navigation.jsp"
"The default matcher" should "exactly match attributes" in {
navigateTo("/navigation.jsp")
fits(
<nav>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import org.scalawebtest.core.gauge.Gauge.fits
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class RegexSpec extends ScalaWebTestBaseSpec {
path = "/navigation.jsp"

"Regex" should "work for path matches" in {
navigateTo("/navigation.jsp")
fits(
<nav>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import org.scalawebtest.core.gauge.Gauge.{NotFit, doesnt, fit, fits}
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class SynonymSpec extends ScalaWebTestBaseSpec {
path = "/index.jsp"

"CheckingGauge" should "work with fits" in {
navigateTo("/index.jsp")
fits(<h1>Unic AEM Testing - Mock Server</h1>)
}
it should "work with fit (synonym of fits)" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import org.scalawebtest.core.gauge.Gauge.fits
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class AjaxSpec extends ScalaWebTestBaseSpec {
path = "/simpleAjax.jsp"
config.enableJavaScript(throwOnError = true)

"A simple webpage loading content with JS" should "be correctly interpreted by HtmlUnit" in {
navigateTo("/simpleAjax.jsp")
eventually(timeout(Span(1, Seconds))) {
container.text should include("Text loaded with JavaScript")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import org.scalawebtest.integration.ScalaWebTestBaseSpec
import play.api.libs.json.Json

class JsonSpec extends ScalaWebTestBaseSpec {
path = "/jsonResponse.json.jsp"
val model = new Scientist

"A computer scientist" should "have the correct firstname" in {
navigateTo("/jsonResponse.json.jsp")
model.firstName should equal("Edsger")
}
it should "have the correct lastname" in {
Expand All @@ -39,16 +39,15 @@ class JsonSpec extends ScalaWebTestBaseSpec {
"University of Texas at Austin"
)
}
}

class Scientist(implicit webDriver: WebDriver) {
def json = Json.parse(webDriver.getPageSource)

def firstName = (json \ "firstName").as[String]
class Scientist(implicit webDriver: WebDriver) {
def json = Json.parse(webDriver.getPageSource)
def firstName = (json \ "firstName").as[String]
def name = (json \ "name").as[String]
def theories = (json \ "theories").as[List[String]]
def universityNames = (json \ "universities" \\ "name").map(_.as[String])
}

def name = (json \ "name").as[String]
}

def theories = (json \ "theories").as[List[String]]

def universityNames = (json \ "universities" \\ "name").map(_.as[String])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.scalawebtest.integration.navigation

import org.scalawebtest.integration.ScalaWebTestBaseSpec

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 {
webDriver.findElementByTagName("h1").getText shouldEqual "b"
}
afterChangingPathTo("/a.jsp")
it should "navigate to page A again" in {
webDriver.findElementByTagName("h1").getText shouldEqual "a"
}
afterChangingPathTo("/b.jsp")
it should "navigate to page B again" in {
webDriver.findElementByTagName("h1").getText shouldEqual "b"
}
}
9 changes: 9 additions & 0 deletions integration_test/src/main/webapp/a.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>a</title>
</head>
<body>
<h1>a</h1>
</body>
</html>
9 changes: 9 additions & 0 deletions integration_test/src/main/webapp/b.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>b</title>
</head>
<body>
<h1>b</h1>
</body>
</html>

0 comments on commit 3c2c483

Please sign in to comment.