OLD | NEW |
---|---|
(Empty) | |
1 """The testing Environment class.""" | |
2 | |
3 | |
4 import time | |
5 from selenium import webdriver | |
6 from selenium.webdriver.chrome.options import Options | |
7 import xml.etree.ElementTree as ElementTree | |
8 | |
9 | |
10 class Environment: | |
11 """Handles the testing Environment. """ | |
vabr (Chromium)
2014/05/14 15:16:09
I don't understand what is meant by "Handles" here
rchtara
2014/05/15 09:39:11
Done.
| |
12 | |
13 def __init__(self, chrome_path, chromedriver_path, | |
14 profile_path, passwords_path): | |
15 """Creates a new testing Environment. | |
16 | |
17 Args: | |
18 chrome_path: The chrome binary file. | |
19 chromedriver_path: The chromedriver binary file. | |
20 profile_path: The testing profile folder. | |
21 passwords_path: The usernames and passwords file. | |
22 """ | |
23 options = Options() | |
24 options.add_argument("enable-automatic-password-saving") | |
25 # Chrome path. | |
26 options.binary_location = chrome_path | |
27 # Testing profile path. | |
28 options.add_argument("user-data-dir=%s" % profile_path) | |
29 print "user-data-dir=%s" % profile_path | |
vabr (Chromium)
2014/05/14 15:16:09
Please do not leave debugging statements in the co
rchtara
2014/05/15 09:39:11
Done.
| |
30 # The webdriver. | |
31 self.driver = webdriver.Chrome(chromedriver_path, 0, options) | |
vabr (Chromium)
2014/05/14 15:16:09
optional nit: would be nice to explain the "0" pas
rchtara
2014/05/15 09:39:11
Done.
| |
32 # The password internals window. | |
33 self.internals_window = self.driver.current_window_handle | |
34 # The Website window. | |
35 self.websitewindow = None | |
36 # The Websites list. | |
37 self.websites = [] | |
38 # An xml tree filled with logins and passwords. | |
39 self.passwords_tree = None | |
40 if passwords_path != None: | |
41 self.passwords_tree = ElementTree.parse(passwords_path).getroot() | |
42 # The Websites list for which we expect the test to be working. | |
43 self.working_tests = [] | |
44 | |
45 def AddWebsite(self, | |
46 website, | |
47 failing=False): | |
48 """Adds a Website to the testing Environment. | |
49 | |
50 Args: | |
51 name: The name of the website. | |
vabr (Chromium)
2014/05/14 15:16:09
There is no 'name' among the arguments. Did you me
rchtara
2014/05/15 09:39:11
Done.
| |
52 username_not_auto: Don't check that the state of the DOM is equal to the | |
vabr (Chromium)
2014/05/14 15:16:09
This is also not in the arguments list.
rchtara
2014/05/15 09:39:11
Done.
| |
53 expected value for the username. | |
54 failing: Test is expected to fail. | |
vabr (Chromium)
2014/05/14 15:16:09
Does this rather mean that the test should not be
rchtara
2014/05/15 09:39:11
Done.
| |
55 | |
56 Returns: | |
vabr (Chromium)
2014/05/14 15:16:09
The function does not appear to return anything.
rchtara
2014/05/15 09:39:11
Done.
| |
57 A new new Website. | |
vabr (Chromium)
2014/05/14 15:16:09
typo: new new
rchtara
2014/05/15 09:39:11
Done.
| |
58 """ | |
59 website.test_environment = self | |
60 website.driver = self.driver | |
61 if self.passwords_tree != None: | |
vabr (Chromium)
2014/05/14 15:16:09
I believe you can leave out '!= None', as 'None' t
rchtara
2014/05/15 09:39:11
I got this warning when I tied to replace this Non
| |
62 if website.username == None: | |
63 username_tag = ( | |
64 self.passwords_tree.find( | |
65 ".//*[@name='%s']/username" % website.name)) | |
66 if (username_tag != None): | |
67 website.username = username_tag.text | |
68 if website.password == None: | |
69 password_tag = ( | |
70 self.passwords_tree.find( | |
71 ".//*[@name='%s']/password" % website.name)) | |
72 if (password_tag != None): | |
73 website.password = password_tag.text | |
74 | |
75 self.websites.append(website) | |
76 if failing == False: | |
vabr (Chromium)
2014/05/14 15:16:09
'failing == False' -> 'not failing'
rchtara
2014/05/15 09:39:11
Done.
| |
77 self.working_tests.append(website.name) | |
78 | |
79 def RemoveAllPasswords(self, websites): | |
80 """Removes the stored passwords for all Websites. | |
81 | |
82 Args: | |
83 websites: The Websites which the passwords are going to be removed. | |
84 """ | |
85 print "\nRemoveAllPasswords\n" | |
86 self.driver.get("chrome://settings/passwords") | |
87 self.driver.switch_to_frame("settings") | |
88 | |
89 for website in websites: | |
90 urls = self.GetURLs() | |
vabr (Chromium)
2014/05/14 15:16:09
I think you can pull
urls = self.GetURLs()
before
rchtara
2014/05/15 09:39:11
The urls list is changing every time.
| |
91 website.RemoveAllPasswords(urls) | |
92 | |
93 def GetURLs(self): | |
94 """Gets all URLs of the saved passwords in the chrome://settings/passwords. | |
95 | |
96 Returns: | |
97 A list of the URLs. | |
98 """ | |
99 arr = self.driver.find_elements_by_css_selector( | |
vabr (Chromium)
2014/05/14 15:16:09
What does 'arr' mean? Could you please use somethi
rchtara
2014/05/15 09:39:11
Done.
| |
100 "#saved-passwords-list .deletable-item") | |
101 urls = [] | |
102 for entry in arr: | |
103 urls.append(entry.find_element_by_class_name("url").text) | |
104 return urls | |
105 | |
106 def NewWindow(self): | |
107 """Opens a new tab and switch to it.""" | |
108 if self.websitewindow == None: | |
109 self.driver.get("https://google.com") | |
110 # There is no straightforward way to open a new tab with chromedriver. | |
111 # One of work-around is to go to a website, insert a link that is going | |
vabr (Chromium)
2014/05/14 15:16:09
nit: One of -> One
rchtara
2014/05/15 09:39:11
Done.
| |
112 # to be opened in a new tab, click on it. To avoid that the chrome popup | |
113 # blocker blocks the new tab, we need to choose a trusted website which | |
114 # is google.com. | |
115 # Links for local resources (chrome://*) are blocked. So this why the | |
vabr (Chromium)
2014/05/14 15:16:09
nit: this -> this is
rchtara
2014/05/15 09:39:11
Done.
| |
116 # new tab is going to be for google.com and then the browser is asked | |
117 # via chromedriver to go to chrome://settings/passwords . | |
vabr (Chromium)
2014/05/14 15:16:09
Seems out of date: this function does not open chr
rchtara
2014/05/15 09:39:11
Done.
| |
118 a = self.driver.execute_script( | |
119 "var d=document;var " | |
vabr (Chromium)
2014/05/14 15:16:09
Please make the line wrapping be one line per stat
rchtara
2014/05/15 09:39:11
Done.
| |
120 "a=d.createElement('a');a.target='_blank';a.href=arguments[0];" | |
121 "a.innerHTML='.';d.body.appendChild(a);return" | |
122 " a;", | |
123 "https://google.com") | |
124 | |
125 a.click() | |
126 time.sleep(1) | |
127 | |
128 self.websitewindow = self.driver.window_handles[-1] | |
129 | |
130 self.driver.switch_to_window(self.websitewindow) | |
131 | |
132 def TestList(self, websites): | |
133 """Runs the tests on many Websites. | |
134 Args: | |
135 websites: A list of Websites that are going to be tested. | |
136 Raises: | |
137 Exception: An exception is raised if the tests fail. | |
138 """ | |
139 self.RemoveAllPasswords(websites) | |
140 self.NewWindow() | |
141 for website in websites: | |
142 website.WrongLoginTest() | |
143 website.SuccessfulLoginTest() | |
144 website.SuccessfulLoginWithAutofilledPasswordTest() | |
145 | |
146 self.RemoveAllPasswords(websites) | |
147 for website in websites: | |
148 website.SuccessfulLoginAfterDeletionTest() | |
149 | |
150 def AllTests(self): | |
151 """Runs the tests on all the websites. | |
152 Raises: | |
153 Exception: An exception is raised if the tests fail. | |
154 """ | |
155 self.TestList(self.websites) | |
156 | |
157 def WorkingTests(self): | |
158 """Runs the tests on all the working websites. | |
159 Raises: | |
160 Exception: An exception is raised if the tests fail. | |
161 """ | |
162 self.Test(self.working_tests) | |
163 | |
164 def Test(self, tests): | |
165 """Runs the tests on many websites. | |
166 Args: | |
167 tests: A list of the names of the websites that are going to be tested. | |
168 Raises: | |
169 Exception: An exception is raised if the tests fail. | |
170 """ | |
171 websites = [] | |
172 for website in self.websites: | |
173 if website.name in tests: | |
174 websites.append(website) | |
175 self.TestList(websites) | |
176 | |
177 def Quit(self): | |
178 """Close the chromedriver.""" | |
179 self.driver.quit() | |
OLD | NEW |