3 Easy Tutorials for Getting Started with Selenium
  October 04, 2018

You’ve learned your first programming language. You’ve familiarized yourself with Selenium. You’ve got a great Selenium testing tool and a great team to support you. You know what to automate and what not to. But where do you start with your first test?

No matter how prepared you are, getting started with Selenium can be intimidating. But by starting small and getting some of those repetitive tests out of the way, you can get your bearings while building a maintainable and reusable automation suite.

If you work in ecommerce, there are a few test cases you might come across regularly: sign-up, login, and sign out. The good news is that these user stories are fairly easy to automate, and once you understand how to set them up, it’ll be easier to start building your own tests in the future.

SignUp

The signup process will differ from site to site, but there are a few patterns that will tend to repeat themselves. For example, many will have multiple pages with “next” buttons, dropdown menus, and form fields to fill out.

Let’s go through what a test case would look like if you were automating a signup for Gmail.

First, we want to import WebDriver as well as the “Select” command we’ll be using:

from selenium import webdriver

from selenium.webdriver.support.select import Select

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

Next, we need to instantiate an instance of ChromeDriver, the guy who will be driving our browser:

driver = webdriver.Chrome()

Now we want to define a method, so you can reuse this test, and navigate to Gmail.com where we’ll click “Create an Account” using the element id:

def site_signup():

driver.get(“https://gmail.com”)

driver.find_element_by_id(“Get Gmail”).click()

Clicking should take us to a new tab, then we’ll want wait for the page to load completely so that the form to create your Google Account appears:

WebDriverWait(driver, 10).until(EC.title_contains("Create Your Account"))

Here you find multiple form fields including First name, Last name, Username, Password, and Confirm password. This script will fill all these fields and click next:

driver.find_element_by_id (“firstName”).send_keys(“Mark”)

driver.find_element_by_id (“lastName”).send_keys(“Zuckerberg”)

driver.find_element_by_id (“username”).send_keys(“MZuckerberg”)

driver.find_element_by_id (“password”).send_keys(“Harvard2004”)

driver.find_element_by_id (“confirmpassword”).send_keys(“Harvard2004”)

driver.find_element_by_id (“Next”).click()

Again, this will take to to a new page where you’ll want to wait until it shows all the form fields for “Create Your Account”. It will then request you fill out more form fields. These will have drop down boxes:

def droppable()

select_fr = Select(driver.find_element_by_id("Month")) select_fr.select_by_index(May)

select_fr = Select(driver.find_element_by_id("Day")) select_fr.select_by_index(14)

select_fr = Select(driver.find_element_by_id("Year")) select_fr.select_by_index(1984)

select_fr = Select(driver.find_element_by_id("Gender")) select_fr.select_by_index(Male)

driver.find_element_by_id(“Next”).click()

From here, you agree to terms and conditions, which means you have to scroll down until you see the “I agree” button:

element = driver.find_element_by_id('I agree') element.location_once_scrolled_into_view

driver.find_element_by_id(“I agree”).click()

That’s it! If you follow these steps, it should create a Gmail account.

Log In

Once you get some of those trickier commands out of the way with sign up, these test cases should be easy. As we mentioned, you’ll see how you build on your previous learning to create new tests, such as with this login that uses find element by id, send keys, and click that we just used.

This is what a login with Facebook might look like (we hope that Mark doesn’t actually use the same password for Gmail and Facebook...and we also hope it's more secure than Harvard2004):

def site_login():

driver.get (“facebook.com”)

driver.find_element_by_id(“ID”).send_keys(“MZuckerberg”)

driver.find_element_by_id (“ID”).send_keys(“Harvard2004”)

driver.find_element_by_id(“submit”).click()

LogOut

Logouts are usually pretty simple as well, but you will probably have to find settings or menu through a drop down. This is what the test would look like if you wanted to log out of Facebook:

def site_logout():

driver.get (“facebook.com”)

driver.find_element_by_id(“userNavigationLabel”).click()

driver.find_element_by_partial_link_text(“Log Out”).click()

You can see here that we switched up the way we located the element (by name instead of id) because of the nature of the dropdown window. Keep in mind there are multiple ways to locate an element if one doesn’t work out, including:

  • ID
  • Name
  • XPath
  • Link Text
  • Partial Link Text
  • Tag Name
  • Class Name
  • CSS Selector

Conclusion

Even if you don’t own a site that includes sign up, login, or log out, practicing tests similar to these will teach you some of the foundations of Selenium, popular commands, and good practices so you can continue to build and improve your Selenium test automation strategy.

To find more common commands that you may run into when building out your Selenium tests, check out our full Selenium toolkit, which includes more Selenium tutorials.