-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathwebdriver_test.py
More file actions
executable file
·113 lines (90 loc) · 2.54 KB
/
Copy pathwebdriver_test.py
File metadata and controls
executable file
·113 lines (90 loc) · 2.54 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python2.7
# Install selenium client library:
#
# $ easy_install2.7 selenium
#
# You need the standalone selenium server to be running for HTMLUnit tests.
#
# Docs:
#
# http://code.google.com/p/selenium/wiki/PythonBindings
# http://seleniumhq.org/docs/03_webdriver.html
#
# API Reference:
#
# http://code.google.com/p/selenium/source/browse/trunk/py
# .../selenium/webdriver/remote/webdriver.py
# http://code.google.com/p/selenium/source/browse/trunk/py
# .../selenium/webdriver/remote/webelement.py
import re
import time
import unittest
import logging
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.exceptions import NoSuchElementException
class TimeoutException(Exception):
pass
class BrowserTestCase(unittest.TestCase):
wait_timeout = 5.0
wait_retry_in = 0.25
@classmethod
def get_chrome(cls):
browser = webdriver.Chrome()
return browser
@classmethod
def get_firefox(cls):
browser = webdriver.Firefox()
return browser
@classmethod
def get_remote(cls):
browser = webdriver.Remote(browser_name="htmlunit")
return browser
@classmethod
def setUpClass(cls):
cls.browser = cls.get_remote()
@classmethod
def tearDownClass(cls):
cls.browser.quit()
def wait_for(self, f, *args):
done = False
element = None
start = datetime.now()
while not done:
try:
element = f(*args)
except Exception, e:
logging.info(e)
if element:
done = True
else:
end = datetime.now()
if (end - start).total_seconds() > self.wait_timeout:
raise TimeoutException()
time.sleep(wait_retry_in)
return element
def page_contains(self, text):
source = self.browser.get_page_source()
return re.search(text, source)
class TestGoogleSearch(BrowserTestCase):
def test_vanity_search(self):
b = self.browser
b.get("http://google.com")
search_box = b.find_element_by_name("q")
search_box.send_keys("0xfe")
button = self.wait_for(b.find_element_by_xpath,
"//input[contains(@value, 'Search')]")
self.wait_for(button.is_displayed)
button.click()
me = self.wait_for(self.page_contains, "muthanna")
self.assertIsNotNone(me)
class TestGoogleSearchFirefox(TestGoogleSearch):
@classmethod
def setUpClass(cls):
cls.browser = cls.get_firefox()
class TestGoogleSearchChrome(TestGoogleSearch):
@classmethod
def setUpClass(cls):
cls.browser = cls.get_chrome()
if __name__ == "__main__":
unittest.main()