Last active
April 2, 2023 10:12
-
-
Save dacr/271fa2d977e57ff4634d2891fde8e064 to your computer and use it in GitHub Desktop.
Playing with scalatest selenium integration to test remote sites with automatic http proxy support. / published by https://github.com/dacr/code-examples-manager #5b2090bd-197a-427a-a041-e35ea0d0ce8f/aa9cd46e8eefba561df88b971f8da67927451448
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : Playing with scalatest selenium integration to test remote sites with automatic http proxy support. | |
// keywords : scala, scalatest, selenium, proxy | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 5b2090bd-197a-427a-a041-e35ea0d0ce8f | |
// created-on : 2020-05-31T19:54:52Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' | |
import $ivy.`org.scalatest::scalatest:3.2.6` | |
import $ivy.`org.scalatestplus::selenium-3-141:3.2.5.0` | |
// Mandatory because by default selenium tools are very verbose... | |
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.SEVERE) | |
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(java.util.logging.Level.SEVERE) | |
import org.scalatest._, matchers._ | |
import org.scalatestplus.selenium._ | |
import org.openqa.selenium.WebDriver | |
import org.openqa.selenium.Proxy | |
import org.openqa.selenium.htmlunit.HtmlUnitDriver | |
import org.openqa.selenium.remote.DesiredCapabilities | |
import org.scalatestplus.selenium._ | |
class ThatSpec extends flatspec.AnyFlatSpec with should.Matchers with WebBrowser { | |
override def suiteName="ThatSpec" | |
val capabilities = DesiredCapabilities.htmlUnit() | |
capabilities.setJavascriptEnabled(false) | |
val proxyRE = """https?://([^:]+(?::\d+))?(?:/.*)?""".r | |
scala.util.Properties.envOrNone("http_proxy") collect { case proxyRE(proxyHostPort) => | |
val proxy = new Proxy() | |
proxy.setHttpProxy(proxyHostPort) | |
proxy.setSslProxy(proxyHostPort) | |
println(s"Using http proxy $proxyHostPort") | |
capabilities.setCapability("proxy", proxy) | |
} | |
implicit val driver: WebDriver = new HtmlUnitDriver(capabilities) | |
// ---------------------------------------------------------------------------------- | |
"selenium" should "check httpbin.org home page" in { | |
val home = "https://httpbin.org" | |
go to (home) | |
pageTitle shouldBe "httpbin.org" | |
pageSource should include ("the developer - Website") | |
} | |
it should "navigate on httpbin.org" in { | |
val home = "https://httpbin.org" | |
go to (home) | |
pageTitle should be ("httpbin.org") | |
click on linkText("the developer - Website") | |
pageTitle should be ("httpbin.org") | |
pageSource should include ("uncomment to enable the green top header") | |
} | |
it should "navigate on wikipedia" in { | |
val wikihome= "https://en.wikipedia.org/wiki/Selenium_(software)" | |
go to (wikihome) | |
pageTitle should be ("Selenium (software) - Wikipedia") | |
click on linkText("Software testing") | |
pageTitle should be ("Software testing - Wikipedia") | |
pageSource should include ("stakeholders") | |
} | |
} | |
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[ThatSpec].getName)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment