webaccount 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +37 -0
- data/README.md +99 -13
- data/lib/{webaccount → WebAccount}/VERSION.rb +1 -1
- data/lib/webaccount.rb +154 -16
- data/test/webaccount_test.rb +372 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b38d097c19e2af46497d256651f7edc1258f6789a04207a4c2e4db08835793ae
|
|
4
|
+
data.tar.gz: a967ff9f12d856ef060ed7feb13ef788104724d4c719d721786f226b560aea14
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 97b9776b4b8680bd730a606a757b94c37d40ded02a2f55e0e2630c6574efd99de25123d549476c54d9a16e42d0b009c6761083b9d8731882b3d7207f5ece39a1
|
|
7
|
+
data.tar.gz: 1de9f972a2fb3a5b4504eacf73f31c64d72170df31908611bd654188d6daec9e657dce9015c64ab2b492f8b7ba4ac6ea0dc6875bf7b10ce6aeda433509f3a1fa
|
data/CHANGELOG
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
1
|
# webaccount/CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 20261115
|
|
4
|
+
|
|
5
|
+
0.8.0: WebAccount reads a sub-class's PAGE_SELECTORS, so that a sub-class needn't override initialize to pass them in.
|
|
6
|
+
|
|
7
|
+
1. + WebAccount::PAGE_SELECTORS: {}.
|
|
8
|
+
2. + WebAccount#page_selectors(): The class's PAGE_SELECTORS.
|
|
9
|
+
3. + WebAccount#login_page_url(), #logged_out_xpath(): The keyword if given, else page_selectors' value.
|
|
10
|
+
4. /WebAccount#username_field_selector()/login_page_username_field_selector()/, /WebAccount#password_field_selector()/login_page_password_field_selector()/: Named as their keywords are.
|
|
11
|
+
5. + WebAccount#default_login_page_username_field_selector(), #default_login_page_password_field_selector(): Named as their keywords are. Until 0.9.0, each is default_username_field_selector() or default_password_field_selector(), so as a sub-class's override of the old name is still used.
|
|
12
|
+
6. ~ WebAccount#username_field_selector(), #password_field_selector(): Until 0.9.0, the readers by their old names.
|
|
13
|
+
7. ~ WebAccount#login_page_username_field_selector(), #login_page_password_field_selector(), #login_page_submit_button_selector(), #logout_button_selector(): The keyword if given, else page_selectors' value, else the default.
|
|
14
|
+
8. ~ WebAccount#login_page(): Gets login_page_url, and raises ArgumentError naming login_page_url: where there is none, rather than handing nil to the browser.
|
|
15
|
+
9. ~ WebAccount#logged_out?(): Looks for logged_out_xpath.
|
|
16
|
+
10. ~ test/webaccount_test.rb: + Each of the six with neither, with a sub-class's PAGE_SELECTORS, and with the keyword winning over it; page_selectors overridden, keeping a parent's values; the old names until 0.9.0; login_page naming the missing login_page_url:.
|
|
17
|
+
11. ~ README.md: MyAccount.new() without PAGE_SELECTORS merged into it; + The keywords win over PAGE_SELECTORS; + Where each page value comes from: the order each reader takes, the old names until 0.9.0, and overriding PAGE_SELECTORS, page_selectors or a single reader.
|
|
18
|
+
12. ~ VERSION: /0.7.0/0.8.0/
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## 20261109
|
|
22
|
+
|
|
23
|
+
0.7.0: Default browser options for Chrome and Firefox: a profile directory, if given, kept between runs, downloads into a download directory without asking, headless, a window size and a user agent; and waiting for a download to finish.
|
|
24
|
+
|
|
25
|
+
1. + WebAccount.new(): + profile_directory:, + download_directory:, the latter defaulting to ~/Downloads, both expanded; + headless:, false by default; + window_size:, a width and a height; + user_agent:.
|
|
26
|
+
2. + WebAccount#download_directory.
|
|
27
|
+
3. ~ WebAccount#driver(): the window resized to window_size, if given, once the browser has started, so as it holds for any browser and whether or not driver_options are given.
|
|
28
|
+
4. ~ WebAccount#default_driver_options(): Chrome's or Firefox's options, by browser(); nil for other browsers, as before.
|
|
29
|
+
5. + WebAccount#default_chrome_options(): the profile directory, if given, as --user-data-dir; --headless=new, if headless; --user-agent, if given; the automation flags masked; no offer to save passwords; downloads into download_directory without asking, opening nothing, PDFs saved rather than shown.
|
|
30
|
+
6. + WebAccount#default_firefox_options(): the profile directory, if given, by -profile, which geckodriver uses in place; -headless, if headless; general.useragent.override, if a user agent is given; no offer to save passwords; downloads into download_directory without asking, PDFs saved rather than shown. Firefox has no preference which masks navigator.webdriver.
|
|
31
|
+
7. + WebAccount#wait_for_download(), #newest_download(): the newest finished file with the extension given, csv by default, changed in download_directory since the time given.
|
|
32
|
+
8. + WebAccount#download_wait(), #default_download_timeout(): a Wait of its own for downloads, at 120s; default_wait_timeout's 30s is for logging in, and would give up on a large file still arriving.
|
|
33
|
+
9. + WebAccount#downloading?(): whether a file's partner is still beside it: Chrome's <name>.crdownload, or Firefox's <stem>.<random>.<extensions>.part, where Firefox has already made an empty file under the final name.
|
|
34
|
+
10. + WebAccount#click(), #stale?(), #wait_for(), #set_value(), private.
|
|
35
|
+
11. ~ test/webaccount_test.rb: tests of the options for each browser, with and without a profile, a download directory, headless and a user agent; the window sized or not; driver_options: over-riding them; finding a download, past one with its .crdownload or .part still there and one older than since; click() and stale?().
|
|
36
|
+
12. ~ README.md: describes Chrome and Firefox, profile and download directories, headless, window size, user agent, waiting for downloads, and the page helpers.
|
|
37
|
+
13. ~ VERSION: /0.6.1/0.7.0/
|
|
38
|
+
|
|
39
|
+
|
|
3
40
|
## 20261103
|
|
4
41
|
|
|
5
42
|
0.6.1: ~ WebAccount#shutdown quits the browser even where logging out fails.
|
data/README.md
CHANGED
|
@@ -51,10 +51,7 @@ $ gem install webaccount
|
|
|
51
51
|
}
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
-
MyAccount.new(
|
|
55
|
-
**{username: username, password: password}\
|
|
56
|
-
.merge(MyAccount::PAGE_SELECTORS)
|
|
57
|
-
)
|
|
54
|
+
MyAccount.new(username: username, password: password)
|
|
58
55
|
# => #<MyAccount @username=username, @password=password...>
|
|
59
56
|
```
|
|
60
57
|
|
|
@@ -73,13 +70,54 @@ $ gem install webaccount
|
|
|
73
70
|
}
|
|
74
71
|
end
|
|
75
72
|
|
|
76
|
-
MyAccount.new(
|
|
77
|
-
**{username: username, password: password}
|
|
78
|
-
.merge(MyAccount::PAGE_SELECTORS)
|
|
79
|
-
)
|
|
73
|
+
MyAccount.new(username: username, password: password)
|
|
80
74
|
# => #<MyAccount @username=username, @password=password...>
|
|
81
75
|
```
|
|
82
76
|
|
|
77
|
+
WebAccount reads a sub-class's `PAGE_SELECTORS` itself, so the sub-class needn't override `initialize`. Each key is also a keyword of `new`, which wins over the sub-class's value:
|
|
78
|
+
```ruby
|
|
79
|
+
MyAccount.new(username: username, password: password, login_page_url: 'https://test.mysite.com/login')
|
|
80
|
+
```
|
|
81
|
+
`login` raises `ArgumentError`, naming `login_page_url:`, if there's no login page URL.
|
|
82
|
+
|
|
83
|
+
### Where each page value comes from
|
|
84
|
+
|
|
85
|
+
Each value is found by a private reader of the same name as its keyword, as `login_page_url` or `logout_button_selector`, which takes the first of:
|
|
86
|
+
|
|
87
|
+
1. the keyword given to `new`;
|
|
88
|
+
2. that key of `page_selectors`, which is the class's `PAGE_SELECTORS`;
|
|
89
|
+
3. for the four selectors, `default_` and the same name, as `default_logout_button_selector`. The login page URL and the logged-out XPath have no default.
|
|
90
|
+
|
|
91
|
+
The readers and defaults for the username and password fields were named `username_field_selector`, `password_field_selector`, `default_username_field_selector` and `default_password_field_selector` before 0.8.0. Those names stay until 0.9.0, an override of either default still being used.
|
|
92
|
+
|
|
93
|
+
So a sub-class can give its values at any of three levels, from the coarsest to the finest.
|
|
94
|
+
|
|
95
|
+
`PAGE_SELECTORS`, for values which are fixed, as in 2 and 3 above.
|
|
96
|
+
|
|
97
|
+
`page_selectors`, for a set of values which has to be worked out, or which differs from a parent's. Overriding it replaces `PAGE_SELECTORS` for every reader, and a sub-class of a sub-class can keep its parent's values and change some:
|
|
98
|
+
```ruby
|
|
99
|
+
class MyBusinessAccount < MyAccount
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
def page_selectors
|
|
103
|
+
super.merge(login_page_url: 'https://business.mysite.com/login')
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
A single reader, for one value which has to be worked out. Starting with `super ||` keeps the keyword winning:
|
|
109
|
+
```ruby
|
|
110
|
+
class MyAccount < WebAccount
|
|
111
|
+
PAGE_SELECTORS = {...}
|
|
112
|
+
|
|
113
|
+
private
|
|
114
|
+
|
|
115
|
+
def login_page_url
|
|
116
|
+
super || "https://mysite.com/login?t=#{Time.now.to_i}"
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
```
|
|
120
|
+
|
|
83
121
|
### Choosing a browser
|
|
84
122
|
|
|
85
123
|
`browser` is passed to `Selenium::WebDriver.for`, so it accepts whatever Selenium does: `:chrome`, `:firefox`, `:safari`, `:edge`, `:ie`. It defaults to `:chrome`, and a String or a capitalised Symbol is fine.
|
|
@@ -89,10 +127,33 @@ $ gem install webaccount
|
|
|
89
127
|
|
|
90
128
|
### Driver options
|
|
91
129
|
|
|
92
|
-
|
|
130
|
+
For Chrome and Firefox, WebAccount builds the options itself:
|
|
131
|
+
|
|
132
|
+
- With `profile_directory`, the browser starts with that profile, kept between runs, so that a site sees a returning browser. Without it, the browser gets a fresh temporary profile each time.
|
|
133
|
+
- With `download_directory`, which defaults to `~/Downloads`, the browser downloads there without asking where, and opens nothing it downloads. Both save PDFs rather than showing them.
|
|
134
|
+
- With `headless: true`, the browser runs without a window.
|
|
135
|
+
- With `user_agent`, the browser sends that string in place of its own. Leave it unset unless a site needs it: a fixed string goes out of date, and a user agent that disagrees with the browser actually running is itself a sign of automation.
|
|
136
|
+
- Neither browser offers to save passwords.
|
|
137
|
+
- Chrome's automation flags are masked. Firefox has no preference that masks `navigator.webdriver`.
|
|
138
|
+
|
|
139
|
+
```ruby
|
|
140
|
+
MyAccount.new(
|
|
141
|
+
username: username, password: password,
|
|
142
|
+
browser: :firefox,
|
|
143
|
+
profile_directory: '~/.my_account/profile',
|
|
144
|
+
download_directory: '~/statements',
|
|
145
|
+
headless: true,
|
|
146
|
+
window_size: [1280, 800]
|
|
147
|
+
)
|
|
148
|
+
```
|
|
149
|
+
For any other browser, the options are nil, which is Selenium's own default.
|
|
150
|
+
|
|
151
|
+
`window_size`, a width and a height, sizes the window once the browser has started, for any browser. A site's layout depends on the window's width, and a narrow window, as a headless browser may start with, can get a mobile layout whose selectors differ.
|
|
152
|
+
|
|
153
|
+
`driver_options` replaces all of this, apart from `window_size`. It is passed to `Selenium::WebDriver.for` as `options:`.
|
|
93
154
|
```ruby
|
|
94
155
|
options = Selenium::WebDriver::Chrome::Options.new
|
|
95
|
-
options.add_argument('--
|
|
156
|
+
options.add_argument('--lang=en-AU')
|
|
96
157
|
|
|
97
158
|
MyAccount.new(username: username, password: password, driver_options: options)
|
|
98
159
|
```
|
|
@@ -102,8 +163,8 @@ Or in a sub-class, if every instance should be configured the same way:
|
|
|
102
163
|
private
|
|
103
164
|
|
|
104
165
|
def default_driver_options
|
|
105
|
-
|
|
106
|
-
options.add_argument('--
|
|
166
|
+
default_chrome_options.tap do |options|
|
|
167
|
+
options.add_argument('--lang=en-AU')
|
|
107
168
|
end
|
|
108
169
|
end
|
|
109
170
|
end
|
|
@@ -111,12 +172,13 @@ Or in a sub-class, if every instance should be configured the same way:
|
|
|
111
172
|
|
|
112
173
|
### Timeouts
|
|
113
174
|
|
|
114
|
-
There are
|
|
175
|
+
There are three, each overridable in a sub-class:
|
|
115
176
|
|
|
116
177
|
| Method | Default | Waits for |
|
|
117
178
|
| --- | --- | --- |
|
|
118
179
|
| `default_find_timeout` | 5s | An element on a page which has already loaded. |
|
|
119
180
|
| `default_wait_timeout` | 30s | Navigation: a submit, the authentication behind it, and the redirect. |
|
|
181
|
+
| `default_download_timeout` | 120s | A download to finish, which takes as long as the file is large. |
|
|
120
182
|
|
|
121
183
|
```ruby
|
|
122
184
|
class SlowAccount < WebAccount
|
|
@@ -190,6 +252,30 @@ Prefer `find` over `driver.find_element`: it waits up to `default_find_timeout`
|
|
|
190
252
|
my_account.list_page
|
|
191
253
|
```
|
|
192
254
|
|
|
255
|
+
### Waiting for downloads
|
|
256
|
+
|
|
257
|
+
`wait_for_download` returns the name of the newest file with the given extension (`csv` by default) to appear in `download_directory` since the time given. It waits up to `default_download_timeout` for one, and raises `Selenium::WebDriver::Error::TimeoutError` if none appears. A file only counts once the browser has finished it. Chrome writes `<name>.crdownload` and renames it when it's done. Firefox creates an empty `<name>` straight away and writes into `<stem>.<random>.<extensions>.part` beside it, where the stem is the name up to its first dot.
|
|
258
|
+
```ruby
|
|
259
|
+
class MyAccount < WebAccount
|
|
260
|
+
def download_statement
|
|
261
|
+
clicked_at = Time.now
|
|
262
|
+
click(id: 'download')
|
|
263
|
+
wait_for_download(since: clicked_at)
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Page helpers
|
|
269
|
+
|
|
270
|
+
These are private, for use in a sub-class:
|
|
271
|
+
|
|
272
|
+
| Method | Does |
|
|
273
|
+
| --- | --- |
|
|
274
|
+
| `click(selector)` | Finds the element afresh on each attempt of `driver.attempt`, so an element replaced by a redrawn page is retried. If the element isn't displayed, for instance a link in a closed menu, it's clicked by JavaScript. |
|
|
275
|
+
| `stale?(element)` | Whether the element has gone from the page, for waiting out a page being redrawn. |
|
|
276
|
+
| `wait_for(selector)` | Waits up to `default_wait_timeout` for any displayed match. |
|
|
277
|
+
| `set_value(element, value)` | Sets the value by JavaScript, so that a date picker or the like doesn't open, and then dispatches a bubbling change event. |
|
|
278
|
+
|
|
193
279
|
## Contributing
|
|
194
280
|
|
|
195
281
|
1. Fork it: https://github.com/thoran/webaccount/fork
|
data/lib/webaccount.rb
CHANGED
|
@@ -6,12 +6,18 @@ require_relative './Thoran/Selenium'
|
|
|
6
6
|
require_relative './WebAccount/VERSION'
|
|
7
7
|
|
|
8
8
|
class WebAccount
|
|
9
|
+
# A sub-class gives its own, keyed as initialize's keywords are; a keyword given wins over it.
|
|
10
|
+
PAGE_SELECTORS = {}
|
|
11
|
+
|
|
9
12
|
attr_accessor :username
|
|
10
13
|
attr_accessor :password
|
|
14
|
+
attr_reader :download_directory
|
|
11
15
|
|
|
12
16
|
def initialize(
|
|
13
17
|
username: nil, password: nil,
|
|
14
18
|
browser: nil, driver_options: nil,
|
|
19
|
+
profile_directory: nil, download_directory: File.join('~', 'Downloads'),
|
|
20
|
+
headless: false, window_size: nil, user_agent: nil,
|
|
15
21
|
login_page_url: nil,
|
|
16
22
|
login_page_username_field_selector: nil, login_page_password_field_selector: nil,
|
|
17
23
|
login_page_submit_button_selector: nil,
|
|
@@ -20,6 +26,9 @@ class WebAccount
|
|
|
20
26
|
)
|
|
21
27
|
@username, @password = username, password
|
|
22
28
|
@browser, @driver_options = browser, driver_options
|
|
29
|
+
@profile_directory = profile_directory && File.expand_path(profile_directory)
|
|
30
|
+
@download_directory = File.expand_path(download_directory)
|
|
31
|
+
@headless, @window_size, @user_agent = headless, window_size, user_agent
|
|
23
32
|
@login_page_url = login_page_url
|
|
24
33
|
@login_page_username_field_selector, @login_page_password_field_selector = login_page_username_field_selector, login_page_password_field_selector
|
|
25
34
|
@login_page_submit_button_selector = login_page_submit_button_selector
|
|
@@ -58,8 +67,11 @@ class WebAccount
|
|
|
58
67
|
driver.quit
|
|
59
68
|
end
|
|
60
69
|
|
|
70
|
+
# The window is sized once the browser has started, so as it's the same for every browser, and holds whether or not driver_options are given.
|
|
61
71
|
def driver
|
|
62
|
-
@driver ||= Selenium::WebDriver.for(browser, options: driver_options)
|
|
72
|
+
@driver ||= Selenium::WebDriver.for(browser, options: driver_options).tap do |driver|
|
|
73
|
+
driver.manage.window.resize_to(*@window_size) if @window_size
|
|
74
|
+
end
|
|
63
75
|
end
|
|
64
76
|
|
|
65
77
|
# predicate methods
|
|
@@ -70,7 +82,7 @@ class WebAccount
|
|
|
70
82
|
|
|
71
83
|
def logged_out?
|
|
72
84
|
driver_wait.until do
|
|
73
|
-
driver.element_present?(:xpath,
|
|
85
|
+
driver.element_present?(:xpath, logged_out_xpath)
|
|
74
86
|
end
|
|
75
87
|
end
|
|
76
88
|
|
|
@@ -91,7 +103,45 @@ class WebAccount
|
|
|
91
103
|
end
|
|
92
104
|
|
|
93
105
|
def default_driver_options
|
|
94
|
-
|
|
106
|
+
case browser
|
|
107
|
+
when :chrome then default_chrome_options
|
|
108
|
+
when :firefox then default_firefox_options
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Chromedriver starts Chrome with a temporary profile unless given one. PDFs, which Chrome would otherwise show itself, are saved like anything else.
|
|
113
|
+
def default_chrome_options
|
|
114
|
+
Selenium::WebDriver::Chrome::Options.new.tap do |o|
|
|
115
|
+
o.add_argument("--user-data-dir=#{@profile_directory}") if @profile_directory
|
|
116
|
+
o.add_argument('--headless=new') if @headless
|
|
117
|
+
o.add_argument("--user-agent=#{@user_agent}") if @user_agent
|
|
118
|
+
o.add_argument('--disable-blink-features=AutomationControlled')
|
|
119
|
+
o.exclude_switches << 'enable-automation'
|
|
120
|
+
o.add_preference(:credentials_enable_service, false)
|
|
121
|
+
o.add_preference(:download, default_directory: @download_directory, prompt_for_download: false, directory_upgrade: true, extensions_to_open: '')
|
|
122
|
+
o.add_preference(:plugins, always_open_pdf_externally: true)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Geckodriver starts Firefox with a temporary profile unless given one with -profile, which it uses in place. Firefox has no preference which masks navigator.webdriver. PDFs, which it would otherwise show itself, are saved like anything else.
|
|
127
|
+
def default_firefox_options
|
|
128
|
+
Selenium::WebDriver::Firefox::Options.new.tap do |o|
|
|
129
|
+
if @profile_directory
|
|
130
|
+
o.add_argument('-profile')
|
|
131
|
+
o.add_argument(@profile_directory)
|
|
132
|
+
end
|
|
133
|
+
o.add_argument('-headless') if @headless
|
|
134
|
+
o.add_preference('general.useragent.override', @user_agent) if @user_agent
|
|
135
|
+
o.add_preference('signon.rememberSignons', false)
|
|
136
|
+
o.add_preference('browser.download.folderList', 2)
|
|
137
|
+
o.add_preference('browser.download.dir', @download_directory)
|
|
138
|
+
o.add_preference('browser.download.useDownloadDir', true)
|
|
139
|
+
o.add_preference('browser.download.always_ask_before_handling_new_types', false)
|
|
140
|
+
o.add_preference('browser.download.start_downloads_in_tmp_dir', false)
|
|
141
|
+
o.add_preference('browser.download.alwaysOpenPanel', false)
|
|
142
|
+
o.add_preference('browser.download.open_pdf_attachments_inline', false)
|
|
143
|
+
o.add_preference('pdfjs.disabled', true)
|
|
144
|
+
end
|
|
95
145
|
end
|
|
96
146
|
|
|
97
147
|
def driver_options
|
|
@@ -118,14 +168,80 @@ class WebAccount
|
|
|
118
168
|
@find_wait ||= Selenium::WebDriver::Wait.new(timeout: default_find_timeout)
|
|
119
169
|
end
|
|
120
170
|
|
|
171
|
+
# For downloads: a file arriving, which takes as long as it's large.
|
|
172
|
+
def default_download_timeout
|
|
173
|
+
120
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def download_wait
|
|
177
|
+
@download_wait ||= Selenium::WebDriver::Wait.new(timeout: default_download_timeout)
|
|
178
|
+
end
|
|
179
|
+
|
|
121
180
|
def find(selector)
|
|
122
181
|
find_wait.until{driver.find_element(selector.keys.first, selector.values.first)}
|
|
123
182
|
end
|
|
124
183
|
|
|
184
|
+
def wait_for(selector)
|
|
185
|
+
driver_wait.until{driver.find_elements(selector.keys.first, selector.values.first).any?(&:displayed?)}
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# page interaction
|
|
189
|
+
|
|
190
|
+
# Found afresh on each attempt, so as an element replaced by a redrawn page is retried rather than ending in StaleElementReferenceError. One which isn't displayed, such as a link in a closed menu, is clicked by JavaScript.
|
|
191
|
+
def click(selector)
|
|
192
|
+
driver.attempt do
|
|
193
|
+
element = find(selector)
|
|
194
|
+
element.displayed? ? element.click : driver.execute_script('arguments[0].click();', element)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def stale?(element)
|
|
199
|
+
element.displayed?
|
|
200
|
+
false
|
|
201
|
+
rescue Selenium::WebDriver::Error::StaleElementReferenceError
|
|
202
|
+
true
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# By JavaScript, so as a date picker or the like doesn't open over the field; the change event is for the page's own handlers.
|
|
206
|
+
def set_value(element, value)
|
|
207
|
+
driver.execute_script("arguments[0].value = arguments[1]; arguments[0].dispatchEvent(new Event('change', {bubbles: true}));", element, value)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# downloads
|
|
211
|
+
|
|
212
|
+
# Waits up to default_download_timeout for a finished download.
|
|
213
|
+
def wait_for_download(since:, extension: 'csv')
|
|
214
|
+
download_wait.until{newest_download(since: since, extension: extension)}
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# The newest finished file with that extension in download_directory changed at or after since; nil where there's none.
|
|
218
|
+
def newest_download(since:, extension: 'csv')
|
|
219
|
+
Dir.glob("*.#{extension}", base: @download_directory)
|
|
220
|
+
.map{|basename| File.join(@download_directory, basename)}
|
|
221
|
+
.select{|filename| File.mtime(filename) >= since && !downloading?(filename)}
|
|
222
|
+
.max_by{|filename| File.mtime(filename)}
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Chrome downloads into <name>.crdownload, and renames it <name> once done. Firefox makes an empty <name> at once, and downloads into <stem>.<random>.<extensions>.part beside it, the stem being the name up to its first dot.
|
|
226
|
+
def downloading?(filename)
|
|
227
|
+
basename = File.basename(filename)
|
|
228
|
+
stem, extensions = basename.split('.', 2)
|
|
229
|
+
firefox_part_file = /\A#{Regexp.escape(stem)}\.([^.]+\.)?#{Regexp.escape(extensions.to_s)}\.part\z/
|
|
230
|
+
Dir.children(File.dirname(filename)).any?{|child| child == "#{basename}.crdownload" || child.match?(firefox_part_file)}
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def page_selectors
|
|
234
|
+
self.class::PAGE_SELECTORS
|
|
235
|
+
end
|
|
236
|
+
|
|
125
237
|
# logging in
|
|
126
238
|
|
|
239
|
+
def login_page_url
|
|
240
|
+
@login_page_url || page_selectors[:login_page_url]
|
|
241
|
+
end
|
|
242
|
+
|
|
127
243
|
def login_page
|
|
128
|
-
driver.get(
|
|
244
|
+
driver.get(login_page_url || raise(ArgumentError, 'missing keyword: :login_page_url'))
|
|
129
245
|
end
|
|
130
246
|
alias_method :get_login_page, :login_page
|
|
131
247
|
|
|
@@ -134,16 +250,16 @@ class WebAccount
|
|
|
134
250
|
username_field.send_keys(username)
|
|
135
251
|
end
|
|
136
252
|
|
|
137
|
-
def
|
|
138
|
-
|
|
253
|
+
def default_login_page_username_field_selector
|
|
254
|
+
default_username_field_selector
|
|
139
255
|
end
|
|
140
256
|
|
|
141
|
-
def
|
|
142
|
-
@login_page_username_field_selector ||
|
|
257
|
+
def login_page_username_field_selector
|
|
258
|
+
@login_page_username_field_selector || page_selectors[:login_page_username_field_selector] || default_login_page_username_field_selector
|
|
143
259
|
end
|
|
144
260
|
|
|
145
261
|
def username_field
|
|
146
|
-
find(
|
|
262
|
+
find(login_page_username_field_selector)
|
|
147
263
|
end
|
|
148
264
|
|
|
149
265
|
def enter_password(password = nil)
|
|
@@ -151,16 +267,16 @@ class WebAccount
|
|
|
151
267
|
password_field.send_keys(password)
|
|
152
268
|
end
|
|
153
269
|
|
|
154
|
-
def
|
|
155
|
-
|
|
270
|
+
def default_login_page_password_field_selector
|
|
271
|
+
default_password_field_selector
|
|
156
272
|
end
|
|
157
273
|
|
|
158
|
-
def
|
|
159
|
-
@login_page_password_field_selector ||
|
|
274
|
+
def login_page_password_field_selector
|
|
275
|
+
@login_page_password_field_selector || page_selectors[:login_page_password_field_selector] || default_login_page_password_field_selector
|
|
160
276
|
end
|
|
161
277
|
|
|
162
278
|
def password_field
|
|
163
|
-
find(
|
|
279
|
+
find(login_page_password_field_selector)
|
|
164
280
|
end
|
|
165
281
|
|
|
166
282
|
def default_login_page_submit_button_selector
|
|
@@ -168,7 +284,7 @@ class WebAccount
|
|
|
168
284
|
end
|
|
169
285
|
|
|
170
286
|
def login_page_submit_button_selector
|
|
171
|
-
@login_page_submit_button_selector || default_login_page_submit_button_selector
|
|
287
|
+
@login_page_submit_button_selector || page_selectors[:login_page_submit_button_selector] || default_login_page_submit_button_selector
|
|
172
288
|
end
|
|
173
289
|
|
|
174
290
|
def login_page_submit_button
|
|
@@ -182,10 +298,32 @@ class WebAccount
|
|
|
182
298
|
end
|
|
183
299
|
|
|
184
300
|
def logout_button_selector
|
|
185
|
-
@logout_button_selector || default_logout_button_selector
|
|
301
|
+
@logout_button_selector || page_selectors[:logout_button_selector] || default_logout_button_selector
|
|
186
302
|
end
|
|
187
303
|
|
|
188
304
|
def logout_button
|
|
189
305
|
find(logout_button_selector)
|
|
190
306
|
end
|
|
307
|
+
|
|
308
|
+
def logged_out_xpath
|
|
309
|
+
@logged_out_xpath || page_selectors[:logged_out_xpath]
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
# Until 0.9.0, the names before 0.8.0's, by which a sub-class may still override the defaults. The defaults themselves move into default_login_page_username_field_selector and default_login_page_password_field_selector then.
|
|
313
|
+
|
|
314
|
+
def default_username_field_selector
|
|
315
|
+
{name: 'username'}
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def default_password_field_selector
|
|
319
|
+
{name: 'password'}
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def username_field_selector
|
|
323
|
+
login_page_username_field_selector
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def password_field_selector
|
|
327
|
+
login_page_password_field_selector
|
|
328
|
+
end
|
|
191
329
|
end
|
data/test/webaccount_test.rb
CHANGED
|
@@ -4,10 +4,14 @@
|
|
|
4
4
|
# 1. No browser: Selenium::WebDriver.for is swapped for a stand-in for the length of
|
|
5
5
|
# each example and put back afterwards, so what is asserted is what WebAccount asks
|
|
6
6
|
# of Selenium and what it hands back.
|
|
7
|
+
# 2. Driver options are built, not used, so no browser is started for them either.
|
|
8
|
+
# 3. Downloads are files made in a temporary directory, with their times set by hand.
|
|
7
9
|
|
|
8
10
|
require_relative '../lib/webaccount'
|
|
9
11
|
|
|
12
|
+
require 'fileutils'
|
|
10
13
|
require 'minitest/autorun'
|
|
14
|
+
require 'tmpdir'
|
|
11
15
|
require 'minitest-spec-context'
|
|
12
16
|
|
|
13
17
|
describe WebAccount do
|
|
@@ -35,6 +39,25 @@ describe WebAccount do
|
|
|
35
39
|
end
|
|
36
40
|
end
|
|
37
41
|
|
|
42
|
+
it "is sized to the window size given, whether or not driver options are given" do
|
|
43
|
+
with_driver_factory do |stand_in, calls|
|
|
44
|
+
window = Object.new
|
|
45
|
+
window.define_singleton_method(:resize_to){|width, height| @size = [width, height]}
|
|
46
|
+
window.define_singleton_method(:size){@size}
|
|
47
|
+
manage = Object.new
|
|
48
|
+
manage.define_singleton_method(:window){window}
|
|
49
|
+
stand_in.define_singleton_method(:manage){manage}
|
|
50
|
+
WebAccount.new(window_size: [1280, 800], driver_options: :options).driver
|
|
51
|
+
_(window.size).must_equal([1280, 800])
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "is left at the browser's own size where none is given" do
|
|
56
|
+
with_driver_factory do |stand_in, calls|
|
|
57
|
+
_(WebAccount.new.driver).must_be_same_as(stand_in)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
38
61
|
it "is made with the browser and driver options given" do
|
|
39
62
|
with_driver_factory do |stand_in, calls|
|
|
40
63
|
WebAccount.new(browser: 'Firefox', driver_options: :options).driver
|
|
@@ -107,4 +130,353 @@ describe WebAccount do
|
|
|
107
130
|
_(page.asked.length).must_equal(1)
|
|
108
131
|
end
|
|
109
132
|
end
|
|
133
|
+
|
|
134
|
+
describe "page selectors" do
|
|
135
|
+
def account_class(page_selectors)
|
|
136
|
+
Class.new(WebAccount){const_set(:PAGE_SELECTORS, page_selectors)}
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
{
|
|
140
|
+
login_page_url: [:login_page_url, nil],
|
|
141
|
+
login_page_username_field_selector: [:login_page_username_field_selector, {name: 'username'}],
|
|
142
|
+
login_page_password_field_selector: [:login_page_password_field_selector, {name: 'password'}],
|
|
143
|
+
login_page_submit_button_selector: [:login_page_submit_button_selector, {xpath: '//input[@type="submit" and @value="Login"]'}],
|
|
144
|
+
logout_button_selector: [:logout_button_selector, {xpath: '//input[@type="submit" and @value="Logout"]'}],
|
|
145
|
+
logged_out_xpath: [:logged_out_xpath, nil],
|
|
146
|
+
}.each do |keyword, (reader, default)|
|
|
147
|
+
describe "##{reader}" do
|
|
148
|
+
it "is WebAccount's own where neither #{keyword}: nor PAGE_SELECTORS gives one" do
|
|
149
|
+
value = WebAccount.new.send(reader)
|
|
150
|
+
default ? _(value).must_equal(default) : _(value).must_be_nil
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
it "is a sub-class's PAGE_SELECTORS[:#{keyword}]" do
|
|
154
|
+
_(account_class(keyword => 'from the sub-class').new.send(reader)).must_equal('from the sub-class')
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
it "is #{keyword}: over a sub-class's PAGE_SELECTORS" do
|
|
158
|
+
_(account_class(keyword => 'from the sub-class').new(keyword => 'given').send(reader)).must_equal('given')
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
describe "the names before 0.8.0's, until 0.9.0" do
|
|
165
|
+
it "are default_username_field_selector and default_password_field_selector, a sub-class's override of which is still used" do
|
|
166
|
+
account_class = Class.new(WebAccount) do
|
|
167
|
+
private def default_username_field_selector = {id: 'old-username'}
|
|
168
|
+
private def default_password_field_selector = {id: 'old-password'}
|
|
169
|
+
end
|
|
170
|
+
_(account_class.new.send(:login_page_username_field_selector)).must_equal({id: 'old-username'})
|
|
171
|
+
_(account_class.new.send(:login_page_password_field_selector)).must_equal({id: 'old-password'})
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
it "are username_field_selector and password_field_selector, which give the new readers' values" do
|
|
175
|
+
account = WebAccount.new(login_page_username_field_selector: {id: 'u'}, login_page_password_field_selector: {id: 'p'})
|
|
176
|
+
_(account.send(:username_field_selector)).must_equal({id: 'u'})
|
|
177
|
+
_(account.send(:password_field_selector)).must_equal({id: 'p'})
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
describe "#page_selectors" do
|
|
182
|
+
it "is the class's PAGE_SELECTORS" do
|
|
183
|
+
account_class = Class.new(WebAccount){const_set(:PAGE_SELECTORS, {login_page_url: 'https://subclass.example/login'})}
|
|
184
|
+
_(account_class.new.send(:page_selectors)).must_equal({login_page_url: 'https://subclass.example/login'})
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
it "is overridden for every reader, and can keep a parent's values" do
|
|
188
|
+
parent = Class.new(WebAccount){const_set(:PAGE_SELECTORS, {login_page_url: 'https://parent.example/login', logged_out_xpath: '//div[@id="parent"]'})}
|
|
189
|
+
child = Class.new(parent){private def page_selectors = super.merge(login_page_url: 'https://child.example/login')}
|
|
190
|
+
_(child.new.send(:login_page_url)).must_equal('https://child.example/login')
|
|
191
|
+
_(child.new.send(:logged_out_xpath)).must_equal('//div[@id="parent"]')
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
describe "#login_page" do
|
|
196
|
+
def account_with_browser(account)
|
|
197
|
+
browser = Object.new
|
|
198
|
+
browser.define_singleton_method(:get){|url| (@got ||= []) << url}
|
|
199
|
+
browser.define_singleton_method(:got){@got}
|
|
200
|
+
account.instance_variable_set(:@driver, browser)
|
|
201
|
+
[account, browser]
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it "gets login_page_url" do
|
|
205
|
+
account, browser = account_with_browser(WebAccount.new(login_page_url: 'https://given.example/login'))
|
|
206
|
+
account.send(:login_page)
|
|
207
|
+
_(browser.got).must_equal(['https://given.example/login'])
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
it "names login_page_url: where there is none" do
|
|
211
|
+
account, browser = account_with_browser(WebAccount.new)
|
|
212
|
+
error = _{account.send(:login_page)}.must_raise(ArgumentError)
|
|
213
|
+
_(error.message).must_equal('missing keyword: :login_page_url')
|
|
214
|
+
_(browser.got).must_be_nil
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
describe "#logged_out?" do
|
|
219
|
+
it "looks for logged_out_xpath" do
|
|
220
|
+
page = Object.new
|
|
221
|
+
page.define_singleton_method(:element_present?){|how, what| (@asked ||= []) << [how, what]; true}
|
|
222
|
+
page.define_singleton_method(:asked){@asked}
|
|
223
|
+
account = WebAccount.new(logged_out_xpath: '//div[@id="given"]')
|
|
224
|
+
account.instance_variable_set(:@driver, page)
|
|
225
|
+
_(account.logged_out?).must_equal(true)
|
|
226
|
+
_(page.asked).must_equal([[:xpath, '//div[@id="given"]']])
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
describe "#driver_options" do
|
|
231
|
+
def driver_options(**args)
|
|
232
|
+
WebAccount.new(**args).send(:driver_options)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
describe "Chrome" do
|
|
236
|
+
it "is the browser by default" do
|
|
237
|
+
_(driver_options).must_be_instance_of(Selenium::WebDriver::Chrome::Options)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it "starts with a fresh profile where none is given" do
|
|
241
|
+
_(driver_options(browser: :chrome).args.grep(/--user-data-dir/)).must_be_empty
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
it "starts with the profile given, expanded" do
|
|
245
|
+
_(driver_options(browser: :chrome, profile_directory: '~/profile').args).must_include("--user-data-dir=#{File.expand_path('~/profile')}")
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
it "masks the automation flags" do
|
|
249
|
+
options = driver_options(browser: :chrome)
|
|
250
|
+
_(options.args).must_include('--disable-blink-features=AutomationControlled')
|
|
251
|
+
_(options.exclude_switches).must_include('enable-automation')
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
it "downloads into ~/Downloads by default, without asking, opening nothing" do
|
|
255
|
+
_(driver_options(browser: :chrome).prefs[:download]).must_equal({default_directory: File.expand_path('~/Downloads'), prompt_for_download: false, directory_upgrade: true, extensions_to_open: ''})
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
it "saves PDFs rather than showing them" do
|
|
259
|
+
_(driver_options(browser: :chrome).prefs[:plugins]).must_equal({always_open_pdf_externally: true})
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
it "has a window by default" do
|
|
263
|
+
_(driver_options(browser: :chrome).args.grep(/headless/)).must_be_empty
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
it "is headless where asked" do
|
|
267
|
+
_(driver_options(browser: :chrome, headless: true).args).must_include('--headless=new')
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
it "says it's whatever Chrome says by default" do
|
|
271
|
+
_(driver_options(browser: :chrome).args.grep(/--user-agent/)).must_be_empty
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
it "says it's the user agent given" do
|
|
275
|
+
_(driver_options(browser: :chrome, user_agent: 'Agent/1.0').args).must_include('--user-agent=Agent/1.0')
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
it "downloads into the download directory given, expanded" do
|
|
279
|
+
_(driver_options(browser: :chrome, download_directory: '~/statements').prefs[:download][:default_directory]).must_equal(File.expand_path('~/statements'))
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
describe "Firefox" do
|
|
284
|
+
it "is Firefox's options" do
|
|
285
|
+
_(driver_options(browser: 'Firefox')).must_be_instance_of(Selenium::WebDriver::Firefox::Options)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
it "starts with a fresh profile where none is given" do
|
|
289
|
+
_(driver_options(browser: :firefox).args).wont_include('-profile')
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
it "starts with the profile given, expanded, in place" do
|
|
293
|
+
_(driver_options(browser: :firefox, profile_directory: '~/profile').args).must_equal(['-profile', File.expand_path('~/profile')])
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
it "downloads into ~/Downloads by default, without asking, opening nothing" do
|
|
297
|
+
prefs = driver_options(browser: :firefox).prefs
|
|
298
|
+
_(prefs['browser.download.folderList']).must_equal(2)
|
|
299
|
+
_(prefs['browser.download.dir']).must_equal(File.expand_path('~/Downloads'))
|
|
300
|
+
_(prefs['browser.download.useDownloadDir']).must_equal(true)
|
|
301
|
+
_(prefs['browser.download.always_ask_before_handling_new_types']).must_equal(false)
|
|
302
|
+
_(prefs['browser.download.start_downloads_in_tmp_dir']).must_equal(false)
|
|
303
|
+
_(prefs['browser.download.open_pdf_attachments_inline']).must_equal(false)
|
|
304
|
+
_(prefs['pdfjs.disabled']).must_equal(true)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
it "has a window by default" do
|
|
308
|
+
_(driver_options(browser: :firefox).args).wont_include('-headless')
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
it "is headless where asked" do
|
|
312
|
+
_(driver_options(browser: :firefox, headless: true).args).must_include('-headless')
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
it "says it's whatever Firefox says by default" do
|
|
316
|
+
_(driver_options(browser: :firefox).prefs).wont_include('general.useragent.override')
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
it "says it's the user agent given" do
|
|
320
|
+
_(driver_options(browser: :firefox, user_agent: 'Agent/1.0').prefs['general.useragent.override']).must_equal('Agent/1.0')
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
it "downloads into the download directory given, expanded" do
|
|
324
|
+
_(driver_options(browser: :firefox, download_directory: '~/statements').prefs['browser.download.dir']).must_equal(File.expand_path('~/statements'))
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
it "is nil for other browsers" do
|
|
329
|
+
_(driver_options(browser: :safari)).must_be_nil
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
it "is the driver options given, in place of the defaults" do
|
|
333
|
+
_(driver_options(browser: :firefox, profile_directory: '~/profile', driver_options: :options)).must_equal(:options)
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
describe "#download_directory" do
|
|
338
|
+
it "is ~/Downloads by default" do
|
|
339
|
+
_(WebAccount.new.download_directory).must_equal(File.expand_path('~/Downloads'))
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
it "is the directory given, expanded" do
|
|
343
|
+
_(WebAccount.new(download_directory: '~/statements').download_directory).must_equal(File.expand_path('~/statements'))
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
describe "#wait_for_download" do
|
|
348
|
+
before do
|
|
349
|
+
@download_directory = Dir.mktmpdir
|
|
350
|
+
@since = Time.now - 60
|
|
351
|
+
@account = WebAccount.new(download_directory: @download_directory)
|
|
352
|
+
@account.instance_variable_set(:@download_wait, Selenium::WebDriver::Wait.new(timeout: 0.3, interval: 0.05))
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
after do
|
|
356
|
+
FileUtils.rm_rf(@download_directory)
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
def download(basename, at: Time.now)
|
|
360
|
+
filename = File.join(@download_directory, basename)
|
|
361
|
+
FileUtils.touch(filename)
|
|
362
|
+
File.utime(at, at, filename)
|
|
363
|
+
filename
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
it "waits up to default_download_timeout, rather than default_wait_timeout" do
|
|
367
|
+
account = WebAccount.new
|
|
368
|
+
_(account.send(:default_download_timeout)).must_equal(120)
|
|
369
|
+
_(account.send(:download_wait)).wont_be_same_as(account.send(:driver_wait))
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
def wait_for_download(**args)
|
|
373
|
+
@account.send(:wait_for_download, since: @since, **args)
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
it "is the newest CSV file since the time given" do
|
|
377
|
+
download('older.csv', at: @since + 10)
|
|
378
|
+
newer = download('newer.csv', at: @since + 20)
|
|
379
|
+
_(wait_for_download).must_equal(newer)
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
it "ignores files from before the time given" do
|
|
383
|
+
download('before.csv', at: @since - 10)
|
|
384
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
it "ignores files with another extension" do
|
|
388
|
+
download('statement.pdf')
|
|
389
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
it "finds the extension given" do
|
|
393
|
+
pdf = download('statement.pdf')
|
|
394
|
+
_(wait_for_download(extension: 'pdf')).must_equal(pdf)
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
it "ignores a file whose Chrome .crdownload is still there" do
|
|
398
|
+
download('statement.csv')
|
|
399
|
+
download('statement.csv.crdownload')
|
|
400
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
it "ignores a file whose Firefox .part is still there" do
|
|
404
|
+
download('12345678_20261103.csv')
|
|
405
|
+
download('12345678_20261103.Xy-9_aB3.csv.part')
|
|
406
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
it "ignores a file whose .part is named after it in full" do
|
|
410
|
+
download('statement.csv')
|
|
411
|
+
download('statement.csv.part')
|
|
412
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
it "takes the stem to be the name up to its first dot, as Firefox does" do
|
|
416
|
+
download('statement.2026.csv')
|
|
417
|
+
download('statement.Xy-9_aB3.2026.csv.part')
|
|
418
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
it "isn't put off by another file's .part" do
|
|
422
|
+
finished = download('finished.csv', at: @since + 10)
|
|
423
|
+
download('unfinished.csv', at: @since + 20)
|
|
424
|
+
download('unfinished.Xy-9_aB3.csv.part', at: @since + 20)
|
|
425
|
+
_(wait_for_download).must_equal(finished)
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
it "waits for the .part to go" do
|
|
429
|
+
finished = download('statement.csv')
|
|
430
|
+
part = download('statement.Xy-9_aB3.csv.part')
|
|
431
|
+
@account.instance_variable_set(:@download_wait, Selenium::WebDriver::Wait.new(timeout: 2, interval: 0.05))
|
|
432
|
+
remover = Thread.new{sleep 0.2; File.delete(part)}
|
|
433
|
+
_(wait_for_download).must_equal(finished)
|
|
434
|
+
remover.join
|
|
435
|
+
end
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
describe "#click" do
|
|
439
|
+
def account_with_element(displayed:)
|
|
440
|
+
element = Object.new
|
|
441
|
+
element.define_singleton_method(:displayed?){displayed}
|
|
442
|
+
element.define_singleton_method(:click){@clicked = true}
|
|
443
|
+
element.define_singleton_method(:clicked?){!!@clicked}
|
|
444
|
+
page = Object.new
|
|
445
|
+
page.define_singleton_method(:attempt){|&block| block.call(self)}
|
|
446
|
+
page.define_singleton_method(:execute_script){|script, *args| (@scripts ||= []) << [script, *args]}
|
|
447
|
+
page.define_singleton_method(:scripts){@scripts.to_a}
|
|
448
|
+
account = WebAccount.new
|
|
449
|
+
account.instance_variable_set(:@driver, page)
|
|
450
|
+
account.define_singleton_method(:find){|selector| element}
|
|
451
|
+
[account, page, element]
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
it "clicks an element which is displayed" do
|
|
455
|
+
account, page, element = account_with_element(displayed: true)
|
|
456
|
+
account.send(:click, id: 'link')
|
|
457
|
+
_(element.clicked?).must_equal(true)
|
|
458
|
+
_(page.scripts).must_be_empty
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
it "clicks an element which isn't displayed by JavaScript" do
|
|
462
|
+
account, page, element = account_with_element(displayed: false)
|
|
463
|
+
account.send(:click, id: 'link')
|
|
464
|
+
_(element.clicked?).must_equal(false)
|
|
465
|
+
_(page.scripts).must_equal([['arguments[0].click();', element]])
|
|
466
|
+
end
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
describe "#stale?" do
|
|
470
|
+
it "is false for an element still on the page" do
|
|
471
|
+
element = Object.new
|
|
472
|
+
element.define_singleton_method(:displayed?){true}
|
|
473
|
+
_(WebAccount.new.send(:stale?, element)).must_equal(false)
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
it "is true for an element gone from the page" do
|
|
477
|
+
element = Object.new
|
|
478
|
+
element.define_singleton_method(:displayed?){raise Selenium::WebDriver::Error::StaleElementReferenceError}
|
|
479
|
+
_(WebAccount.new.send(:stale?, element)).must_equal(true)
|
|
480
|
+
end
|
|
481
|
+
end
|
|
110
482
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: webaccount
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- thoran
|
|
@@ -80,9 +80,9 @@ files:
|
|
|
80
80
|
- lib/Thoran/Selenium.rb
|
|
81
81
|
- lib/Thoran/Selenium/WebDriver/Driver/Attempt/attempt.rb
|
|
82
82
|
- lib/Thoran/Selenium/WebDriver/Driver/ElementPresentQ/element_presentQ.rb
|
|
83
|
+
- lib/WebAccount/VERSION.rb
|
|
83
84
|
- lib/web_account.rb
|
|
84
85
|
- lib/webaccount.rb
|
|
85
|
-
- lib/webaccount/VERSION.rb
|
|
86
86
|
- test/Thoran/Selenium/WebDriver/Driver/Attempt_test.rb
|
|
87
87
|
- test/webaccount_test.rb
|
|
88
88
|
- webaccount.gemspec
|