Skip to content

Commit

Permalink
Add "currentPage fits <html></html>" #83
Browse files Browse the repository at this point in the history
 - add CurrentPage class to IntegrationSpec
 - CurrentPage provides convenience methods to access webDriver.getPageSource, and HtmlGauge .fits, .fit, .doesntFit and not fit (via doesNotFit)
  • Loading branch information
DaniRey committed Jun 10, 2019
1 parent 03be5ce commit 8e9e002
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 34 deletions.
Expand Up @@ -23,10 +23,12 @@ import org.scalatest._
import org.scalatest.concurrent.Eventually
import org.scalatestplus.selenium.WebBrowser
import org.scalawebtest.core.configuration.{BaseConfiguration, Configuration, HtmlUnitConfiguration, LoginConfiguration}
import org.scalawebtest.core.gauge.Gauge
import org.slf4j.{Logger, LoggerFactory}

import scala.collection.mutable.ListBuffer
import scala.language.postfixOps
import scala.xml.NodeSeq

/**
* This is the base trait for integration specs. The recommended way is to create your own project specific trait, which extends
Expand Down Expand Up @@ -59,10 +61,54 @@ trait IntegrationSpec extends WebBrowser with Suite with BeforeAndAfterEach with
*/
var path: String = ""

/**
* @return [[uri]] as String
*/
def url: String = uri.toString

/**
* @return [[config.baseUri]] + [[path]] as [[java.net.URI]]
*/
def uri: URI = config.baseUri.resolve(config.baseUri.getPath + path)

/**
* @return [[webDriver.getPageSource]] wrapped in a [[CurrentPage]]
*/
def currentPage: CurrentPage = CurrentPage(webDriver.getPageSource)

/**
* The [[CurrentPage]] provides convenient access to the source of the current page and to HtmlGauge functions.
*/
case class CurrentPage(source: String) {
/**
* The same as [[org.scalawebtest.core.gauge.HtmlGauge#fits]], but explicitly using the source of the currentPage.
*
* @see [[org.scalawebtest.core.gauge.HtmlGauge#fits]]
*/
def fits(definition: NodeSeq): Unit = new Gauge(definition).fitsCurrentPageSource(source)

/**
* The same as [[org.scalawebtest.core.gauge.HtmlGauge#fits]], but explicitly using the source of the currentPage.
*
* @see [[org.scalawebtest.core.gauge.HtmlGauge#fits]]
*/
def fit(definition: NodeSeq): Unit = new Gauge(definition).fitsCurrentPageSource(source)

/**
* The same as [[org.scalawebtest.core.gauge.HtmlGauge.doesnt#fit]], but explicitly using the source of the currentPage.
*
* @see [[org.scalawebtest.core.gauge.HtmlGauge.doesnt#fit]]
*/
def doesNotFit(definition: NodeSeq): Unit = new Gauge(definition).doesNotFitCurrentPageSource(source)

/**
* The same as [[org.scalawebtest.core.gauge.HtmlGauge.doesnt#fit]], but explicitly using the source of the currentPage.
*
* @see [[org.scalawebtest.core.gauge.HtmlGauge.doesnt#fit]]
*/
def doesntFit(definition: NodeSeq): Unit = new Gauge(definition).doesNotFitCurrentPageSource(source)
}

/**
* HtmlUnit reports a lot of unimportant warnings, such as:
* 'WARNING: Obsolete content type encountered: 'text/javascript'
Expand Down
Expand Up @@ -14,10 +14,6 @@
*/
package org.scalawebtest.core

import com.gargoylesoftware.htmlunit.TextPage
import com.gargoylesoftware.htmlunit.html.HtmlPage
import com.gargoylesoftware.htmlunit.xml.XmlPage
import org.openqa.selenium.WebDriver
import org.scalatest.Assertions

/**
Expand Down Expand Up @@ -62,20 +58,4 @@ trait ResponseAccessors {
did not contain the expected response header with field-name: '$name'
It contained the following header field-names: $headerNames""")
}

/**
* @return Some(HtmlPage) if the currentPage is a HtmlPage otherwise None
*/
def currentHtmlPage: Option[HtmlPage] = asWebClientExposingDriverOrError(webDriver).getCurrentHtmlPage

/**
* @return Some(TextPage) if the currentPage is a TextPage otherwise None
*/
def currentTextPage: Option[TextPage] = asWebClientExposingDriverOrError(webDriver).getCurrentTextPage

/**
* @return Some(XmlPage) if the currentPage is an XmlPage otherwise None
*/
def currentXmlPage: Option[XmlPage] = asWebClientExposingDriverOrError(webDriver).getCurrentXmlPage

}
Expand Up @@ -38,9 +38,13 @@ class Gauge(definition: NodeSeq)(implicit webDriver: WebDriver) extends Assertio
case class Fit(domNode: JNode, misfitRelevance: MisfitRelevance)

def fits(): Unit = {
var fittingNodes = List[Fit]()
fitsCurrentPageSource(webDriver.getPageSource)
}

def fitsCurrentPageSource(currentPageSource: String): Unit = {
var fittingNodes = List[Fit]()

val currentPage = Jsoup.parse(webDriver.getPageSource)
val currentPage = Jsoup.parse(currentPageSource)
definition.theSeq.foreach(gaugeElement => {
val fittingNode = nodeFits(currentPage, gaugeElement, None, MISFIT_RELEVANCE_START_VALUE)
if (fittingNode.isEmpty) {
Expand All @@ -55,7 +59,11 @@ class Gauge(definition: NodeSeq)(implicit webDriver: WebDriver) extends Assertio
}

def doesNotFit(): Unit = {
val currentPage = Jsoup.parse(webDriver.getPageSource)
doesNotFitCurrentPageSource(webDriver.getPageSource)
}

def doesNotFitCurrentPageSource(currentPageSource: String): Unit = {
val currentPage = Jsoup.parse(currentPageSource)
definition.theSeq.foreach(gaugeElement => {
val fit = nodeFits(currentPage, gaugeElement, None, MISFIT_RELEVANCE_START_VALUE)
if (fit.isDefined) {
Expand Down Expand Up @@ -491,13 +499,13 @@ trait HtmlGauge {
}

/**
* Synonym for [[Gauge.fits]]. Use whatever reads better in your current context.
* Synonym for [[org.scalawebtest.core.gauge.HtmlGauge#fits]]. Use whatever reads better in your current context.
*/
def fit(definition: NodeSeq)(implicit webDriver: WebDriver): Unit = fits(definition)

implicit class NotFit(notWord: NotWord) {
/**
* Synonym for [[doesnt.fit]]. Use whatever reads better in your current context.
* Synonym for [[org.scalawebtest.core.gauge.HtmlGauge.doesnt#fit]]. Use whatever reads better in your current context.
*/
def fit(definition: NodeSeq)(implicit webDriver: WebDriver): Unit = doesnt.fit(definition)
}
Expand All @@ -506,7 +514,7 @@ trait HtmlGauge {
/**
* Assert that the current document `doesnt fit` the html snippet provided as definition for the `Gauge`
*
* To get detailed information about available options in `Gauge` definitions, read the ScalaDoc of [[Gauge.fits]]
* To get detailed information about available options in `Gauge` definitions, read the ScalaDoc of [[org.scalawebtest.core.gauge.HtmlGauge#fits]]
*/
def fit(definition: NodeSeq)(implicit webDriver: WebDriver): Unit = {
new Gauge(definition).doesNotFit()
Expand Down
@@ -0,0 +1,37 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalawebtest.integration.gauge

import org.scalawebtest.core.IntegrationFlatSpec
import org.scalawebtest.core.gauge.HtmlGauge._

class HtmlGaugeFromCurrentPageSpec extends IntegrationFlatSpec {
config.useBaseUri("http://localhost:9090")

path = "/index.jsp"

"CurrentPage" should "fit a matching GaugeDefinition" in {
currentPage fits <h1>ScalaWebTest - Mock Server</h1>
}
it should "also work with fit (synonym of fits)" in {
currentPage fit <h1>ScalaWebTest - Mock Server</h1>
}
it should "not fit a GaugeDefinition with a wrong title" in {
currentPage doesntFit <h2>ScalaWebTest - False Text and Tag</h2>
}
it should """also work with "doesNotFit" (synonym of doesntFit)""" in {
currentPage doesNotFit <h2>ScalaWebTest - False Text and Tag</h2>
}
}
@@ -1,16 +1,11 @@
package org.scalawebtest.integration.response

import com.gargoylesoftware.htmlunit.html.HtmlPage
import org.scalawebtest.core.ResponseAccessors
import org.scalawebtest.integration.ScalaWebTestBaseSpec

class CurrentPageSpec extends ScalaWebTestBaseSpec with ResponseAccessors {
class CurrentPageSpec extends ScalaWebTestBaseSpec {
path = "/index.jsp"

"When opening a HTML page, it" should "be accessible via currentHtmlPage" in {
currentHtmlPage.get shouldBe a[HtmlPage]
}
it should "return None when trying to get the currentXmlPage" in {
currentXmlPage shouldBe None
"CurrentPage.source" should "return the same as webdriver.getPageSource" in {
currentPage.source shouldBe webDriver.getPageSource
}
}

0 comments on commit 8e9e002

Please sign in to comment.