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

Commit

Permalink
update LogIn to determine the domain automatically and have an easier…
Browse files Browse the repository at this point in the history
… to understand syntax
  • Loading branch information
johnduffell committed Jul 21, 2014
1 parent 8659f64 commit 1bcde38
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 42 deletions.
50 changes: 50 additions & 0 deletions 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)
}
}

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

This file was deleted.

1 change: 1 addition & 0 deletions src/test/resources/project.conf
@@ -1 +1,2 @@
"idApiRoot" : "https://idapi.code.dev-theguardian.com"
testBaseUrl: "http://m.code.dev-theguardian.com"
37 changes: 31 additions & 6 deletions 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")
}
}

}

0 comments on commit 1bcde38

Please sign in to comment.