Python: Selenium Tests

(Last Updated On: )

Selenium is a great way to test your UI. It is compatible with different browsers. I will show you two.

Gecko Driver Installation:

Make sure you are using latest version. At the time of this writing it is 0.19.0.

wget https://github.com/mozilla/geckodriver/releases/download/v0.19.0/geckodriver-v0.19.0-linux64.tar.gz
sudo tar -xvzf geckodriver-v0.19.0-linux64.tar.gz
sudo chmod +x geckodriver
cp geckodriver /usr/local/bin/
sudo cp geckodriver /usr/local/bin/

You can use phantomjs, firefox, chrome, etc.

PhantomJS Installation:

sudo mv phantomjs-2.1.1-linux-x86_64.tar.bz2 /usr/local/share/.
cd /usr/local/share/
sudo tar xjf phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-2.1.1-linux-x86_64 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin/phantomjs

Firefox Installation:

sudo apt-get update
wget https://ftp.mozilla.org/pub/firefox/releases/50.0/linux-x86_64/en-US/firefox-50.0.tar.bz2
sudo tar -xjf firefox-50.0.tar.bz2
sudo rm -rf /opt/firefox
sudo mv firefox /opt/firefox
sudo mv /usr/bin/firefox /usr/bin/firefoxold
sudo ln -s /opt/firefoxX/firefox /usr/bin/firefox

Firefox Headless Installation:

sudo apt-get install xvfb
pip3 install pyvirtualdisplay==0.2.1

Selenium Installation:

pip3 install selenium==3.6.0

PyUnit Selenium Test Examples:

Setup:

#If you are using headless firefox
from pyvirtualdisplay import Display
#The selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import unittest, os, time

class MySeleniumTests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.server_url = "http://" + os.getenv("WEBSITE_URL", 'localhost:5000')

    def setUp(self):
        #if you are using firefox headless browser
        display = Display(visible=0, size=(1080, 720))
        display.start()
		
        #Firefox selenium driver.
        self.driver = webdriver.Firefox()
		
        #PhantomJS selenium driver
        self.driver = webdriver.PhantomJS()
		
        self.driver.implicitly_wait(60)
        self.driver.set_page_load_timeout(60)
        self.driver.set_window_size(1080, 720)
        self.base_url = self.server_url

        self.driver.get(self.base_url + "/")
		
        #If your site has a login then you need to set the username and password first.
        self.driver.find_element_by_id("user").clear()
        self.driver.find_element_by_id("user").send_keys(USERNAME)
        self.driver.find_element_by_id("password").clear()
        self.driver.find_element_by_id("password").send_keys(PWD)
        self.driver.find_element_by_id("submit").click()
        time.sleep(1)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

Test Title:

self.driver.get(self.server_url)
self.assertIn("MySite", self.driver.title)

Find Class:

self.assertTrue(WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "my-awesome-class"))))

Find ID:

self.assertTrue(WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.ID, "myId"))))

Find Partial Text:

self.driver.find_element_by_partial_link_text("My Text On Page")

Find Element Contains Text:

self.assertTrue('MyText' in self.driver.find_element_by_id('container').text)

Click Element:

self.driver.find_element_by_id('myId').click()

Wait Element To Show:

self.assertTrue(WebDriverWait(self.driver, 10).until(EC.text_to_be_present_in_element((By.ID, 'MyID'), "Text To See")))

xPath Click Second Element:

self.driver.find_element_by_xpath("(//div[@class='my-awesome-class'])[1]").click()

Clear Input:

self.driver.find_element_by_id("myId").clear()

Send Data To Input:

self.driver.find_element_by_id("myId").send_keys('My New Data')