From 8e9e00283dbdabcd1eaf7726a93568969e373bbe Mon Sep 17 00:00:00 2001 From: Dani Rey Date: Mon, 10 Jun 2019 11:19:56 +0200 Subject: [PATCH] Add "currentPage fits " #83 - add CurrentPage class to IntegrationSpec - CurrentPage provides convenience methods to access webDriver.getPageSource, and HtmlGauge .fits, .fit, .doesntFit and not fit (via doesNotFit) --- .../scalawebtest/core/IntegrationSpec.scala | 46 +++++++++++++++++++ .../scalawebtest/core/ResponseAccessors.scala | 20 -------- .../org/scalawebtest/core/gauge/Gauge.scala | 20 +++++--- .../gauge/HtmlGaugeFromCurrentPageSpec.scala | 37 +++++++++++++++ .../response/CurrentPageSpec.scala | 11 ++--- 5 files changed, 100 insertions(+), 34 deletions(-) create mode 100644 scalawebtest-integration/src/it/scala/org/scalawebtest/integration/gauge/HtmlGaugeFromCurrentPageSpec.scala diff --git a/scalawebtest-core/src/main/scala/org/scalawebtest/core/IntegrationSpec.scala b/scalawebtest-core/src/main/scala/org/scalawebtest/core/IntegrationSpec.scala index 423bad7..ddcc571 100755 --- a/scalawebtest-core/src/main/scala/org/scalawebtest/core/IntegrationSpec.scala +++ b/scalawebtest-core/src/main/scala/org/scalawebtest/core/IntegrationSpec.scala @@ -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 @@ -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' diff --git a/scalawebtest-core/src/main/scala/org/scalawebtest/core/ResponseAccessors.scala b/scalawebtest-core/src/main/scala/org/scalawebtest/core/ResponseAccessors.scala index a70489e..566c5f1 100644 --- a/scalawebtest-core/src/main/scala/org/scalawebtest/core/ResponseAccessors.scala +++ b/scalawebtest-core/src/main/scala/org/scalawebtest/core/ResponseAccessors.scala @@ -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 /** @@ -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 - } diff --git a/scalawebtest-core/src/main/scala/org/scalawebtest/core/gauge/Gauge.scala b/scalawebtest-core/src/main/scala/org/scalawebtest/core/gauge/Gauge.scala index 8996bd9..e2f7585 100755 --- a/scalawebtest-core/src/main/scala/org/scalawebtest/core/gauge/Gauge.scala +++ b/scalawebtest-core/src/main/scala/org/scalawebtest/core/gauge/Gauge.scala @@ -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) { @@ -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) { @@ -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) } @@ -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() diff --git a/scalawebtest-integration/src/it/scala/org/scalawebtest/integration/gauge/HtmlGaugeFromCurrentPageSpec.scala b/scalawebtest-integration/src/it/scala/org/scalawebtest/integration/gauge/HtmlGaugeFromCurrentPageSpec.scala new file mode 100644 index 0000000..d5acc04 --- /dev/null +++ b/scalawebtest-integration/src/it/scala/org/scalawebtest/integration/gauge/HtmlGaugeFromCurrentPageSpec.scala @@ -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

ScalaWebTest - Mock Server

+ } + it should "also work with fit (synonym of fits)" in { + currentPage fit

ScalaWebTest - Mock Server

+ } + it should "not fit a GaugeDefinition with a wrong title" in { + currentPage doesntFit

ScalaWebTest - False Text and Tag

+ } + it should """also work with "doesNotFit" (synonym of doesntFit)""" in { + currentPage doesNotFit

ScalaWebTest - False Text and Tag

+ } +} diff --git a/scalawebtest-integration/src/it/scala/org/scalawebtest/integration/response/CurrentPageSpec.scala b/scalawebtest-integration/src/it/scala/org/scalawebtest/integration/response/CurrentPageSpec.scala index 39c4b02..a9aa026 100644 --- a/scalawebtest-integration/src/it/scala/org/scalawebtest/integration/response/CurrentPageSpec.scala +++ b/scalawebtest-integration/src/it/scala/org/scalawebtest/integration/response/CurrentPageSpec.scala @@ -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 } }