This repository has been archived by the owner on May 9, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from guardian/simplify-login-signature
Simplify login signature
- Loading branch information
Showing
5 changed files
with
114 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/test/scala/com/gu/automation/signin/LoggingInTest.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} | ||
|
||
} |