• Json
  • Login Action
  • Cookies
  • Local Storage

Login via browser's cookies (Selenium WebDriver) or Local Storage (JavaScript)

How to avoid repeating login action before running each test?

It depends on how this was implemented in your Web application (via cookies or Local Storage).

So, let's start with cookies. The following algorithm should be used in this case (with the help of Selenium WebDriver):

  1. Login by usual (via email and password);
  2. Save cookies of your current session;
  3. Open the site and load saved cookies.

After step 3 you will log in to the application as the same after step 1. Now I will demonstrate this with the following code snippets:

py
"""Module Utilities with different useful methods."""

...
import pickle
import json
...


class Utilities:
    """Class Utilities with different useful methods."""

    ...

    def save_cookie(self, name):
        """A method for saving cookies for domain."""

        with open(name + ".pkl", "wb") as filehandler:

            pickle.dump(self.driver.get_cookies(), filehandler)

        def load_cookie(self, name):
        """A method for loading cookies for domain."""

        with open(name + ".pkl", "rb") as cookiesfile:

            cookies = pickle.load(cookiesfile)

            for cookie in cookies:

                if isinstance(cookie.get("expiry"), float):

                    cookie["expiry"] = int(cookie["expiry"])

                self.driver.add_cookie(cookie)

Note that you can save and load cookies only for the current domain.

What if is your Web application using Local Storage? In this case, you can use the following method in Utilities class:

py
def set_local_storage(self, key, value):
    """A method for setting key in local storage."""

    self.driver.execute_script("window.localStorage.setItem('{}',{})".format(key, json.dumps(value)))

Using this method you can set up all needed pairs of key/value in Local Storage. Then you will see that you are logged in (after updating the page).

Now let's consider a more practical example -- login to Facebook via pre-saved cookies of user. First of all, we need to log in by usual and save cookies of the user:

py
"""Module for saving cookies for the domain."""

import time

import config
from utilities import Utilities


class GetCookies(Utilities):
    """Class for saving cookies for the domain."""

    def __init__(self, email="", password=""):
        """Method for saving cookies for the domain."""

        config.DOMAIN = "https://facebook.com"
        self._set_up()
        # Finding elements on the page and actions.
        self.wait_visibility_by_id("email").send_keys(email)
        self.find_by_id("pass").send_keys(password)
        self.find_by_id("loginbutton").click()
        # Saving user's cookies.
        self.save_cookie("ratmir.asanov.demo")
        time.sleep(config.DELAY2)
        self._tear_down()


if __name__ == "__main__":
    COOKIE = GetCookies(config.USER1["email"], config.USER1["password"])

Then we need to log in with using pre-saved user's cookies:

py
"""Module for loading cookies for the domain."""

import time

import config
from utilities import Utilities


class LoadCookies(Utilities):
    """Class for loading cookies for the domain."""

    def __init__(self, name_of_cookies_file):
        """Method for loading cookies for the domain."""

        config.DOMAIN = "https://www.facebook.com"
        self._set_up()
        # Loading user's pre-saved cookies.
        self.load_cookie(name_of_cookies_file)
        # Refresh the page to see the changes (user should be logged in).
        self.refresh_page()
        time.sleep(config.DELAY2 * 5)
        self._tear_down()


if __name__ == "__main__":
    COOKIE = LoadCookies("ratmir.asanov.demo")

Hope it helps you!

Click here for source code.

Used references:

Setup prerender on webpack with prerender-spa-plugin, Part 1

During developing SPA (Single Page Application) we met with a common SPA problem: although google crawler uses the last Chromium version to run JS and indexing results as HTML pages (it’s not so easy to make it work and there is still the Fix Search-related JavaScript problems list), but for other search systems and sharing in social networks SPA doesn’t work. They will get the default index.html with the default title, description, etc for all pages.

Vue.js and TypeScript Problems Collection

I believe that TypeScript will give you a lot of advantages and if you will invest some time in learning, it will definitely give you outcomes in future. So I decided to migrate small Vue.js 2 application on TypeScript. At first, I wasn't prepared to fac

Work and relax simultaneously. Cyprus for IT-workers

The island in the eastern Mediterranean - Cyprus - is known as a popular outsourcing place for IT-workers from all over the world, and in particular from Europe. What exactly attracts them on this small piece of land among the waters of the Mediterranean

Help Ukraine to stop russian aggression