Boost you autotests with fast authorization

Luke, use you force… If you can.

TL;DR

If you application allow — use cookie for authorization in test.

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

Let’s rock!

In some time during development and passing 1000000 tests you always come to optimization.

The first and main point for optimization — application authorization.

From start I’m using one method that authorize me in application via UI:

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();
}

And it work. It just works. This method took me about 8–10 seconds. And almost each test required authorization.

Imagine, if count of test equals 100? OR 1000? You’re get huge time number.

Optimization. Round 1.

For first we added method to that automatically login user into application. But, for this I need user authorization token that return me from API call. But it’s extra API call that required some time.

public void authorizeWithToken(final User user) {
String token = api.getAutoLoginToken(user);
webDriver.get("https://my-supa-dupa-app.com/auto-login/" + token);
}

In this case for first browser navigate to auto login URL and then redirect me to authorized page in application.

So in this case my login action took about 6–8 seconds. Not so much. But we have some progress.

Optimization. Round 2.

As out application store authorization in cookie, so let’s try to use them. In this scenario — for first I had to open for first, and then add cookies.

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("token", accessToken.getUserId());
webDriver.manage().addCookie(token);
webDriver.manage().addCookie(userId);
}

In this case we again navigate to index page, wait for page load, then add cookies and navigate to target page.

I don’t know why, but Selenium add cookies with some delay (slowly). So in this case index page loading and authorization took me about 7–9 seconds.

That’s was a good plan, but something goes wrong :C

Optimization. Round 3.

After some research and chatting in automation slack chat I’ve got a new idea. And it was related to cookie again.

Two point related to this idea:

  1. Open as light as possible application page
  2. Try to add cookie using JS not build-in selenium method.

As I research the lightest pages are /favicon.png or /robots.txt because no content there. But some page can be missing. I’m using /robots.txt because we have this page and it’s contains few lines of text— super lightweight page!

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);
}
}

So. As adding cookie via JS faster that selenium, open light page — fast. As result I got about 2–4 seconds for authorizing into application and open target page. Impressive. That’s exactly what I need.

Always try to find better solution and you will find it! =_=