improved tests

test code waits for selenium to be available
screenshots get named more precisely
This commit is contained in:
Joachim Lusiardi 2016-05-27 11:33:15 +02:00
parent cc414092a3
commit 3f8f30f5d1
1 changed files with 91 additions and 45 deletions

View File

@ -1,4 +1,5 @@
import unittest
import inspect
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
@ -6,55 +7,100 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
def wait_net_service(server, port, timeout=None):
""" Wait for network service to appear
@param timeout: in seconds, if None or 0 wait forever
@return: True of False, if timeout is None may return only True or
throw unhandled network exception
"""
import socket
import errno
s = socket.socket()
if timeout:
from time import time as now
# time module is needed to calc timeout shared between two exceptions
end = now() + timeout
while True:
try:
if timeout:
next_timeout = end - now()
if next_timeout < 0:
return False
else:
s.settimeout(next_timeout)
s.connect((server, port))
except socket.timeout as err:
# this exception occurs only if timeout is set
if timeout:
return False
except socket.error as err:
# catch timeout exception from underlying network library
# this one is different from socket.timeout
if type(err.args) != tuple or err[0] != errno.ETIMEDOUT:
raise
else:
s.close()
return True
class PythonOrgSearch(unittest.TestCase):
screen_shot_counter=0
def setUp(self):
self.driver = webdriver.Remote(
command_executor='http://selenium:4444/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX)
self.driver.get("http://rollerverbrauch:5000")
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "email")))
def setUp(self):
self.screen_shot_counter = 0
self.driver = webdriver.Remote(
command_executor='http://selenium:4444/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX)
self.driver.get("http://rollerverbrauch:5000")
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "email")))
def test_page_loads(self):
self.driver.get("http://rollerverbrauch:5000")
self.create_screenshot()
assert "refuel" in self.driver.title
def test_page_loads(self):
self.driver.get("http://rollerverbrauch:5000")
self.create_screenshot()
assert "refuel" in self.driver.title
def test_can_register(self):
self.driver.get("http://rollerverbrauch:5000")
self.driver.find_element_by_partial_link_text('Register').click()
self.create_screenshot()
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "submit")))
self.create_screenshot()
self.driver.find_element_by_id('email').send_keys('test@test.com')
self.driver.find_element_by_id('password').send_keys('test123')
self.driver.find_element_by_id('password_confirm').send_keys('test123')
self.create_screenshot()
self.driver.find_element_by_id('submit').click()
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "i1")))
self.create_screenshot()
def test_register_must_repeat_pwd(self):
self.driver.get("http://rollerverbrauch:5000")
self.driver.find_element_by_partial_link_text('Register').click()
self.create_screenshot()
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "submit")))
self.create_screenshot()
self.driver.find_element_by_id('email').send_keys('test@test.com')
self.driver.find_element_by_id('password').send_keys('test123')
self.create_screenshot()
self.driver.find_element_by_id('submit').click()
self.create_screenshot()
error = self.driver.find_elements_by_class_name('error')
assert error is not None
def create_screenshot(self):
print(self.__class__.__name__, inspect.stack()[1][3])
self.driver.get_screenshot_as_file('/results/%s_%s_%s.png' % (self.__class__.__name__, inspect.stack()[1][3], str(self.screen_shot_counter)))
self.screen_shot_counter += 1
def tearDown(self):
self.driver.close()
def test_can_register(self):
self.driver.get("http://rollerverbrauch:5000")
self.driver.find_element_by_partial_link_text('Register').click()
self.create_screenshot()
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "submit")))
self.create_screenshot()
self.driver.find_element_by_id('email').send_keys('test@test.com')
self.driver.find_element_by_id('password').send_keys('test123')
self.driver.find_element_by_id('password_confirm').send_keys('test123')
self.create_screenshot()
self.driver.find_element_by_id('submit').click()
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "i1")))
self.create_screenshot()
def test_register_must_repeat_pwd(self):
self.driver.get("http://rollerverbrauch:5000")
self.driver.find_element_by_partial_link_text('Register').click()
self.create_screenshot()
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "submit")))
self.create_screenshot()
self.driver.find_element_by_id('email').send_keys('test@test.com')
self.driver.find_element_by_id('password').send_keys('test123')
self.create_screenshot()
self.driver.find_element_by_id('submit').click()
self.create_screenshot()
error = self.driver.find_elements_by_class_name('error')
assert error is not None
def create_screenshot(self):
self.driver.get_screenshot_as_file('/results/screenshot_%s.png' % (str(self.screen_shot_counter)))
self.screen_shot_counter += 1
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
wait_net_service('selenium', 4444)
unittest.main()