OLD | NEW |
---|---|
(Empty) | |
1 import time | |
vabr (Chromium)
2014/05/08 13:53:45
Please add a module docstring (also in other Pytho
vabr (Chromium)
2014/05/08 13:53:45
Also please add the encoding, like you did in test
rchtara
2014/05/14 11:58:04
Done.
| |
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 | |
vabr (Chromium)
2014/05/08 13:53:45
Please add a docstring documenting this class.
Als
| |
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 """ | |
vabr (Chromium)
2014/05/08 13:53:45
'For one liner docstrings, please keep the closing
rchtara
2014/05/14 11:58:04
Done.
| |
17 print "actiontype: %s actionparam: %s" % (self.action_type, self.param) | |
vabr (Chromium)
2014/05/08 13:53:45
If you need this temporarily as a debugging messag
rchtara
2014/05/14 11:58:04
I think it's important to put the log, because the
| |
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": | |
vabr (Chromium)
2014/05/08 13:53:45
typo: optinal -> optional
rchtara
2014/05/14 11:58:04
Done.
| |
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 | |
vabr (Chromium)
2014/05/08 13:53:45
As already mentioned off-line: except for doc-stri
rchtara
2014/05/14 11:58:04
Done.
| |
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") | |
vabr (Chromium)
2014/05/08 13:53:45
Why do we actually care if Chrome fills the passwo
rchtara
2014/05/14 11:58:04
Chrome doesn't fill password input until the user
| |
57 password_element.clear() | |
58 password_element.send_keys(self.website.password) | |
59 elif self.action_type == "wait": | |
60 time.sleep(self.param) | |
vabr (Chromium)
2014/05/08 13:53:45
You already have one if-branch for "wait". Please
rchtara
2014/05/14 11:58:04
Done.
| |
61 | |
62 def DoAutofilled(self, driver): | |
63 """Make the action behavior when the username and password fields are | |
vabr (Chromium)
2014/05/08 13:53:45
nit: Please use single-space consistently, no doub
vabr (Chromium)
2014/05/08 13:53:45
I cannot parse the sentence. Are you trying to say
rchtara
2014/05/14 11:58:04
Done.
rchtara
2014/05/14 11:58:04
Done.
| |
64 expected to be autofilled: for fillusername amd fillpassword, check that | |
65 the autofilled values are equal to the ones we expect. For other actions, | |
66 do the default behavior. | |
67 | |
vabr (Chromium)
2014/05/08 13:53:45
nit: Please remove the blank line.
rchtara
2014/05/14 11:58:04
Done.
| |
68 """ | |
69 if (self.action_type == "fillusername" and | |
70 not self.website.username_not_auto): | |
71 print "actiontype: %s actionparam: %s" % (self.action_type, self.param) | |
72 username_element = driver.find_element_by_css_selector(self.param) | |
73 assert username_element.get_attribute("value") == self.website.username, ( | |
vabr (Chromium)
2014/05/08 13:53:45
The Chromium style guide for Python (http://dev.ch
rchtara
2014/05/14 11:58:04
Done.
| |
74 "Error: autofilled " | |
75 "username is different form the one we just saved for the " | |
76 "following website : %s \n" % self.website.Url()) | |
77 elif self.action_type == "fillpassword": | |
78 print "actiontype: %s actionparam: %s" % (self.action_type, self.param) | |
79 """Chrome protects the password inputs and doesn't fill them until | |
vabr (Chromium)
2014/05/08 13:53:45
Actually, some errors I saw in the past were when
rchtara
2014/05/14 11:58:04
I made the test and you right: the password is aut
| |
80 the user interacts with them. So when using chromedriver, if we | |
81 want to get the stored password, we need to be careful about that: | |
82 we need to send a key to password element, chrome is going to | |
83 autofill it if he has already saved password for it, then we need | |
84 to get the value of the input and we remove the character we add | |
85 it before. | |
86 """ | |
87 password_element = driver.find_element_by_css_selector(self.param) | |
88 password_element.send_keys("a") | |
89 ps = password_element.get_attribute("value")[:-1] | |
90 password_element.clear() | |
91 password_element.send_keys(ps) | |
92 assert password_element.get_attribute("value") == self.website.password, ( | |
93 "Error: autofilled password is different from the one we just saved " | |
94 "for the following website : %s p1: %s p2:%s \n" % | |
95 (self.website.Url(), password_element.get_attribute("value"), | |
96 self.website.password)) | |
97 else: | |
98 self.Do(driver) | |
99 | |
100 def DoNotAutofilled(self, driver): | |
101 """Do the action behavior when the password field is not expected to be | |
vabr (Chromium)
2014/05/08 13:53:45
Please once you improve the docstring for the prev
rchtara
2014/05/14 11:58:04
Done.
rchtara
2014/05/14 11:58:04
Done.
| |
102 autofilled: for fillpassword check that the password field is empty before | |
103 filling it. For other actions, do the default behavior. | |
104 """ | |
105 if self.action_type == "fillpassword": | |
106 """Chrome protects the password inputs and doesn't fill them until | |
107 the user interacts with them. So when using chromedriver, if we | |
108 want to get the stored password, we need to be careful about that: | |
109 we need to send a key to password element, chrome is going to | |
110 autofill it if he has already saved password for it, then we need | |
111 to get the value of the input and we remove the character we add | |
112 it before. | |
113 """ | |
114 password_element = driver.find_element_by_css_selector(self.param) | |
115 password_element.send_keys("a") | |
116 ps = password_element.get_attribute("value")[1:] | |
117 password_element.clear() | |
118 password_element.send_keys(ps) | |
119 | |
120 assert len(ps) == 0, ("Error: password is autofilled when it shouldn't " | |
vabr (Chromium)
2014/05/08 13:53:45
len(ps) == 0
should be just
ps
(Look for "len(" in
rchtara
2014/05/14 11:58:04
Done.
| |
121 " be for the following website : %s \n" | |
122 % self.website.Url()) | |
123 | |
124 self.Do(driver) | |
OLD | NEW |