| Index: components/test/data/password_manager/action.py
|
| diff --git a/components/test/data/password_manager/action.py b/components/test/data/password_manager/action.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..40ba0c6f2b6da85839c80c87362137504362645b
|
| --- /dev/null
|
| +++ b/components/test/data/password_manager/action.py
|
| @@ -0,0 +1,120 @@
|
| +import time
|
| +from selenium.webdriver.common.action_chains import ActionChains
|
| +from selenium.webdriver.common.keys import Keys
|
| +from selenium.common.exceptions import ElementNotVisibleException
|
| +
|
| +
|
| +class Action:
|
| +
|
| + def __init__(self, action_type, param, website):
|
| + self.action_type = action_type
|
| + self.param = param
|
| + self.website = website
|
| +
|
| + def Do(self, driver):
|
| + """Do the default behavior of the action.
|
| + """
|
| + print "actiontype: %s actionparam: %s" % (self.action_type, self.param)
|
| + if self.action_type == "goto":
|
| + driver.get(self.param)
|
| + elif self.action_type == "wait":
|
| + time.sleep(self.param)
|
| + elif self.action_type == "click":
|
| + element = driver.find_element_by_css_selector(self.param)
|
| + element.click()
|
| + elif self.action_type == "hover":
|
| + element = driver.find_element_by_css_selector(self.param)
|
| + hover = ActionChains(driver).move_to_element(element)
|
| + hover.perform()
|
| + elif self.action_type == "submit":
|
| + element = driver.find_element_by_css_selector(self.param)
|
| + element.submit()
|
| + elif self.action_type == "enter":
|
| + body = driver.find_element_by_tag_name("body")
|
| + body.send_keys(Keys.ENTER)
|
| + elif self.action_type == "optinalfillusername":
|
| + username_element = driver.find_element_by_css_selector(self.param)
|
| + print username_element
|
| + try:
|
| + username_element.clear()
|
| + username_element.send_keys(self.website.username)
|
| + except ElementNotVisibleException:
|
| + pass
|
| + elif self.action_type == "fillusername":
|
| + username_element = driver.find_element_by_css_selector(self.param)
|
| + username_element.clear()
|
| + username_element.send_keys(self.website.username)
|
| + elif self.action_type == "fillpassword":
|
| + """Chrome protects the password inputs and doesn't fill them until
|
| + the user interacts with them. So when using chromedriver, we need
|
| + to be careful about that: we need to send a key to password
|
| + element, chrome is going to autofill it if he has already saved
|
| + password for it, then we need to clear everything and send the
|
| + password.
|
| + """
|
| + password_element = driver.find_element_by_css_selector(self.param)
|
| + password_element.send_keys("a")
|
| + password_element.clear()
|
| + password_element.send_keys(self.website.password)
|
| + elif self.action_type == "wait":
|
| + time.sleep(self.param)
|
| +
|
| + def DoAutofilled(self, driver):
|
| + """Make the action behavior when the username and password fields are expected to be autofilled: for fillusername amd fillpassword, check that the autofilled values are equal to the ones we expect. For other actions, do the default behavior.
|
| +
|
| + """
|
| + if (self.action_type == "fillusername" and
|
| + not self.website.username_not_auto):
|
| + print "actiontype: %s actionparam: %s" % (self.action_type, self.param)
|
| + username_element = driver.find_element_by_css_selector(self.param)
|
| + assert username_element.get_attribute("value") == self.website.username, (
|
| + "Error: autofilled "
|
| + "username is different form the one we just saved for the "
|
| + "following website : %s \n" % self.website.Url())
|
| + elif self.action_type == "fillpassword":
|
| + print "actiontype: %s actionparam: %s" % (self.action_type, self.param)
|
| + """Chrome protects the password inputs and doesn't fill them until
|
| + the user interacts with them. So when using chromedriver, if we
|
| + want to get the stored password, we need to be careful about that:
|
| + we need to send a key to password element, chrome is going to
|
| + autofill it if he has already saved password for it, then we need
|
| + to get the value of the input and we remove the character we add
|
| + it before.
|
| + """
|
| + password_element = driver.find_element_by_css_selector(self.param)
|
| + password_element.send_keys("a")
|
| + ps = password_element.get_attribute("value")[:-1]
|
| + password_element.clear()
|
| + password_element.send_keys(ps)
|
| + assert password_element.get_attribute("value") == self.website.password, (
|
| + "Error: autofilled password is different from the one we just saved "
|
| + "for the following website : %s p1: %s p2:%s \n" %
|
| + (self.website.Url(), password_element.get_attribute("value"),
|
| + self.website.password))
|
| + else:
|
| + self.Do(driver)
|
| +
|
| + def DoNotAutofilled(self, driver):
|
| + """Do the action behavior when the password field is not expected to be autofilled: for fillpassword check that the password field is empty before filling it. For other actions, do the default behavior.
|
| + """
|
| + if self.action_type == "fillpassword":
|
| + print "actiontype: %s actionparam: %s" % (self.action_type, self.param)
|
| + """Chrome protects the password inputs and doesn't fill them until
|
| + the user interacts with them. So when using chromedriver, if we
|
| + want to get the stored password, we need to be careful about that:
|
| + we need to send a key to password element, chrome is going to
|
| + autofill it if he has already saved password for it, then we need
|
| + to get the value of the input and we remove the character we add
|
| + it before.
|
| + """
|
| + password_element = driver.find_element_by_css_selector(self.param)
|
| + password_element.send_keys("a")
|
| + ps = password_element.get_attribute("value")[1:]
|
| + password_element.clear()
|
| + password_element.send_keys(ps)
|
| +
|
| + assert len(ps) == 0, ("Error: password is autofilled when it shouldn't "
|
| + " be for the following website : %s \n"
|
| + % self.website.Url())
|
| +
|
| + self.Do(driver)
|
|
|