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:

driver.get("www.geo2tag.org")

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
try:
    # Wait 10 seconds until the method will be executed
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()
# 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)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
# Create the cookies
cookie = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
driver.add_cookie(cookie)
# Output all cookies
driver.get_cookies()
# Execute the script of searching for class 'comment-user' and click on the first found element 
driver.execute_script("document.getElementsByClassName('comment-user')[0].click()")

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

or

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

Examples

https://bitbucket.org/osll/geomongo/src/84f3b62be58c2bd2e431fdf529aa0fb24d602d26/src/selenium_tests/test_GT_1340_login_resurce.py?at=master&fileviewer=file-view-default

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