Skip to content

omaciel/Web-UI-Automation-with-Selenium-for-Beginners

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Web UI Automation with Selenium for Beginners

Objective

  • Cover Web UI automation using Selenium with a focus on the Python programming language
  • Learn how to easily gather Web UI information, record their actions and play them back via Selenium IDE or Scirocco
  • Write Python code to perform the same actions interactively
  • Run your code with py.test
  • Use SauceLabs to execute automated tests on multiple types of operating systems and web browser combinations.
  • Use Travis for a Continuous Integration/Delivery process

Follow Along

You can get a copy of all files used in this tutorial by cloning this repository!

git clone https://github.com/omaciel/Web-UI-Automation-with-Selenium-for-Beginners.git

Then, make sure to install all the required Python modules using pip:

pip install -r requirements.txt

From this point onward you can follow along :)

Using Jupyter Notebook

You can also follow along using a Jupyter Notebook. See this helpfull document explaining now to use a virtualenv in a Jupyter notebook

  • Install the ipython kernel module into your virtualenv
workon my-virtualenv-name  # activate your virtualenv, if you haven't already
pip install ipykernel
  • Now run the kernel "self-install" script:
python -m ipykernel install --user --name=my-virtualenv-name

Replacing the --name parameter as appropriate.

  • You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.

Using Selenium IDE

Overview

  • Firefox plugin ONLY
  • Can be tricky to install
  • Requires Firefox == 45 or older :/
  • Records all interactions with browser
  • Can replay all recordings
  • Allows for multiple tests to be recorded
  • GREAT IDE with tons of useful features
  • Exports test cases into Python code (and other formats)
  • Requires creativity to ensure that playing back activities will wait for web elements to be present

How to install it

  • Make sure that you're using an older version of Firefox
    • I recommend version 45
  • Install Selenium IDE on your system

Selenium IDE

Demo

Selenium IDE demo

Export

Selenium IDE will let you export your recording into Python code.

Selenium IDE export

Using Scirocco Recorder for Chrome

Overview

  • Easier to install
  • Works with latest Chrome browser
  • Records all interactions with browser
  • Can replay all recordings
  • Not as full fledged IDE as Selenium IDE
  • Limited range of commands
  • Limited options for exporting test cases (Python is NOT supported)

How to install it

  • Install Scirocco from the Google Web Store

Scirocco Recorder for Chrome

Demo

Scirocco demo

Export

Scirocco will let you export your recording.

Scirocco export

Using Selenium with Python

Now we will write actual python code to interact with a web browser.

Install the selenium python module

NOTE: This step can be skipped if you've cloned the repository and installed all Python dependencies.

pip install selenium

Install web driver

You will need to install a valid webdriver:

For this tutorial I have chosen to use the web driver for the chrome web browser.

NOTE: Make sure to include the ChromeDriver location in your PATH environment variable

Interact with Chrome via Python Selenium

Open a python shell:

>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> browser = webdriver.Chrome()
>>> browser.get('https://www.google.com')

>>> element = browser.find_element_by_id('lst-ib')
>>> assert element is not None
>>> element.send_keys('Red Hat' + Keys.RETURN)
>>> assert browser.title.startswith('Red Hat')

>>> browser.get('https://www.google.com')
>>> element = browser.find_element_by_id('hplogo')
>>> assert element is not None
>>> browser.execute_script("arguments[0].setAttribute('srcset', 'https://omaciel.fedorapeople
.org/ogmaciel.png')", element)
>>> browser.execute_script("arguments[0].setAttribute('height', '100%')", element)

Demo

Demo

Using Python Selenium with Unittest

Let's create a couple of automated tests using Python's unittest module:

File: test_SeleniumUnittest.py

Selenium + Python Unittest

Now, execute it:

python tests/test_SeleniumUnittest.py
testPageTitle (__main__.GoogleTestCase) ... ok
testSearchPageTitle (__main__.GoogleTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 9.557s

OK

Using Python Selenium with Pytest

Let's do the same exercise, but this time using pytest

Install pytest

NOTE: This step can be skipped if you've cloned the repository and install all Python dependencies.

pip install pytest

File: test_SeleniumPytest.py

Selenium + Pytest

Now, execute it:

pytest -v tests/test_SeleniumPytest.py
================================ test session starts =================================
platform darwin -- Python 3.6.1, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 -- /Users/omaciel/.virtualenvs/selenium-talk/bin/python3.6
cachedir: .cache
rootdir: /Users/omaciel/Dropbox/Work/pythontalk, inifile:
plugins: xdist-1.20.0, forked-0.2
collected 2 items

tests/test_SeleniumPytest.py::test_PageTitle PASSED
tests/test_SeleniumPytest.py::test_SearchPageTitle PASSED

============================== 2 passed in 5.86 seconds ==============================

Running All Tests Simultaneously

Now, let's execute all tests in multiple threads:

Install python-xdist

NOTE: This step can be skipped if you've cloned the repository and install all Python dependencies.

pip install python-xdist

Execute All Tests

pytest -n 4 

Using Python Selenium with Pytest and SauceLabs

Assuming that you have an account at SauceLabs and that you have exported your credentials via your system's environmental variables:

File: test_SeleniumSauce.py

Selenium + SauceLabs

Now, execute it:

pytest -v tests/test_SeleniumSauce.py
pytest -v tests/test_SeleniumSauce.py
================================ test session starts =================================
platform darwin -- Python 3.6.1, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 -- /Users/omaciel/.virtualenvs/selenium-talk/bin/python3.6
cachedir: .cache
rootdir: /Users/omaciel/Dropbox/Work/pythontalk, inifile:
plugins: xdist-1.20.0, forked-0.2
collected 2 items

tests/test_SeleniumSauce.py::test_PageTitle PASSED
tests/test_SeleniumSauce.py::test_SearchPageTitle PASSED

============================= 2 passed in 15.89 seconds ==============================

Demo

Demo

About

Quick introduction to using Selenium with Python for Web UI automation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published