From d8493a8f515086d40167ba3271ae3918e5a42e96 Mon Sep 17 00:00:00 2001 From: John Duffell Date: Fri, 18 Jul 2014 15:38:07 +0100 Subject: [PATCH 1/4] no need to have ()=> when you are using the logInToGUPage --- README.md | 11 +++++------ build.sbt | 2 +- .../automation/{support => signin}/LoggingIn.scala | 6 +++--- src/test/resources/project.conf | 1 + .../com/gu/automation/signin/LoggingInTest.scala | 12 ++++++++++++ 5 files changed, 22 insertions(+), 10 deletions(-) rename src/main/scala/com/gu/automation/{support => signin}/LoggingIn.scala (87%) create mode 100644 src/test/resources/project.conf create mode 100644 src/test/scala/com/gu/automation/signin/LoggingInTest.scala diff --git a/README.md b/README.md index fb8e1aa..c4597cf 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ -web-automation-core-scala-api +scala-automation-web-signin ----------------------------- To use this just edit your build.sbt as follows: libraryDependencies ++= Seq( - "com.gu" %% "web-automation-core-scala-api" % "1.0-SNAPSHOT" + "com.gu" %% "scala-automation-web-signin" % "1.xxx" ) -AuthApi -------- -From your code, you can import com.gu.automation.api.AuthApi and then do something like +Add something to your conf file e.g. for CODE +"idApiRoot" : "https://idapi.code.dev-theguardian.com" - val future = AuthApi.authenticate(email, password) \ No newline at end of file +See the example code in LoggingInTest \ No newline at end of file diff --git a/build.sbt b/build.sbt index 6077e83..ff09e17 100644 --- a/build.sbt +++ b/build.sbt @@ -19,7 +19,7 @@ resolvers ++= Seq( libraryDependencies ++= Seq( //"org.scalatest" % "scalatest_2.10" % "2.0", //"com.typesafe.play" %% "play-ws" % "2.3.0" - "com.gu" %% "scala-automation" % "1.0", + "com.gu" %% "scala-automation" % "1.16", "com.gu" %% "scala-automation-api-client" % "1.0" ) diff --git a/src/main/scala/com/gu/automation/support/LoggingIn.scala b/src/main/scala/com/gu/automation/signin/LoggingIn.scala similarity index 87% rename from src/main/scala/com/gu/automation/support/LoggingIn.scala rename to src/main/scala/com/gu/automation/signin/LoggingIn.scala index f3f2cfc..42c2a29 100644 --- a/src/main/scala/com/gu/automation/support/LoggingIn.scala +++ b/src/main/scala/com/gu/automation/signin/LoggingIn.scala @@ -25,10 +25,10 @@ trait LoggingIn { } } - def logInToGUPage[P](goto: () => P, user: Option[String] = None)(implicit driver: WebDriver): P = { - goto() + def logInToGUPage[P](goto: => P, user: Option[String] = None)(implicit driver: WebDriver): P = { + goto addGULoginCookies(user) - goto() + goto } } diff --git a/src/test/resources/project.conf b/src/test/resources/project.conf new file mode 100644 index 0000000..16a77ba --- /dev/null +++ b/src/test/resources/project.conf @@ -0,0 +1 @@ +"idApiRoot" : "https://idapi.code.dev-theguardian.com" diff --git a/src/test/scala/com/gu/automation/signin/LoggingInTest.scala b/src/test/scala/com/gu/automation/signin/LoggingInTest.scala new file mode 100644 index 0000000..8d719a1 --- /dev/null +++ b/src/test/scala/com/gu/automation/signin/LoggingInTest.scala @@ -0,0 +1,12 @@ +package com.gu.automation.signin + +import com.gu.automation.support.LoggingIn +import org.scalatest.FlatSpec + +class LoggingInTest extends FlatSpec with LoggingIn { + + "loggingIn" should "compile the example code" in { +// logInToGUPage(ExamplePage.goto(), Some("user")) + } + +} From 8659f6425e1e3133020391a27c3b1a96d9c9854b Mon Sep 17 00:00:00 2001 From: John Duffell Date: Fri, 18 Jul 2014 15:57:30 +0100 Subject: [PATCH 2/4] update javadoc for LoggingIn trait --- src/main/scala/com/gu/automation/signin/LoggingIn.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/scala/com/gu/automation/signin/LoggingIn.scala b/src/main/scala/com/gu/automation/signin/LoggingIn.scala index 42c2a29..b20a0cf 100644 --- a/src/main/scala/com/gu/automation/signin/LoggingIn.scala +++ b/src/main/scala/com/gu/automation/signin/LoggingIn.scala @@ -8,7 +8,9 @@ import scala.concurrent.Await ; /** - * Created by jduffell on 20/06/2014. + * Example: + * + * logInToGUPage(ExamplePage.goto(), Some("user")) */ trait LoggingIn { From 1bcde38e39e7a1ece4022c80ab288539caac5aeb Mon Sep 17 00:00:00 2001 From: John Duffell Date: Mon, 21 Jul 2014 17:23:34 +0100 Subject: [PATCH 3/4] update LogIn to determine the domain automatically and have an easier to understand syntax --- .../com/gu/automation/signin/LogIn.scala | 50 +++++++++++++++++++ .../com/gu/automation/signin/LoggingIn.scala | 36 ------------- src/test/resources/project.conf | 1 + .../gu/automation/signin/LoggingInTest.scala | 37 +++++++++++--- 4 files changed, 82 insertions(+), 42 deletions(-) create mode 100644 src/main/scala/com/gu/automation/signin/LogIn.scala delete mode 100644 src/main/scala/com/gu/automation/signin/LoggingIn.scala diff --git a/src/main/scala/com/gu/automation/signin/LogIn.scala b/src/main/scala/com/gu/automation/signin/LogIn.scala new file mode 100644 index 0000000..991ba7a --- /dev/null +++ b/src/main/scala/com/gu/automation/signin/LogIn.scala @@ -0,0 +1,50 @@ +package com.gu.automation.support + +import com.gu.automation.api.AuthApi +import org.openqa.selenium.{Cookie, WebDriver} +import scala.concurrent.duration._ + +import scala.concurrent.Await +; + +/** + * Example: if your local.conf contains: + * + * userName: { + * loginEmail: "asdf@theguardian.com" + * loginPassword: passw0rd + * } + * + * You would do this code: + * + * LogIn(Some("userName")) + * ExamplePage.goto() + * + */ +object LogIn { + + def getCookieDomain(url: String) = + """http(s?)://([^.]*(\.))?([^/]+).*$""".r.replaceAllIn(url, "$3$4") + + def apply(user: Option[String])(implicit driver: WebDriver) = { + val idApiRoot = Config().getIdApiRoot() + val loginEmail = Config().getLoginEmail(user) + val loginPassword = Config().getLoginPassword(user) + val baseUrl = Config().getTestBaseUrl() + val loginDomain = getCookieDomain(baseUrl) + + driver.get(baseUrl) // have to be on the right url to add the cookies + + val future = AuthApi(idApiRoot).authenticate(loginEmail, loginPassword) + + val accessToken = Await.result(future, 30.seconds) + val cookies = accessToken match { case Right(cookies) => cookies } + cookies.foreach { + case (key, value) => + val isSecure = key.startsWith("SC_") + val cookie = new Cookie(key, value, loginDomain, "/", null, isSecure, isSecure) + driver.manage().addCookie(cookie) + } + } + +} diff --git a/src/main/scala/com/gu/automation/signin/LoggingIn.scala b/src/main/scala/com/gu/automation/signin/LoggingIn.scala deleted file mode 100644 index b20a0cf..0000000 --- a/src/main/scala/com/gu/automation/signin/LoggingIn.scala +++ /dev/null @@ -1,36 +0,0 @@ -package com.gu.automation.support - -import com.gu.automation.api.AuthApi -import org.openqa.selenium.{Cookie, WebDriver} -import scala.concurrent.duration._ - -import scala.concurrent.Await -; - -/** - * Example: - * - * logInToGUPage(ExamplePage.goto(), Some("user")) - */ -trait LoggingIn { - - def addGULoginCookies(user: Option[String])(implicit driver: WebDriver) = { - val future = AuthApi(Config().getIdApiRoot()).authenticate(Config().getLoginEmail(user), Config().getLoginPassword(user)) - - val accessToken = Await.result(future, 30.seconds) - val cookies = accessToken match { case Right(cookies) => cookies } - cookies.foreach { - case (key, value) => - val isSecure = key.startsWith("SC_") - val cookie = new Cookie(key, value, null, "/", null, isSecure, isSecure) - driver.manage().addCookie(cookie) - } - } - - def logInToGUPage[P](goto: => P, user: Option[String] = None)(implicit driver: WebDriver): P = { - goto - addGULoginCookies(user) - goto - } - -} diff --git a/src/test/resources/project.conf b/src/test/resources/project.conf index 16a77ba..865414f 100644 --- a/src/test/resources/project.conf +++ b/src/test/resources/project.conf @@ -1 +1,2 @@ "idApiRoot" : "https://idapi.code.dev-theguardian.com" +testBaseUrl: "http://m.code.dev-theguardian.com" diff --git a/src/test/scala/com/gu/automation/signin/LoggingInTest.scala b/src/test/scala/com/gu/automation/signin/LoggingInTest.scala index 8d719a1..b40e885 100644 --- a/src/test/scala/com/gu/automation/signin/LoggingInTest.scala +++ b/src/test/scala/com/gu/automation/signin/LoggingInTest.scala @@ -1,12 +1,37 @@ package com.gu.automation.signin -import com.gu.automation.support.LoggingIn -import org.scalatest.FlatSpec +import com.gu.automation.core.WebDriverFeatureSpec +import com.gu.automation.support.{LogIn, Config} +import org.openqa.selenium.{By, WebDriver} +import org.scalatest.Matchers -class LoggingInTest extends FlatSpec with LoggingIn { +class LoggingInTest extends WebDriverFeatureSpec with Matchers { - "loggingIn" should "compile the example code" in { -// logInToGUPage(ExamplePage.goto(), Some("user")) - } + info("Tests for the API Logging in function") + + feature("should be able to log in to the browser") { + + scenario("check we can get the right cookie domains") { _ => + + LogIn.getCookieDomain("http://www.theguardian.com/uk") should be (".theguardian.com") + LogIn.getCookieDomain("http://localhost:9000/") should be ("localhost:9000") + LogIn.getCookieDomain("https://www.theguardian.com/uk") should be (".theguardian.com") + LogIn.getCookieDomain("https://m.code.dev-theguardian.com/") should be (".code.dev-theguardian.com") + + } + + /** + * scenarioWeb handles starting and stopping the browser, you will have to declare an implicit driver as shown + */ + scenarioWeb("check we are logged in when we have added the cookies") { implicit driver: WebDriver => + + LogIn(Some("memberLogin")) +// ExamplePage.goto() + + driver.get(Config().getTestBaseUrl()) + val userSpan = driver.findElement(By.xpath("//div[@data-component='identity-profile']")).findElement(By.className("js-profile-info")) + userSpan.getText should be ("Reg Idtester") + } + } } From 37b3000840eb4d9c8b952dd3f2cb91f15680104d Mon Sep 17 00:00:00 2001 From: John Duffell Date: Tue, 22 Jul 2014 11:04:57 +0100 Subject: [PATCH 4/4] update logging in trait to upload --- .../com/gu/automation/signin/LogIn.scala | 15 +++++++++++--- src/test/resources/project.conf | 2 -- .../gu/automation/signin/LoggingInTest.scala | 20 +++++++++++++++---- 3 files changed, 28 insertions(+), 9 deletions(-) delete mode 100644 src/test/resources/project.conf diff --git a/src/main/scala/com/gu/automation/signin/LogIn.scala b/src/main/scala/com/gu/automation/signin/LogIn.scala index 991ba7a..e3f5c19 100644 --- a/src/main/scala/com/gu/automation/signin/LogIn.scala +++ b/src/main/scala/com/gu/automation/signin/LogIn.scala @@ -26,7 +26,13 @@ object LogIn { def getCookieDomain(url: String) = """http(s?)://([^.]*(\.))?([^/]+).*$""".r.replaceAllIn(url, "$3$4") - def apply(user: Option[String])(implicit driver: WebDriver) = { + def apply()(implicit driver: WebDriver) = { + apply(None, driver) + } + def apply(user: String)(implicit driver: WebDriver) = { + apply(Some(user), driver) + } + private def apply(user: Option[String], driver: WebDriver) = { val idApiRoot = Config().getIdApiRoot() val loginEmail = Config().getLoginEmail(user) val loginPassword = Config().getLoginPassword(user) @@ -37,8 +43,11 @@ object LogIn { val future = AuthApi(idApiRoot).authenticate(loginEmail, loginPassword) - val accessToken = Await.result(future, 30.seconds) - val cookies = accessToken match { case Right(cookies) => cookies } + val accessToken = Await.result(future, 10.seconds) + val cookies = accessToken match { + case Right(cookies) => cookies + case Left(error) => throw new RuntimeException(s"authenticate $loginEmail failed: $error") + } cookies.foreach { case (key, value) => val isSecure = key.startsWith("SC_") diff --git a/src/test/resources/project.conf b/src/test/resources/project.conf deleted file mode 100644 index 865414f..0000000 --- a/src/test/resources/project.conf +++ /dev/null @@ -1,2 +0,0 @@ -"idApiRoot" : "https://idapi.code.dev-theguardian.com" -testBaseUrl: "http://m.code.dev-theguardian.com" diff --git a/src/test/scala/com/gu/automation/signin/LoggingInTest.scala b/src/test/scala/com/gu/automation/signin/LoggingInTest.scala index b40e885..d08fdb0 100644 --- a/src/test/scala/com/gu/automation/signin/LoggingInTest.scala +++ b/src/test/scala/com/gu/automation/signin/LoggingInTest.scala @@ -14,20 +14,32 @@ class LoggingInTest extends WebDriverFeatureSpec with Matchers { scenario("check we can get the right cookie domains") { _ => LogIn.getCookieDomain("http://www.theguardian.com/uk") should be (".theguardian.com") - LogIn.getCookieDomain("http://localhost:9000/") should be ("localhost:9000") LogIn.getCookieDomain("https://www.theguardian.com/uk") should be (".theguardian.com") LogIn.getCookieDomain("https://m.code.dev-theguardian.com/") should be (".code.dev-theguardian.com") } + // could add another test with a fake AuthApi checking the cookies really are set + /** - * scenarioWeb handles starting and stopping the browser, you will have to declare an implicit driver as shown + * This is an end to end test that we really end up logged in. + * + * To pass it needs a local.conf containing something like + * + * "idApiRoot" : "https://idapi.code.dev-theguardian.com" + * testBaseUrl: "http://m.code.dev-theguardian.com" + * memberLogin: { + * "loginEmail" : "regidqa@gmail.com" + * "loginPassword" : "ask_gwyn!" + * } + * browser: chrome + */ scenarioWeb("check we are logged in when we have added the cookies") { implicit driver: WebDriver => - LogIn(Some("memberLogin")) -// ExamplePage.goto() + LogIn("memberLogin") + // now go to a URL where we are probably logged in driver.get(Config().getTestBaseUrl()) val userSpan = driver.findElement(By.xpath("//div[@data-component='identity-profile']")).findElement(By.className("js-profile-info")) userSpan.getText should be ("Reg Idtester")