webaccount 0.7.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e8acb9d277b85cb4e26f1fcb044f824fcf46b1af31da763c85b6b591914423f
4
- data.tar.gz: bb1d5087faa8a378554b51c259bd443106c4b416488647b646faae934b12a407
3
+ metadata.gz: b38d097c19e2af46497d256651f7edc1258f6789a04207a4c2e4db08835793ae
4
+ data.tar.gz: a967ff9f12d856ef060ed7feb13ef788104724d4c719d721786f226b560aea14
5
5
  SHA512:
6
- metadata.gz: e48376f067c4162165efd91f13ecf5eace38f102675cd3fe1884b62361ab4fc92c5a51a93c0a7d3a9006ee43dfa53558afaed027d8f43685666188bfa03ce98b
7
- data.tar.gz: d2fe92579b0194533de1e0a3e071d8515aed876c60e4cb7149b1d9a6da0b4dc85d5a558b5676570a3df982ca9f11dc55bf85760de0660fbfb8c2dc686309ca59
6
+ metadata.gz: 97b9776b4b8680bd730a606a757b94c37d40ded02a2f55e0e2630c6574efd99de25123d549476c54d9a16e42d0b009c6761083b9d8731882b3d7207f5ece39a1
7
+ data.tar.gz: 1de9f972a2fb3a5b4504eacf73f31c64d72170df31908611bd654188d6daec9e657dce9015c64ab2b492f8b7ba4ac6ea0dc6875bf7b10ce6aeda433509f3a1fa
data/CHANGELOG CHANGED
@@ -1,5 +1,23 @@
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
+
3
21
  ## 20261109
4
22
 
5
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.
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.
@@ -2,5 +2,5 @@
2
2
  # WebAccount::VERSION
3
3
 
4
4
  class WebAccount
5
- VERSION = '0.7.0'
5
+ VERSION = '0.8.0'
6
6
  end
data/lib/webaccount.rb CHANGED
@@ -6,6 +6,9 @@ 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
11
14
  attr_reader :download_directory
@@ -79,7 +82,7 @@ class WebAccount
79
82
 
80
83
  def logged_out?
81
84
  driver_wait.until do
82
- driver.element_present?(:xpath, @logged_out_xpath)
85
+ driver.element_present?(:xpath, logged_out_xpath)
83
86
  end
84
87
  end
85
88
 
@@ -227,10 +230,18 @@ class WebAccount
227
230
  Dir.children(File.dirname(filename)).any?{|child| child == "#{basename}.crdownload" || child.match?(firefox_part_file)}
228
231
  end
229
232
 
233
+ def page_selectors
234
+ self.class::PAGE_SELECTORS
235
+ end
236
+
230
237
  # logging in
231
238
 
239
+ def login_page_url
240
+ @login_page_url || page_selectors[:login_page_url]
241
+ end
242
+
232
243
  def login_page
233
- driver.get(@login_page_url)
244
+ driver.get(login_page_url || raise(ArgumentError, 'missing keyword: :login_page_url'))
234
245
  end
235
246
  alias_method :get_login_page, :login_page
236
247
 
@@ -239,16 +250,16 @@ class WebAccount
239
250
  username_field.send_keys(username)
240
251
  end
241
252
 
242
- def default_username_field_selector
243
- {name: 'username'}
253
+ def default_login_page_username_field_selector
254
+ default_username_field_selector
244
255
  end
245
256
 
246
- def username_field_selector
247
- @login_page_username_field_selector || default_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
248
259
  end
249
260
 
250
261
  def username_field
251
- find(username_field_selector)
262
+ find(login_page_username_field_selector)
252
263
  end
253
264
 
254
265
  def enter_password(password = nil)
@@ -256,16 +267,16 @@ class WebAccount
256
267
  password_field.send_keys(password)
257
268
  end
258
269
 
259
- def default_password_field_selector
260
- {name: 'password'}
270
+ def default_login_page_password_field_selector
271
+ default_password_field_selector
261
272
  end
262
273
 
263
- def password_field_selector
264
- @login_page_password_field_selector || default_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
265
276
  end
266
277
 
267
278
  def password_field
268
- find(password_field_selector)
279
+ find(login_page_password_field_selector)
269
280
  end
270
281
 
271
282
  def default_login_page_submit_button_selector
@@ -273,7 +284,7 @@ class WebAccount
273
284
  end
274
285
 
275
286
  def login_page_submit_button_selector
276
- @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
277
288
  end
278
289
 
279
290
  def login_page_submit_button
@@ -287,10 +298,32 @@ class WebAccount
287
298
  end
288
299
 
289
300
  def logout_button_selector
290
- @logout_button_selector || default_logout_button_selector
301
+ @logout_button_selector || page_selectors[:logout_button_selector] || default_logout_button_selector
291
302
  end
292
303
 
293
304
  def logout_button
294
305
  find(logout_button_selector)
295
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
296
329
  end
@@ -131,6 +131,102 @@ describe WebAccount do
131
131
  end
132
132
  end
133
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
+
134
230
  describe "#driver_options" do
135
231
  def driver_options(**args)
136
232
  WebAccount.new(**args).send(:driver_options)
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.7.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