-
Notifications
You must be signed in to change notification settings - Fork 2
/
testFrameWork.py
72 lines (60 loc) · 2.77 KB
/
testFrameWork.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# All the imports we do
import unittest
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
# In order to move the stuff below in their own files,
# use from NameOfFile import *, or NameOfFile import class
# Stuff that needs to go in the configuration
settings = dict(
driverpath = './chromedriver',
baseurl = 'https://www.google.nl',
maxviablewaittime = 5
)
# Stuff that needs to go in the fixtures where settings are imported
max_viable_wait_time = settings['maxviablewaittime'] # A test fails when page loading takes longer than this wait time
chrome_driver_path = settings['driverpath']
URL = settings['baseurl']
# Definition of classes of tests
class basicTests(unittest.TestCase):
# Start the browser, go to the URL, perhaps do the login
def setUp(self):
self.driver = webdriver.Chrome(chrome_driver_path)
self.driver.get(URL)
self.driver.maximize_window()
# This counts as one test, even if there are numerous asserts in this test
def testCheckTitle(self):
self.assertTrue('Google' in self.driver.title, 'Nope, Google does not appear in the page title')
def testSendSearchText(self):
try:
element = EC.visibility_of_element_located((By.XPATH, '//*[@id="lst-ib"]'))
WebDriverWait(self.driver, max_viable_wait_time).until(element)
except TimeoutException:
self.assertTrue(False, 'Page did not load in time')
element = self.driver.find_element_by_xpath('//*[@id="lst-ib"]')
element.clear()
element.send_keys('YoungCapital rulez')
element.send_keys(Keys.RETURN)
try:
element = EC.visibility_of_element_located((By.XPATH, '//*[@id="resultStats"]'))
WebDriverWait(self.driver, max_viable_wait_time).until(element)
except TimeoutException:
self.assertTrue(False, 'Results did not show in time')
element = self.driver.find_element_by_xpath('//*[@id="resultStats"]')
response = element.text
self.assertTrue('resultaten' in response, 'Not the expected response')
responseSet = response.split(' ') # split the response at the spaces
print(responseSet[1] + " resultaten .. ")
# End the session here. More that one test can have run at this time
def tearDown(self):
self.driver.quit()
# Here the actual run-the-tests
suite = unittest.TestSuite()
suite.addTest(basicTests('testCheckTitle'))
suite.addTest(basicTests('testSendSearchText'))
runner = unittest.TextTestRunner(verbosity = 2)
result = runner.run(suite)