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.
update LogIn to determine the domain automatically and have an easier…
… to understand syntax
- Loading branch information
1 parent
8659f64
commit 1bcde38
Showing
4 changed files
with
82 additions
and
42 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 |
---|---|---|
@@ -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) | ||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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 +1,2 @@ | ||
"idApiRoot" : "https://idapi.code.dev-theguardian.com" | ||
testBaseUrl: "http://m.code.dev-theguardian.com" |
37 changes: 31 additions & 6 deletions
37
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 |
---|---|---|
@@ -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") | ||
} | ||
} | ||
|
||
} |