Testing with Selenium

What’s this?

The method of testing using software library Selenium WebDriver, it allows to perform all manual work with browser automatically. 

How does it work?

1. Connect the libraries:

from selenium import webdriver
driver = webdriver.Firefox()

2. Connect to the browser and webpage:


3. Further you can perform different actions with the page:

# Search an element by 'id'
login_form = driver.find_element_by_id('loginForm')
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
    # Wait 10 seconds until the method will be executed
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
# Select the elements
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
# Execute hover with 'menu'
# and press on 'submenu'
actions = ActionChains(driver)
# Create the cookies
cookie = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
# Output all cookies
# Execute the script of searching for class 'comment-user' and click on the first found element 

How is it implemented in our project 

1. Create the test in directory geomongo/src/tst_selenium/

2. Import the basic class from directory geomongo/src/tst_selenium/ from file basic_selenium_test.py.

3. Create a class for testing any functional and make inheriting from the basic class. Import created test class to the file main.py and add into the function main the next string:

suite.addTest(BasicSeleniumTest.parametrize(TEST_CLASS_NAME, param=host))

where TEST_CLASS_NAME – our imported class.

4. Further the checkers will be work like in unit testing. Since the basic class is inherited from TestCase, then our class also will be have all attributes and methods for unit testing.

 Running tests

1. If there is GUI

cd geomongo/src/tst_selenium/
python main.py HOST


cd geomongo && ./scripts/run_selenium_tests.sh

2. If there is only console

cd geomongo/src/tst_selenium/
xvfb-run -a python main.py HOST



Useful links

  1. https://selenium-python.readthedocs.org
  2. http://habrahabr.ru/post/248559/
  3. http://www.seleniumhq.org/docs/
ShareShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on VKEmail this to someone