The Issue

Some applications you encounter will require you to enter a verification code when logging in on a new machine. As you are starting a new session each time you launch a browser through Selenium & Watir Webdriver, you will be asked enter a verification code every time. When building out test automation, this will stop your script dead in its tracks; unless you add steps to navigate to your email, pull the verification code data, and enter it (an unnecessarily complicated solution), you’ll have difficulty continuing. This is where saving and loading cookies comes in.

The Answer

By saving our browser cookies to a temp file, we can have our Watir Webdriver script load cookies from a session where we have validated our user already; meaning the validation code will not be required. Now we can take a quick look at the save and load commands we will be using:

 

The Code

Lets look at the process of saving our cookies first. We will use Salesforce as our example. We can open a pry session by simply typing pry into our command line and hitting enter. This will allow us to execute browser commands real time and test our code. After beginning our pry session, we will launch our browser session and navigate to the Salesforce URL.

This brings us to the Login page. We will manually navigate to our email to obtain our Verification Code, and enter it in the displayed Salesforce page:

The Salesforce verification page we will need to load our cookies to.

The Salesforce verification page we will need to load our cookies to.

After logging in, we will use the following Ruby code to capture our temp file with cookies.

After running this command, our Ruby file will be saved into the file path that we were running the command line from. In our case, we were running it from our desktop, so we navigate to C:\Users\Admin\Desktop. Here we see our file:

The file containing our verified Salesforce session cookies.

The file containing our verified Salesforce session cookies.

Now we have confirmed that we have our Ruby temp file. We can now test loading our temp file to ensure that we can load our cookies from the saved session.

We will start a new session and navigate back to the Salesforce login page:

Now, before logging in, we will run our command to load the browser session:

After running this command, we are able to log in to Salesforce without hitting the verification code screen:

The automation test script can log in to Salesforce successfully after loading the cookies.

The automation test script can log in to Salesforce successfully after loading the cookies.

The Result

Now we have a way to load our browser cookies from our validated session to use in our automated test. Here is a breakdown of our results:

  • Browser opens
  • Browser navigates to the Salesforce login page
  • Command to load cookies is executed
  • We successfully log into the Salesforce home page without needing to enter a validation code.

The Takeaway 

Saving and loading your browser cookies will allow you to overcome painful blockers while building out automation with Selenium & Watir Webdriver. In our Salesforce example, a user is locked out of the site after sending too many validation requests in an hour. By loading the cookies from our validated session with our Ruby file, we are able to circumvent our blocking issue and continue to build out automation.