Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """WebsiteTest testing class.""" | 5 """WebsiteTest testing class.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 import time | 8 import time |
| 9 | 9 |
| 10 from selenium.webdriver.common.action_chains import ActionChains | 10 from selenium.webdriver.common.action_chains import ActionChains |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 """ | 23 """ |
| 24 | 24 |
| 25 # Possible values of self.autofill_expectation. | 25 # Possible values of self.autofill_expectation. |
| 26 AUTOFILLED = 1 # Expect password and username to be autofilled. | 26 AUTOFILLED = 1 # Expect password and username to be autofilled. |
| 27 NOT_AUTOFILLED = 2 # Expect password and username not to be autofilled. | 27 NOT_AUTOFILLED = 2 # Expect password and username not to be autofilled. |
| 28 | 28 |
| 29 # The maximal accumulated time to spend in waiting for website UI | 29 # The maximal accumulated time to spend in waiting for website UI |
| 30 # interaction. | 30 # interaction. |
| 31 MAX_WAIT_TIME_IN_SECONDS = 200 | 31 MAX_WAIT_TIME_IN_SECONDS = 200 |
| 32 | 32 |
| 33 def __init__(self, name, username_not_auto=False): | 33 def __init__(self, name, username_not_auto=False, password_not_auto=True): |
|
vabr (Chromium)
2015/05/18 16:13:34
Should the default value be False?
melandory
2015/05/19 08:06:34
Done.
| |
| 34 """Creates a new WebsiteTest. | 34 """Creates a new WebsiteTest. |
| 35 | 35 |
| 36 Args: | 36 Args: |
| 37 name: The website name, identifying it in the test results. | 37 name: The website name, identifying it in the test results. |
| 38 username_not_auto: Expect that the tested website fills username field | 38 username_not_auto: Expect that the tested website fills username field |
| 39 on load, and Chrome cannot autofill in that case. | 39 on load, and Chrome cannot autofill in that case. |
| 40 password_not_auto: Expect that the tested website fills password field | |
| 41 on load, and Chrome cannot autofill in that case. | |
| 40 """ | 42 """ |
| 41 self.name = name | 43 self.name = name |
| 42 self.username = None | 44 self.username = None |
| 43 self.password = None | 45 self.password = None |
| 44 self.username_not_auto = username_not_auto | 46 self.username_not_auto = username_not_auto |
| 47 self.password_not_auto = password_not_auto | |
| 45 | 48 |
| 46 # Specify, whether it is expected that credentials get autofilled. | 49 # Specify, whether it is expected that credentials get autofilled. |
| 47 self.autofill_expectation = WebsiteTest.NOT_AUTOFILLED | 50 self.autofill_expectation = WebsiteTest.NOT_AUTOFILLED |
| 48 self.remaining_seconds_to_wait = WebsiteTest.MAX_WAIT_TIME_IN_SECONDS | 51 self.remaining_seconds_to_wait = WebsiteTest.MAX_WAIT_TIME_IN_SECONDS |
| 49 # The testing Environment, if added to any. | 52 # The testing Environment, if added to any. |
| 50 self.environment = None | 53 self.environment = None |
| 51 # The webdriver from the environment. | 54 # The webdriver from the environment. |
| 52 self.driver = None | 55 self.driver = None |
| 53 | 56 |
| 54 # Mouse/Keyboard actions. | 57 # Mouse/Keyboard actions. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 element = self._ReturnElementIfDisplayed(selector) | 191 element = self._ReturnElementIfDisplayed(selector) |
| 189 return element | 192 return element |
| 190 | 193 |
| 191 # Form actions. | 194 # Form actions. |
| 192 | 195 |
| 193 def FillPasswordInto(self, selector): | 196 def FillPasswordInto(self, selector): |
| 194 """Ensures that the selected element's value is the saved password. | 197 """Ensures that the selected element's value is the saved password. |
| 195 | 198 |
| 196 Depending on self.autofill_expectation, this either checks that the | 199 Depending on self.autofill_expectation, this either checks that the |
| 197 element already has the password autofilled, or checks that the value | 200 element already has the password autofilled, or checks that the value |
| 198 is empty and replaces it with the password. | 201 is empty and replaces it with the password. If self.password_not_auto |
| 202 is true, it skips the checks and just overwrites the value with the | |
| 203 password. | |
| 199 | 204 |
| 200 Args: | 205 Args: |
| 201 selector: The CSS selector for the filled element. | 206 selector: The CSS selector for the filled element. |
| 202 | 207 |
| 203 Raises: | 208 Raises: |
| 204 Exception: An exception is raised if the element's value is different | 209 Exception: An exception is raised if the element's value is different |
| 205 from the expectation. | 210 from the expectation. |
| 206 """ | 211 """ |
| 207 | 212 |
| 208 logging.log(SCRIPT_DEBUG, "action: FillPasswordInto %s" % selector) | 213 logging.log(SCRIPT_DEBUG, "action: FillPasswordInto %s" % selector) |
| 209 password_element = self.WaitUntilDisplayed(selector) | 214 password_element = self.WaitUntilDisplayed(selector) |
| 210 | 215 |
| 211 # Chrome protects the password inputs and doesn't fill them until | 216 # Chrome protects the password inputs and doesn't fill them until |
| 212 # the user interacts with the page. To be sure that such thing has | 217 # the user interacts with the page. To be sure that such thing has |
| 213 # happened we perform |Keys.CONTROL| keypress. | 218 # happened we perform |Keys.CONTROL| keypress. |
| 214 action_chains = ActionChains(self.driver) | 219 action_chains = ActionChains(self.driver) |
| 215 action_chains.key_down(Keys.CONTROL).key_up(Keys.CONTROL).perform() | 220 action_chains.key_down(Keys.CONTROL).key_up(Keys.CONTROL).perform() |
| 216 | 221 |
| 217 self.Wait(2) # TODO(vabr): Detect when autofill finished. | 222 self.Wait(2) # TODO(vabr): Detect when autofill finished. |
| 218 if self.autofill_expectation == WebsiteTest.AUTOFILLED: | 223 if not self.password_not_auto: |
| 219 if password_element.get_attribute("value") != self.password: | 224 if self.autofill_expectation == WebsiteTest.AUTOFILLED: |
| 220 raise Exception("Error: autofilled password is different from the saved" | 225 if password_element.get_attribute("value") != self.password: |
| 221 " one on website: %s" % self.name) | 226 raise Exception("Error: autofilled password is different from the" |
| 222 elif self.autofill_expectation == WebsiteTest.NOT_AUTOFILLED: | 227 "saved one on website: %s" % self.name) |
| 223 if password_element.get_attribute("value"): | 228 elif self.autofill_expectation == WebsiteTest.NOT_AUTOFILLED: |
| 224 raise Exception("Error: password value unexpectedly not empty on" | 229 if password_element.get_attribute("value"): |
| 225 "website: %s" % self.name) | 230 raise Exception("Error: password value unexpectedly not empty on" |
| 226 password_element.send_keys(self.password) | 231 "website: %s" % self.name) |
| 232 | |
| 233 password_element.clear() | |
| 234 password_element.send_keys(self.password) | |
| 227 | 235 |
| 228 def FillUsernameInto(self, selector): | 236 def FillUsernameInto(self, selector): |
| 229 """Ensures that the selected element's value is the saved username. | 237 """Ensures that the selected element's value is the saved username. |
| 230 | 238 |
| 231 Depending on self.autofill_expectation, this either checks that the | 239 Depending on self.autofill_expectation, this either checks that the |
| 232 element already has the username autofilled, or checks that the value | 240 element already has the username autofilled, or checks that the value |
| 233 is empty and replaces it with the password. If self.username_not_auto | 241 is empty and replaces it with the password. If self.username_not_auto |
| 234 is true, it skips the checks and just overwrites the value with the | 242 is true, it skips the checks and just overwrites the value with the |
| 235 username. | 243 username. |
| 236 | 244 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 [environment.MESSAGE_SAVE], | 364 [environment.MESSAGE_SAVE], |
| 357 True, | 365 True, |
| 358 "Error: did not detect login success on website: %s" % self.name) | 366 "Error: did not detect login success on website: %s" % self.name) |
| 359 self.Logout() | 367 self.Logout() |
| 360 self.LoginWhenAutofilled() | 368 self.LoginWhenAutofilled() |
| 361 self.environment.CheckForNewString( | 369 self.environment.CheckForNewString( |
| 362 [environment.MESSAGE_SAVE], | 370 [environment.MESSAGE_SAVE], |
| 363 True, | 371 True, |
| 364 "Error: failed autofilled login on website: %s" % self.name) | 372 "Error: failed autofilled login on website: %s" % self.name) |
| 365 | 373 |
| OLD | NEW |