Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from guardian/simplify-login-signature
Browse files Browse the repository at this point in the history
Simplify login signature
  • Loading branch information
johnduffell committed Jul 22, 2014
2 parents 507d7c3 + 37b3000 commit 8bb74e7
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 41 deletions.
11 changes: 5 additions & 6 deletions 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)
See the example code in LoggingInTest
2 changes: 1 addition & 1 deletion build.sbt
Expand Up @@ -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"
)

Expand Down
59 changes: 59 additions & 0 deletions src/main/scala/com/gu/automation/signin/LogIn.scala
@@ -0,0 +1,59 @@
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()(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)
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, 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_")
val cookie = new Cookie(key, value, loginDomain, "/", null, isSecure, isSecure)
driver.manage().addCookie(cookie)
}
}

}
34 changes: 0 additions & 34 deletions src/main/scala/com/gu/automation/support/LoggingIn.scala

This file was deleted.

49 changes: 49 additions & 0 deletions src/test/scala/com/gu/automation/signin/LoggingInTest.scala
@@ -0,0 +1,49 @@
package com.gu.automation.signin

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 WebDriverFeatureSpec with Matchers {

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("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

/**
* 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("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")
}
}

}

0 comments on commit 8bb74e7

Please sign in to comment.