On 5 September 2016 I gave a talk and a live demonstration of Selenium IDE and the Selenium Driver of Python at PyWeb-IL.
These are some notes for the presentation.
We talked about Selenium IDE the plugin for Firefox.
Mentioned the FirePath plugin to interrogate a DOM using CSS selectors or XPath.
I've also mentioned the Accessibility Developer Tools of Chrome that has nothing to do with Selenium, but a recent interview I made, that will be published on the CMOS podcast inspired me to talk about.
We looked at the test for the web site of Jewish Diaspora house. It is not written well, but it can give a few ideas. Especially to people who listened to the talk.
examples/python/pyweb-il-56/test_dbs.py
from __future__ import print_function, division
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
import unittest
class TestSearch(unittest.TestCase):
def test_search(self):
url = 'http://dbs.bh.org.il/'
driver = webdriver.Chrome()
driver.get(url)
assert driver.title == 'BHS'
# check the GitHub ribbon
ribbon = driver.find_element_by_class_name('github-fork-ribbon')
assert ribbon
assert ribbon.get_attribute('href') == 'https://github.com/Beit-Hatfutsot/dbs-front/blob/dev/README.md'
assert re.search(r'My Jewish Story', driver.page_source)
#check the popup
assert re.search(r'Welcome!', driver.page_source)
# hide the popup
button = driver.find_element_by_class_name('beta-notification__close')
assert button
button.click()
assert not re.search(r'Welcome!', driver.page_source)
# At first I directly selected the element with the 'title' class and the 'icon' class,
# but then I found out that each menu item has both of those.
# Then I went to the parent elements and there I found uniqueness.
museum = driver.find_element_by_class_name('museum')
assert museum.text == 'Museum'
# Museum link
elem = museum.find_element_by_class_name('title')
assert elem.is_displayed()
assert elem.text == 'Museum'
# After a lot of sweating, I found that there is an 'icon' class which is replacing the 'title'
icon = museum.find_element_by_class_name('icon')
assert not icon.is_displayed()
ActionChains(driver).move_to_element(elem).perform()
time.sleep(1)
elem = museum.find_element_by_class_name('title')
#print(elem)
assert not elem.is_displayed()
icon = museum.find_element_by_class_name('icon')
assert icon.is_displayed()
# time.sleep(5)
# TODO: click on the element when it is not in hover mode
icon.click()
assert driver.title == 'Home | Beit Hatfutsot'
assert driver.current_url == 'http://www.bh.org.il/'
driver.back()
assert driver.title == 'BHS'
assert driver.current_url == url
#language_selector = driver.find_element_by_css_selector('img[alt="Select Language"]')
language_selector = driver.find_element_by_class_name('language-icon')
#print(language_selector)
language_selector.click()
# TODO: check the Hebrew version
time.sleep(3)
assert re.search(r'My Jewish Story', driver.page_source)
driver.back()
time.sleep(3)
# TODO: hmm, why does this match while the browser still show the Hebrew version?
assert re.search(r'My Jewish Story', driver.page_source)
#time.sleep(5)
#print(driver.content)
#assert True
driver.close()
if __name__ == "__main__":
unittest.main()
We also looked at the web site of the Wellington City Council that has responsive-design, and we had a script to test the responsiveness, though we have not looked at it.
examples/python/pyweb-il-56/test_wellington.py
from __future__ import print_function, division
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import re
import unittest
class TestSearch(unittest.TestCase):
def test_search(self):
url = 'http://wellington.govt.nz/'
driver = webdriver.Chrome()
driver.get(url)
size0 = driver.get_window_size()
print(size0)
#print(driver.get_window_size()) # {u'width': 1050, u'height': 829}
#print(driver.get_window_position()) # {u'y': 45, u'x': 22}
driver.maximize_window()
size1 = driver.get_window_size() # {u'width': 1050, u'height': 873}
#print(size1)
time.sleep(5)
self.assertEqual(driver.title, 'Wellington City Council')
header = driver.find_element_by_class_name('nav-header-aux')
#print(header.text)
assert header.is_displayed()
a = header.find_element_by_css_selector('a[href="/do-it-online"]')
self.assertEqual(a.text, 'Do it Online')
driver.set_window_size(895, size1['height']) # width, height
time.sleep(5)
header = driver.find_element_by_class_name('nav-header-aux')
#print(header.text)
assert not header.is_displayed()
menu = driver.find_element_by_class_name('menu-title')
assert not menu.is_displayed()
driver.set_window_size(749, size1['height']) # width, height
time.sleep(5)
header = driver.find_element_by_class_name('nav-header-aux')
#print(header.text)
assert not header.is_displayed()
menu = driver.find_element_by_class_name('menu-title')
assert menu.is_displayed()
#a = driver.find_element_by_css_selector('a[href="/do-it-online"]')
#print(a.text)
driver.set_window_size(300, size1['height']) # width, height
time.sleep(5)
driver.close()
if __name__ == "__main__":
unittest.main()
I've included these examples so you can learn from my mistakes. These were both examples in progress that should be further improved for real use.
Additional comments by Miki Tebeka
An example on running selenium "headless" (without screen) Dockerfile and docker-test