OLD | NEW |
(Empty) | |
| 1 import time |
| 2 from selenium.webdriver.common.action_chains import ActionChains |
| 3 from selenium.webdriver.common.keys import Keys |
| 4 from selenium.common.exceptions import ElementNotVisibleException |
| 5 |
| 6 |
| 7 class Action: |
| 8 |
| 9 def __init__(self, action_type, param, website): |
| 10 self.action_type = action_type |
| 11 self.param = param |
| 12 self.website = website |
| 13 |
| 14 def Do(self, driver): |
| 15 """Do the default behavior of the action. |
| 16 """ |
| 17 print "actiontype: %s actionparam: %s" % (self.action_type, self.param) |
| 18 if self.action_type == "goto": |
| 19 driver.get(self.param) |
| 20 elif self.action_type == "wait": |
| 21 time.sleep(self.param) |
| 22 elif self.action_type == "click": |
| 23 element = driver.find_element_by_css_selector(self.param) |
| 24 element.click() |
| 25 elif self.action_type == "hover": |
| 26 element = driver.find_element_by_css_selector(self.param) |
| 27 hover = ActionChains(driver).move_to_element(element) |
| 28 hover.perform() |
| 29 elif self.action_type == "submit": |
| 30 element = driver.find_element_by_css_selector(self.param) |
| 31 element.submit() |
| 32 elif self.action_type == "enter": |
| 33 body = driver.find_element_by_tag_name("body") |
| 34 body.send_keys(Keys.ENTER) |
| 35 elif self.action_type == "optinalfillusername": |
| 36 username_element = driver.find_element_by_css_selector(self.param) |
| 37 print username_element |
| 38 try: |
| 39 username_element.clear() |
| 40 username_element.send_keys(self.website.username) |
| 41 except ElementNotVisibleException: |
| 42 pass |
| 43 elif self.action_type == "fillusername": |
| 44 username_element = driver.find_element_by_css_selector(self.param) |
| 45 username_element.clear() |
| 46 username_element.send_keys(self.website.username) |
| 47 elif self.action_type == "fillpassword": |
| 48 """Chrome protects the password inputs and doesn't fill them until |
| 49 the user interacts with them. So when using chromedriver, we need |
| 50 to be careful about that: we need to send a key to password |
| 51 element, chrome is going to autofill it if he has already saved |
| 52 password for it, then we need to clear everything and send the |
| 53 password. |
| 54 """ |
| 55 password_element = driver.find_element_by_css_selector(self.param) |
| 56 password_element.send_keys("a") |
| 57 password_element.clear() |
| 58 password_element.send_keys(self.website.password) |
| 59 elif self.action_type == "wait": |
| 60 time.sleep(self.param) |
| 61 |
| 62 def DoAutofilled(self, driver): |
| 63 """Make the action behavior when the username and password fields are expec
ted to be autofilled: for fillusername amd fillpassword, check that the autofill
ed values are equal to the ones we expect. For other actions, do the default beh
avior. |
| 64 |
| 65 """ |
| 66 if (self.action_type == "fillusername" and |
| 67 not self.website.username_not_auto): |
| 68 print "actiontype: %s actionparam: %s" % (self.action_type, self.param) |
| 69 username_element = driver.find_element_by_css_selector(self.param) |
| 70 assert username_element.get_attribute("value") == self.website.username, ( |
| 71 "Error: autofilled " |
| 72 "username is different form the one we just saved for the " |
| 73 "following website : %s \n" % self.website.Url()) |
| 74 elif self.action_type == "fillpassword": |
| 75 print "actiontype: %s actionparam: %s" % (self.action_type, self.param) |
| 76 """Chrome protects the password inputs and doesn't fill them until |
| 77 the user interacts with them. So when using chromedriver, if we |
| 78 want to get the stored password, we need to be careful about that: |
| 79 we need to send a key to password element, chrome is going to |
| 80 autofill it if he has already saved password for it, then we need |
| 81 to get the value of the input and we remove the character we add |
| 82 it before. |
| 83 """ |
| 84 password_element = driver.find_element_by_css_selector(self.param) |
| 85 password_element.send_keys("a") |
| 86 ps = password_element.get_attribute("value")[:-1] |
| 87 password_element.clear() |
| 88 password_element.send_keys(ps) |
| 89 assert password_element.get_attribute("value") == self.website.password, ( |
| 90 "Error: autofilled password is different from the one we just saved " |
| 91 "for the following website : %s p1: %s p2:%s \n" % |
| 92 (self.website.Url(), password_element.get_attribute("value"), |
| 93 self.website.password)) |
| 94 else: |
| 95 self.Do(driver) |
| 96 |
| 97 def DoNotAutofilled(self, driver): |
| 98 """Do the action behavior when the password field is not expected to be auto
filled: for fillpassword check that the password field is empty before filling i
t. For other actions, do the default behavior. |
| 99 """ |
| 100 if self.action_type == "fillpassword": |
| 101 print "actiontype: %s actionparam: %s" % (self.action_type, self.param) |
| 102 """Chrome protects the password inputs and doesn't fill them until |
| 103 the user interacts with them. So when using chromedriver, if we |
| 104 want to get the stored password, we need to be careful about that: |
| 105 we need to send a key to password element, chrome is going to |
| 106 autofill it if he has already saved password for it, then we need |
| 107 to get the value of the input and we remove the character we add |
| 108 it before. |
| 109 """ |
| 110 password_element = driver.find_element_by_css_selector(self.param) |
| 111 password_element.send_keys("a") |
| 112 ps = password_element.get_attribute("value")[1:] |
| 113 password_element.clear() |
| 114 password_element.send_keys(ps) |
| 115 |
| 116 assert len(ps) == 0, ("Error: password is autofilled when it shouldn't " |
| 117 " be for the following website : %s \n" |
| 118 % self.website.Url()) |
| 119 |
| 120 self.Do(driver) |
OLD | NEW |