CrossBrowserTesting.com

A Design, Development, Testing Blog

  • Product Overview
  • Pricing
  • Back Home
  • Free Trial
  • Design
  • Development
  • Manual Testing
  • Test Automation
  • Visual Testing

Selenium 101: How To Automate Your Login Process

January 31, 2018 By Alex McPeak 1 Comment

automate login with selenium
While learning Selenium can surely be challenging in the shift from manual to automation, starting small and making the effort to be continuously learning will help you become proficient in no time.

CrossBrowserTesting wants to help your team get started with automated testing, which is why we’re creating Selenium 101 guides to teach you the basics. By the end, every software team will want you scripting tests for them.

For the first of the series, we’re starting at the very beginning by showing you how to automate a simple login process with Selenium using Python (because it’s one of the easiest programming languages to learn first).

You’ll want to install Python, Chrome Driver, and Selenium before starting. ChromeDriver will come in the form of an executable (Windows) or a binary (Mac/Unix). Those technical details aren’t too important now, but you’ll need the file itself. You can get the latest release of ChromeDriver here. Use the following command to add the Selenium library to Python.

pip install selenium

You’ll also need to import some specific modules from Python’s Selenium library. At the bare minimum, you’ll need to do the following:

from selenium import webdriver

Finally, we need to actually start a webdriver. We can do so with only one line of code:

# if chromedriver is not in your path, you’ll need to add it here

driver = webdriver.Chrome()

Now, we can get started automating a simple task like your website’s login form. Basically what we want to do is navigate to the website, locate the username and password fields, enter your credentials, and submit them to get past your login screen. You might also want to define a “method” (something you can call repeatedly) so you can reuse it within other tests. It’ll look something like this:

def site_login():
driver.get (“URL”)
driver.find_element_by_id(“ID”).send_keys(“username”)
driver.find_element_by_id (“ID”).send_keys(“password”)
driver.find_element_by_id(“submit”).click()

For example, if we were to be automating a Facebook login, it would look something like this:

def fb_login():
driver.get (“https://www.facebook.com”)

driver.find_element_by_id(“email”).send_keys(‘fakeemail@crossbrowsertesting.com’)
driver.find_element_by_id(“pass”).send_keys(“fakepassword1”)
driver.find_element_by_id(“loginbutton”).click()

There’s more than one way to locate the elements of your web application in order to find the username and password fields, and some may not always be available to you depending on the way your webpage was written. You can find elements by ID, as we exemplified, but you can also locate them by name, XPath, CSS Selectors, and more. To read about the different ways to locate elements during your login process, read Selenium’s official documentation.

Some pages use dynamic content (meaning lots of JavaScript!). To handle this effectively, we sometimes need to wait for some event to occur. To check and make sure that the login was successful, you might want locate an element on the page you’d land on after your login form by using a wait. We’ll need a few more components from the Selenium library. With the same example, you could do it by locating an element by like this:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait WebDriverWait(driver, 10).until(EC.title_contains("home"))

The above code will wait a maximum of 10 seconds while attempting to find the “Home” button displayed when you first login to Facebook. Again, there are a few different ways to go about this including waiting for an element to be clickable, visible, or present on the page. You can read more about that here.

There you have it; this should provide a basic foundation for automating a simple login process with Selenium in Python. Take a look at the rest of our Selenium 101 series to continue on your journey to becoming a test automation master:

  • Selenium 101: Running Your Test Against Two Browsers at Once
  • Selenium 101: Navigating Waits and Sleeps
  • Selenium 101: How to Take Screenshots During Automated Tests
  • Selenium 101: Automating User Actions
  • Selenium 101: Generating Reports
  • Selenium 101: Mastering Selenium Design Patterns
  • Selenium 101: Managing Multiple Tabs

Filed Under: Test Automation Tagged With: login, selenium 101, test automation

About Alex McPeak

Alex McPeak is a Content Marketing Specialist for CrossBrowserTesting and is always looking to provide insights in testing, development, and design for the software community, appearing in outlets such as Abstracta, DZone, and Ministry of Testing. She's especially interested in writing about the latest innovations in technology and is forever #TeamiPhone.

Comments

  1. tpos says

    June 27, 2018 at 11:06 am

    with this code, how do you ‘test’ whether the login was a success or failure?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Try CrossBrowserTesting

Everything you need for testing on the web. Browsers & Devices included.


  • Grid In The Cloud
  • Simple CI Integrations
  • Native Debugging Tools
  • Real iOS and Android
  • 2050+ Browser/OS Combinations
  • Intuitive REST API

Start Testing Today

Want Testing Tips?

Want the latest tips & strategies from industry experts right to your inbox? Sign up below.
 

Join Over 600,000 Testers & Developers And Start Testing Today

Learn more Free Trial

Features

  • Live Testing
  • Selenium Automation
  • Automated Screenshots
  • Screenshot Comparison
  • Local Testing
  • Real Devices

Solutions

  • Automated Testing
  • Visual Testing
  • Manual Testing
  • Enterprise
  • Internet Explorer

Resources

  • Browsers & Devices
  • Blog
  • Webinars
  • Integrations
  • ROI Calculator

Company

  • About Us
  • Careers
  • Plans
  • Terms of use
  • Security

Support

  • Help Center
  • API Docs
  • Schedule A Demo
  • Contact Us
  • Write For Us