webaccount 0.6.1 → 0.7.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 +19 -0
- data/README.md +53 -5
- data/lib/webaccount/VERSION.rb +1 -1
- data/lib/webaccount.rb +107 -2
- data/test/webaccount_test.rb +276 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2e8acb9d277b85cb4e26f1fcb044f824fcf46b1af31da763c85b6b591914423f
|
|
4
|
+
data.tar.gz: bb1d5087faa8a378554b51c259bd443106c4b416488647b646faae934b12a407
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e48376f067c4162165efd91f13ecf5eace38f102675cd3fe1884b62361ab4fc92c5a51a93c0a7d3a9006ee43dfa53558afaed027d8f43685666188bfa03ce98b
|
|
7
|
+
data.tar.gz: d2fe92579b0194533de1e0a3e071d8515aed876c60e4cb7149b1d9a6da0b4dc85d5a558b5676570a3df982ca9f11dc55bf85760de0660fbfb8c2dc686309ca59
|
data/CHANGELOG
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# webaccount/CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 20261109
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
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:.
|
|
8
|
+
2. + WebAccount#download_directory.
|
|
9
|
+
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.
|
|
10
|
+
4. ~ WebAccount#default_driver_options(): Chrome's or Firefox's options, by browser(); nil for other browsers, as before.
|
|
11
|
+
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.
|
|
12
|
+
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.
|
|
13
|
+
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.
|
|
14
|
+
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.
|
|
15
|
+
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.
|
|
16
|
+
10. + WebAccount#click(), #stale?(), #wait_for(), #set_value(), private.
|
|
17
|
+
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?().
|
|
18
|
+
12. ~ README.md: describes Chrome and Firefox, profile and download directories, headless, window size, user agent, waiting for downloads, and the page helpers.
|
|
19
|
+
13. ~ VERSION: /0.6.1/0.7.0/
|
|
20
|
+
|
|
21
|
+
|
|
3
22
|
## 20261103
|
|
4
23
|
|
|
5
24
|
0.6.1: ~ WebAccount#shutdown quits the browser even where logging out fails.
|
data/README.md
CHANGED
|
@@ -89,10 +89,33 @@ $ gem install webaccount
|
|
|
89
89
|
|
|
90
90
|
### Driver options
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
For Chrome and Firefox, WebAccount builds the options itself:
|
|
93
|
+
|
|
94
|
+
- 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.
|
|
95
|
+
- 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.
|
|
96
|
+
- With `headless: true`, the browser runs without a window.
|
|
97
|
+
- 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.
|
|
98
|
+
- Neither browser offers to save passwords.
|
|
99
|
+
- Chrome's automation flags are masked. Firefox has no preference that masks `navigator.webdriver`.
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
MyAccount.new(
|
|
103
|
+
username: username, password: password,
|
|
104
|
+
browser: :firefox,
|
|
105
|
+
profile_directory: '~/.my_account/profile',
|
|
106
|
+
download_directory: '~/statements',
|
|
107
|
+
headless: true,
|
|
108
|
+
window_size: [1280, 800]
|
|
109
|
+
)
|
|
110
|
+
```
|
|
111
|
+
For any other browser, the options are nil, which is Selenium's own default.
|
|
112
|
+
|
|
113
|
+
`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.
|
|
114
|
+
|
|
115
|
+
`driver_options` replaces all of this, apart from `window_size`. It is passed to `Selenium::WebDriver.for` as `options:`.
|
|
93
116
|
```ruby
|
|
94
117
|
options = Selenium::WebDriver::Chrome::Options.new
|
|
95
|
-
options.add_argument('--
|
|
118
|
+
options.add_argument('--lang=en-AU')
|
|
96
119
|
|
|
97
120
|
MyAccount.new(username: username, password: password, driver_options: options)
|
|
98
121
|
```
|
|
@@ -102,8 +125,8 @@ Or in a sub-class, if every instance should be configured the same way:
|
|
|
102
125
|
private
|
|
103
126
|
|
|
104
127
|
def default_driver_options
|
|
105
|
-
|
|
106
|
-
options.add_argument('--
|
|
128
|
+
default_chrome_options.tap do |options|
|
|
129
|
+
options.add_argument('--lang=en-AU')
|
|
107
130
|
end
|
|
108
131
|
end
|
|
109
132
|
end
|
|
@@ -111,12 +134,13 @@ Or in a sub-class, if every instance should be configured the same way:
|
|
|
111
134
|
|
|
112
135
|
### Timeouts
|
|
113
136
|
|
|
114
|
-
There are
|
|
137
|
+
There are three, each overridable in a sub-class:
|
|
115
138
|
|
|
116
139
|
| Method | Default | Waits for |
|
|
117
140
|
| --- | --- | --- |
|
|
118
141
|
| `default_find_timeout` | 5s | An element on a page which has already loaded. |
|
|
119
142
|
| `default_wait_timeout` | 30s | Navigation: a submit, the authentication behind it, and the redirect. |
|
|
143
|
+
| `default_download_timeout` | 120s | A download to finish, which takes as long as the file is large. |
|
|
120
144
|
|
|
121
145
|
```ruby
|
|
122
146
|
class SlowAccount < WebAccount
|
|
@@ -190,6 +214,30 @@ Prefer `find` over `driver.find_element`: it waits up to `default_find_timeout`
|
|
|
190
214
|
my_account.list_page
|
|
191
215
|
```
|
|
192
216
|
|
|
217
|
+
### Waiting for downloads
|
|
218
|
+
|
|
219
|
+
`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.
|
|
220
|
+
```ruby
|
|
221
|
+
class MyAccount < WebAccount
|
|
222
|
+
def download_statement
|
|
223
|
+
clicked_at = Time.now
|
|
224
|
+
click(id: 'download')
|
|
225
|
+
wait_for_download(since: clicked_at)
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Page helpers
|
|
231
|
+
|
|
232
|
+
These are private, for use in a sub-class:
|
|
233
|
+
|
|
234
|
+
| Method | Does |
|
|
235
|
+
| --- | --- |
|
|
236
|
+
| `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. |
|
|
237
|
+
| `stale?(element)` | Whether the element has gone from the page, for waiting out a page being redrawn. |
|
|
238
|
+
| `wait_for(selector)` | Waits up to `default_wait_timeout` for any displayed match. |
|
|
239
|
+
| `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. |
|
|
240
|
+
|
|
193
241
|
## Contributing
|
|
194
242
|
|
|
195
243
|
1. Fork it: https://github.com/thoran/webaccount/fork
|
data/lib/webaccount/VERSION.rb
CHANGED
data/lib/webaccount.rb
CHANGED
|
@@ -8,10 +8,13 @@ require_relative './WebAccount/VERSION'
|
|
|
8
8
|
class WebAccount
|
|
9
9
|
attr_accessor :username
|
|
10
10
|
attr_accessor :password
|
|
11
|
+
attr_reader :download_directory
|
|
11
12
|
|
|
12
13
|
def initialize(
|
|
13
14
|
username: nil, password: nil,
|
|
14
15
|
browser: nil, driver_options: nil,
|
|
16
|
+
profile_directory: nil, download_directory: File.join('~', 'Downloads'),
|
|
17
|
+
headless: false, window_size: nil, user_agent: nil,
|
|
15
18
|
login_page_url: nil,
|
|
16
19
|
login_page_username_field_selector: nil, login_page_password_field_selector: nil,
|
|
17
20
|
login_page_submit_button_selector: nil,
|
|
@@ -20,6 +23,9 @@ class WebAccount
|
|
|
20
23
|
)
|
|
21
24
|
@username, @password = username, password
|
|
22
25
|
@browser, @driver_options = browser, driver_options
|
|
26
|
+
@profile_directory = profile_directory && File.expand_path(profile_directory)
|
|
27
|
+
@download_directory = File.expand_path(download_directory)
|
|
28
|
+
@headless, @window_size, @user_agent = headless, window_size, user_agent
|
|
23
29
|
@login_page_url = login_page_url
|
|
24
30
|
@login_page_username_field_selector, @login_page_password_field_selector = login_page_username_field_selector, login_page_password_field_selector
|
|
25
31
|
@login_page_submit_button_selector = login_page_submit_button_selector
|
|
@@ -58,8 +64,11 @@ class WebAccount
|
|
|
58
64
|
driver.quit
|
|
59
65
|
end
|
|
60
66
|
|
|
67
|
+
# 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
68
|
def driver
|
|
62
|
-
@driver ||= Selenium::WebDriver.for(browser, options: driver_options)
|
|
69
|
+
@driver ||= Selenium::WebDriver.for(browser, options: driver_options).tap do |driver|
|
|
70
|
+
driver.manage.window.resize_to(*@window_size) if @window_size
|
|
71
|
+
end
|
|
63
72
|
end
|
|
64
73
|
|
|
65
74
|
# predicate methods
|
|
@@ -91,7 +100,45 @@ class WebAccount
|
|
|
91
100
|
end
|
|
92
101
|
|
|
93
102
|
def default_driver_options
|
|
94
|
-
|
|
103
|
+
case browser
|
|
104
|
+
when :chrome then default_chrome_options
|
|
105
|
+
when :firefox then default_firefox_options
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Chromedriver starts Chrome with a temporary profile unless given one. PDFs, which Chrome would otherwise show itself, are saved like anything else.
|
|
110
|
+
def default_chrome_options
|
|
111
|
+
Selenium::WebDriver::Chrome::Options.new.tap do |o|
|
|
112
|
+
o.add_argument("--user-data-dir=#{@profile_directory}") if @profile_directory
|
|
113
|
+
o.add_argument('--headless=new') if @headless
|
|
114
|
+
o.add_argument("--user-agent=#{@user_agent}") if @user_agent
|
|
115
|
+
o.add_argument('--disable-blink-features=AutomationControlled')
|
|
116
|
+
o.exclude_switches << 'enable-automation'
|
|
117
|
+
o.add_preference(:credentials_enable_service, false)
|
|
118
|
+
o.add_preference(:download, default_directory: @download_directory, prompt_for_download: false, directory_upgrade: true, extensions_to_open: '')
|
|
119
|
+
o.add_preference(:plugins, always_open_pdf_externally: true)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# 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.
|
|
124
|
+
def default_firefox_options
|
|
125
|
+
Selenium::WebDriver::Firefox::Options.new.tap do |o|
|
|
126
|
+
if @profile_directory
|
|
127
|
+
o.add_argument('-profile')
|
|
128
|
+
o.add_argument(@profile_directory)
|
|
129
|
+
end
|
|
130
|
+
o.add_argument('-headless') if @headless
|
|
131
|
+
o.add_preference('general.useragent.override', @user_agent) if @user_agent
|
|
132
|
+
o.add_preference('signon.rememberSignons', false)
|
|
133
|
+
o.add_preference('browser.download.folderList', 2)
|
|
134
|
+
o.add_preference('browser.download.dir', @download_directory)
|
|
135
|
+
o.add_preference('browser.download.useDownloadDir', true)
|
|
136
|
+
o.add_preference('browser.download.always_ask_before_handling_new_types', false)
|
|
137
|
+
o.add_preference('browser.download.start_downloads_in_tmp_dir', false)
|
|
138
|
+
o.add_preference('browser.download.alwaysOpenPanel', false)
|
|
139
|
+
o.add_preference('browser.download.open_pdf_attachments_inline', false)
|
|
140
|
+
o.add_preference('pdfjs.disabled', true)
|
|
141
|
+
end
|
|
95
142
|
end
|
|
96
143
|
|
|
97
144
|
def driver_options
|
|
@@ -118,10 +165,68 @@ class WebAccount
|
|
|
118
165
|
@find_wait ||= Selenium::WebDriver::Wait.new(timeout: default_find_timeout)
|
|
119
166
|
end
|
|
120
167
|
|
|
168
|
+
# For downloads: a file arriving, which takes as long as it's large.
|
|
169
|
+
def default_download_timeout
|
|
170
|
+
120
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def download_wait
|
|
174
|
+
@download_wait ||= Selenium::WebDriver::Wait.new(timeout: default_download_timeout)
|
|
175
|
+
end
|
|
176
|
+
|
|
121
177
|
def find(selector)
|
|
122
178
|
find_wait.until{driver.find_element(selector.keys.first, selector.values.first)}
|
|
123
179
|
end
|
|
124
180
|
|
|
181
|
+
def wait_for(selector)
|
|
182
|
+
driver_wait.until{driver.find_elements(selector.keys.first, selector.values.first).any?(&:displayed?)}
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# page interaction
|
|
186
|
+
|
|
187
|
+
# 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.
|
|
188
|
+
def click(selector)
|
|
189
|
+
driver.attempt do
|
|
190
|
+
element = find(selector)
|
|
191
|
+
element.displayed? ? element.click : driver.execute_script('arguments[0].click();', element)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def stale?(element)
|
|
196
|
+
element.displayed?
|
|
197
|
+
false
|
|
198
|
+
rescue Selenium::WebDriver::Error::StaleElementReferenceError
|
|
199
|
+
true
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# 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.
|
|
203
|
+
def set_value(element, value)
|
|
204
|
+
driver.execute_script("arguments[0].value = arguments[1]; arguments[0].dispatchEvent(new Event('change', {bubbles: true}));", element, value)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# downloads
|
|
208
|
+
|
|
209
|
+
# Waits up to default_download_timeout for a finished download.
|
|
210
|
+
def wait_for_download(since:, extension: 'csv')
|
|
211
|
+
download_wait.until{newest_download(since: since, extension: extension)}
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# The newest finished file with that extension in download_directory changed at or after since; nil where there's none.
|
|
215
|
+
def newest_download(since:, extension: 'csv')
|
|
216
|
+
Dir.glob("*.#{extension}", base: @download_directory)
|
|
217
|
+
.map{|basename| File.join(@download_directory, basename)}
|
|
218
|
+
.select{|filename| File.mtime(filename) >= since && !downloading?(filename)}
|
|
219
|
+
.max_by{|filename| File.mtime(filename)}
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# 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.
|
|
223
|
+
def downloading?(filename)
|
|
224
|
+
basename = File.basename(filename)
|
|
225
|
+
stem, extensions = basename.split('.', 2)
|
|
226
|
+
firefox_part_file = /\A#{Regexp.escape(stem)}\.([^.]+\.)?#{Regexp.escape(extensions.to_s)}\.part\z/
|
|
227
|
+
Dir.children(File.dirname(filename)).any?{|child| child == "#{basename}.crdownload" || child.match?(firefox_part_file)}
|
|
228
|
+
end
|
|
229
|
+
|
|
125
230
|
# logging in
|
|
126
231
|
|
|
127
232
|
def login_page
|
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,257 @@ describe WebAccount do
|
|
|
107
130
|
_(page.asked.length).must_equal(1)
|
|
108
131
|
end
|
|
109
132
|
end
|
|
133
|
+
|
|
134
|
+
describe "#driver_options" do
|
|
135
|
+
def driver_options(**args)
|
|
136
|
+
WebAccount.new(**args).send(:driver_options)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
describe "Chrome" do
|
|
140
|
+
it "is the browser by default" do
|
|
141
|
+
_(driver_options).must_be_instance_of(Selenium::WebDriver::Chrome::Options)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it "starts with a fresh profile where none is given" do
|
|
145
|
+
_(driver_options(browser: :chrome).args.grep(/--user-data-dir/)).must_be_empty
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it "starts with the profile given, expanded" do
|
|
149
|
+
_(driver_options(browser: :chrome, profile_directory: '~/profile').args).must_include("--user-data-dir=#{File.expand_path('~/profile')}")
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
it "masks the automation flags" do
|
|
153
|
+
options = driver_options(browser: :chrome)
|
|
154
|
+
_(options.args).must_include('--disable-blink-features=AutomationControlled')
|
|
155
|
+
_(options.exclude_switches).must_include('enable-automation')
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it "downloads into ~/Downloads by default, without asking, opening nothing" do
|
|
159
|
+
_(driver_options(browser: :chrome).prefs[:download]).must_equal({default_directory: File.expand_path('~/Downloads'), prompt_for_download: false, directory_upgrade: true, extensions_to_open: ''})
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
it "saves PDFs rather than showing them" do
|
|
163
|
+
_(driver_options(browser: :chrome).prefs[:plugins]).must_equal({always_open_pdf_externally: true})
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it "has a window by default" do
|
|
167
|
+
_(driver_options(browser: :chrome).args.grep(/headless/)).must_be_empty
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
it "is headless where asked" do
|
|
171
|
+
_(driver_options(browser: :chrome, headless: true).args).must_include('--headless=new')
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
it "says it's whatever Chrome says by default" do
|
|
175
|
+
_(driver_options(browser: :chrome).args.grep(/--user-agent/)).must_be_empty
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
it "says it's the user agent given" do
|
|
179
|
+
_(driver_options(browser: :chrome, user_agent: 'Agent/1.0').args).must_include('--user-agent=Agent/1.0')
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
it "downloads into the download directory given, expanded" do
|
|
183
|
+
_(driver_options(browser: :chrome, download_directory: '~/statements').prefs[:download][:default_directory]).must_equal(File.expand_path('~/statements'))
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
describe "Firefox" do
|
|
188
|
+
it "is Firefox's options" do
|
|
189
|
+
_(driver_options(browser: 'Firefox')).must_be_instance_of(Selenium::WebDriver::Firefox::Options)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
it "starts with a fresh profile where none is given" do
|
|
193
|
+
_(driver_options(browser: :firefox).args).wont_include('-profile')
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
it "starts with the profile given, expanded, in place" do
|
|
197
|
+
_(driver_options(browser: :firefox, profile_directory: '~/profile').args).must_equal(['-profile', File.expand_path('~/profile')])
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
it "downloads into ~/Downloads by default, without asking, opening nothing" do
|
|
201
|
+
prefs = driver_options(browser: :firefox).prefs
|
|
202
|
+
_(prefs['browser.download.folderList']).must_equal(2)
|
|
203
|
+
_(prefs['browser.download.dir']).must_equal(File.expand_path('~/Downloads'))
|
|
204
|
+
_(prefs['browser.download.useDownloadDir']).must_equal(true)
|
|
205
|
+
_(prefs['browser.download.always_ask_before_handling_new_types']).must_equal(false)
|
|
206
|
+
_(prefs['browser.download.start_downloads_in_tmp_dir']).must_equal(false)
|
|
207
|
+
_(prefs['browser.download.open_pdf_attachments_inline']).must_equal(false)
|
|
208
|
+
_(prefs['pdfjs.disabled']).must_equal(true)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
it "has a window by default" do
|
|
212
|
+
_(driver_options(browser: :firefox).args).wont_include('-headless')
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
it "is headless where asked" do
|
|
216
|
+
_(driver_options(browser: :firefox, headless: true).args).must_include('-headless')
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
it "says it's whatever Firefox says by default" do
|
|
220
|
+
_(driver_options(browser: :firefox).prefs).wont_include('general.useragent.override')
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
it "says it's the user agent given" do
|
|
224
|
+
_(driver_options(browser: :firefox, user_agent: 'Agent/1.0').prefs['general.useragent.override']).must_equal('Agent/1.0')
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
it "downloads into the download directory given, expanded" do
|
|
228
|
+
_(driver_options(browser: :firefox, download_directory: '~/statements').prefs['browser.download.dir']).must_equal(File.expand_path('~/statements'))
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
it "is nil for other browsers" do
|
|
233
|
+
_(driver_options(browser: :safari)).must_be_nil
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
it "is the driver options given, in place of the defaults" do
|
|
237
|
+
_(driver_options(browser: :firefox, profile_directory: '~/profile', driver_options: :options)).must_equal(:options)
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
describe "#download_directory" do
|
|
242
|
+
it "is ~/Downloads by default" do
|
|
243
|
+
_(WebAccount.new.download_directory).must_equal(File.expand_path('~/Downloads'))
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
it "is the directory given, expanded" do
|
|
247
|
+
_(WebAccount.new(download_directory: '~/statements').download_directory).must_equal(File.expand_path('~/statements'))
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
describe "#wait_for_download" do
|
|
252
|
+
before do
|
|
253
|
+
@download_directory = Dir.mktmpdir
|
|
254
|
+
@since = Time.now - 60
|
|
255
|
+
@account = WebAccount.new(download_directory: @download_directory)
|
|
256
|
+
@account.instance_variable_set(:@download_wait, Selenium::WebDriver::Wait.new(timeout: 0.3, interval: 0.05))
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
after do
|
|
260
|
+
FileUtils.rm_rf(@download_directory)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def download(basename, at: Time.now)
|
|
264
|
+
filename = File.join(@download_directory, basename)
|
|
265
|
+
FileUtils.touch(filename)
|
|
266
|
+
File.utime(at, at, filename)
|
|
267
|
+
filename
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
it "waits up to default_download_timeout, rather than default_wait_timeout" do
|
|
271
|
+
account = WebAccount.new
|
|
272
|
+
_(account.send(:default_download_timeout)).must_equal(120)
|
|
273
|
+
_(account.send(:download_wait)).wont_be_same_as(account.send(:driver_wait))
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def wait_for_download(**args)
|
|
277
|
+
@account.send(:wait_for_download, since: @since, **args)
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
it "is the newest CSV file since the time given" do
|
|
281
|
+
download('older.csv', at: @since + 10)
|
|
282
|
+
newer = download('newer.csv', at: @since + 20)
|
|
283
|
+
_(wait_for_download).must_equal(newer)
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
it "ignores files from before the time given" do
|
|
287
|
+
download('before.csv', at: @since - 10)
|
|
288
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
it "ignores files with another extension" do
|
|
292
|
+
download('statement.pdf')
|
|
293
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
it "finds the extension given" do
|
|
297
|
+
pdf = download('statement.pdf')
|
|
298
|
+
_(wait_for_download(extension: 'pdf')).must_equal(pdf)
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
it "ignores a file whose Chrome .crdownload is still there" do
|
|
302
|
+
download('statement.csv')
|
|
303
|
+
download('statement.csv.crdownload')
|
|
304
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
it "ignores a file whose Firefox .part is still there" do
|
|
308
|
+
download('12345678_20261103.csv')
|
|
309
|
+
download('12345678_20261103.Xy-9_aB3.csv.part')
|
|
310
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
it "ignores a file whose .part is named after it in full" do
|
|
314
|
+
download('statement.csv')
|
|
315
|
+
download('statement.csv.part')
|
|
316
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
it "takes the stem to be the name up to its first dot, as Firefox does" do
|
|
320
|
+
download('statement.2026.csv')
|
|
321
|
+
download('statement.Xy-9_aB3.2026.csv.part')
|
|
322
|
+
_{wait_for_download}.must_raise(Selenium::WebDriver::Error::TimeoutError)
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
it "isn't put off by another file's .part" do
|
|
326
|
+
finished = download('finished.csv', at: @since + 10)
|
|
327
|
+
download('unfinished.csv', at: @since + 20)
|
|
328
|
+
download('unfinished.Xy-9_aB3.csv.part', at: @since + 20)
|
|
329
|
+
_(wait_for_download).must_equal(finished)
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
it "waits for the .part to go" do
|
|
333
|
+
finished = download('statement.csv')
|
|
334
|
+
part = download('statement.Xy-9_aB3.csv.part')
|
|
335
|
+
@account.instance_variable_set(:@download_wait, Selenium::WebDriver::Wait.new(timeout: 2, interval: 0.05))
|
|
336
|
+
remover = Thread.new{sleep 0.2; File.delete(part)}
|
|
337
|
+
_(wait_for_download).must_equal(finished)
|
|
338
|
+
remover.join
|
|
339
|
+
end
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
describe "#click" do
|
|
343
|
+
def account_with_element(displayed:)
|
|
344
|
+
element = Object.new
|
|
345
|
+
element.define_singleton_method(:displayed?){displayed}
|
|
346
|
+
element.define_singleton_method(:click){@clicked = true}
|
|
347
|
+
element.define_singleton_method(:clicked?){!!@clicked}
|
|
348
|
+
page = Object.new
|
|
349
|
+
page.define_singleton_method(:attempt){|&block| block.call(self)}
|
|
350
|
+
page.define_singleton_method(:execute_script){|script, *args| (@scripts ||= []) << [script, *args]}
|
|
351
|
+
page.define_singleton_method(:scripts){@scripts.to_a}
|
|
352
|
+
account = WebAccount.new
|
|
353
|
+
account.instance_variable_set(:@driver, page)
|
|
354
|
+
account.define_singleton_method(:find){|selector| element}
|
|
355
|
+
[account, page, element]
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
it "clicks an element which is displayed" do
|
|
359
|
+
account, page, element = account_with_element(displayed: true)
|
|
360
|
+
account.send(:click, id: 'link')
|
|
361
|
+
_(element.clicked?).must_equal(true)
|
|
362
|
+
_(page.scripts).must_be_empty
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
it "clicks an element which isn't displayed by JavaScript" do
|
|
366
|
+
account, page, element = account_with_element(displayed: false)
|
|
367
|
+
account.send(:click, id: 'link')
|
|
368
|
+
_(element.clicked?).must_equal(false)
|
|
369
|
+
_(page.scripts).must_equal([['arguments[0].click();', element]])
|
|
370
|
+
end
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
describe "#stale?" do
|
|
374
|
+
it "is false for an element still on the page" do
|
|
375
|
+
element = Object.new
|
|
376
|
+
element.define_singleton_method(:displayed?){true}
|
|
377
|
+
_(WebAccount.new.send(:stale?, element)).must_equal(false)
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
it "is true for an element gone from the page" do
|
|
381
|
+
element = Object.new
|
|
382
|
+
element.define_singleton_method(:displayed?){raise Selenium::WebDriver::Error::StaleElementReferenceError}
|
|
383
|
+
_(WebAccount.new.send(:stale?, element)).must_equal(true)
|
|
384
|
+
end
|
|
385
|
+
end
|
|
110
386
|
end
|