Boost you autotests with fast authorization

Aliaksandr Rasolka
Feb 1, 2018 · 3 min read

TL;DR

“Long exposure of two cyclists in helmets racing on a road” by Markus Spiske on Unsplash

Let’s rock!

public void authorizeWithUi(final User user) {
webDriver.get("https://my-supa-dupa-app.com/login");
emailField.sendKeys(user.getEmail());
passwordField.sendKeys(user.getPassword());
loginButton.click();
}
public void authorizeWithToken(final User user) {
String token = api.getAutoLoginToken(user);
webDriver.get("https://my-supa-dupa-app.com/auto-login/" + token);
}
public void authorizeWithCookie(final AccessToken accessToken) {
webDriver.get("https://my-supa-dupa-app.com");
Cookie token = new Cookie("token", accessToken.getToken());
Cookie userId = new Cookie("userId", accessToken.getUserId());
webDriver.manage().addCookie(token);
webDriver.manage().addCookie(userId);
}
public class Authorization {
public void authorizeWithCookie(final AccessToken accessToken) {
open("https://my-supa-dupa-app.com/robots.txt");

setItemInCookie("token", accessToken.getToken());
setItemInCookie("userId", accessToken.getUserId());
}
private void setItemInCookie(final String item, final String value) {
final String jsCode = String.format(
"document.cookie='%s=%s';",
item,
value);
executeJavaScript(jsCode);
}
private void executeJavaScript(final String jsCode) {
((JavascriptExecutor) webDriver).executeScript(jsCode);
}
private void open(final String url) {
webDriver.get(url);
}
}

Aliaksandr Rasolka

Written by

Automation QA from Belarus

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade