Bypass Login Step in selenium webdriver projects is sometimes needed to increase automation speed because many of the test scenarios start with that step and It takes time. In this article, we will learn how to do this operation.

Let’s say you are testing a website with login functionality. Assume that this step takes 10 seconds. For one test, that’s acceptable but when there are 200 test cases in our regression suite, it makes more than 30 minutes that you waste. In case you are using a cloud provider to test, you will consume your time credits easily.

So I was thinking about how to bypass it? I come up with a solution that uses cookie manipulation. Many of the websites are creating cookies. Those cookies create an expiration timestamp to your logged in user. So you access the authorized area of the website faster even after turning on/off the browser. So why not inject those cookies into our selenium driver before ever test.

For this tutorial, you should be familiar with Apache HttpClient.

The website we are testing is www.hepsiburada.com. It’s an e-commerce website and store “Session Cookie” in SFSESSIONID. Every website handles this issue differently, you need to talk your development team to understand how they store cookies.

bypass login

Let’s start…

Step 1: Make an Initial Request to Web App

We create HttpClient to make a request to the login module of the website that we are testing.

We create a HttpClientContext and CookieStore to do some stuff on cookies. Then we execute the HttpGet request. This step is not mandatory. I am just making this request to show you basic stuff about Http requests and cookies.

Step 2: Collect Created Cookies

As we configured a CookieStore previously, it will store all the cookies created by the first HttpGet command. We are just printing out to analyze them in the console. You can see there’s a cookie with SFSESSIONID. We are on the right track.

Third 3: Make a Request to Login Service

Every web page makes a POST request to authenticate. You can just open Chrome Console and take a look at the Network tab. You can capture the Request URL and Form Data’s from the Network tab.

bypass login page

bypass login step

You firstly create HttpPost object to the web service. Then create a NameValuePair to store the credentials and bind them with the request. Here’s how to do it.

Now we have all the cookies in our hands. We need to add those Cookies to our WebDriver.

Step 4: Add Cookies to Selenium WebDriver To Bypass Login

WebDriver allows you to add/remove Cookies with its internal APIs. I am going to add all cookies I got in 3rd step. Selenium Cookie object needs two parameters. First one is the name, the second one is the value.

A quick reminder, you cannot add cookies before navigating a web page. In case you want to add a cookie to a page before navigating, you’ll receive an exception. That’s why you definitely have a navigate to a page. Then add cookies and navigate once again to that page.

Now, you logged-in to the site without dealing with the Login Page. You just saved a big amount of time in testing.

For a broad implementation, you need to deal with those cookie handling issues in BeforeSuite so you can get the cookie once and use it in other tests. That depends on your implementation.

Here’s the sample source code: https://github.com/swtestacademy/ByPassLogin

Happy automated testing!
Canberk Akduygu