webaccount 0.7.0 → 0.9.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: 6bfe32e37ba437205c4ec943500e4a0c819eff7c9f3bf6adb27c5b37e1d729ee
4
+ data.tar.gz: 100ddd0d21d047b45bfa2c95b030cb0dbabf4740fa7e2045c9c3793ae1c338da
5
5
  SHA512:
6
- metadata.gz: e48376f067c4162165efd91f13ecf5eace38f102675cd3fe1884b62361ab4fc92c5a51a93c0a7d3a9006ee43dfa53558afaed027d8f43685666188bfa03ce98b
7
- data.tar.gz: d2fe92579b0194533de1e0a3e071d8515aed876c60e4cb7149b1d9a6da0b4dc85d5a558b5676570a3df982ca9f11dc55bf85760de0660fbfb8c2dc686309ca59
6
+ metadata.gz: 1184c9709bdbda58b943e27e73c1ad7ef70bf8010a6cb7f47b24da63568182946726cc95da2b26597df272a8f8e9e0543b516e1a61b8ca201f02b4a52568d995
7
+ data.tar.gz: eeef9cb683e28e4be6611c6872851f39f6c1e628dc7b35e7e15dfc6f9ef7c7d2068b111afb50d5b59df28b3c367d152340142a750235607c7601b7def3c3fa88
data/CHANGELOG CHANGED
@@ -1,21 +1,57 @@
1
1
  # webaccount/CHANGELOG
2
2
 
3
+ ## 20261115
4
+
5
+ 0.9.0: WebAccount page selectors are now public, each with a setter; and the logged-out page is found by a selector (logged_out_selector).
6
+
7
+ 1. ~ WebAccount#login_page_url(), #login_page_username_field_selector(), #login_page_password_field_selector(), #login_page_submit_button_selector(), #logout_button_selector(): Public.
8
+ 2. + WebAccount#login_page_url=(), #login_page_username_field_selector=(), #login_page_password_field_selector=(), #login_page_submit_button_selector=(), #logout_button_selector=(), #logged_out_selector=(), #logged_out_xpath=().
9
+ 3. ~ WebAccount.new(): + logged_out_selector:.
10
+ 4. + WebAccount#logged_out_selector(): The value given or set, else page_selectors' value. A logged_out_xpath is taken as {xpath: ...}, after a logged_out_selector given the same way.
11
+ 5. ~ WebAccount#logged_out_xpath(): Public. Deprecated for logged_out_selector until 0.10.0.
12
+ 6. ~ WebAccount#logged_out?(): Looks for logged_out_selector, and raises ArgumentError naming logged_out_selector: where there is none, rather than handing nil to the browser.
13
+ 7. + WebAccount#required(): The named value, or ArgumentError naming it.
14
+ 8. ~ WebAccount#login_page(): Uses required().
15
+ 9. + WebAccount#xpath_selector().
16
+ 10. - WebAccount#username_field_selector(), #password_field_selector(), #default_username_field_selector(), #default_password_field_selector(): Deprecated in 0.8.0.
17
+ 11. ~ WebAccount#default_login_page_username_field_selector(), #default_login_page_password_field_selector(): The defaults themselves.
18
+ 12. ~ test/webaccount_test.rb: Each of the six is public with a setter, and the value set wins over PAGE_SELECTORS; logged_out_xpath given as a keyword, in PAGE_SELECTORS or set, and giving way to logged_out_selector; the names before 0.8.0's are gone; logged_out? looks for logged_out_selector, and names it where there is none.
19
+ 13. ~ README.md: logged_out_selector in place of logged_out_xpath. /Where each page value comes from/Where each page selector comes from/. The setters are shown, and the readers are public. logged_out_xpath's deprecation in place of the old names'.
20
+ 14. ~ CHANGELOG: The markers and capitals of 0.4.1's to 0.8.0's entries are corrected.
21
+ 15. ~ VERSION: /0.8.0/0.9.0/
22
+
23
+ 0.8.0: WebAccount reads a sub-class's PAGE_SELECTORS, so that a sub-class needn't override initialize to pass them in.
24
+
25
+ 1. + WebAccount::PAGE_SELECTORS: {}.
26
+ 2. + WebAccount#page_selectors(): The class's PAGE_SELECTORS.
27
+ 3. + WebAccount#login_page_url(), #logged_out_xpath(): The keyword if given, else page_selectors' value.
28
+ 4. /WebAccount#username_field_selector()/login_page_username_field_selector()/, /WebAccount#password_field_selector()/login_page_password_field_selector()/: Named as their keywords are.
29
+ 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.
30
+ 6. ~ WebAccount#username_field_selector(), #password_field_selector(): Until 0.9.0, the readers by their old names.
31
+ 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.
32
+ 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.
33
+ 9. ~ WebAccount#logged_out?(): Looks for logged_out_xpath.
34
+ 10. ~ test/webaccount_test.rb: Each of the six is tested with neither, with a sub-class's PAGE_SELECTORS, and with the keyword winning over it; and so are page_selectors overridden, keeping a parent's values, the old names until 0.9.0, and login_page naming the missing login_page_url:.
35
+ 11. ~ README.md: MyAccount.new() is called without PAGE_SELECTORS merged into it. The keywords are said to win over PAGE_SELECTORS. + Where each page value comes from, giving the order each reader takes, the old names until 0.9.0, and overriding PAGE_SELECTORS, page_selectors or a single reader.
36
+ 12. ~ VERSION: /0.7.0/0.8.0/
37
+
38
+
3
39
  ## 20261109
4
40
 
5
41
  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
42
 
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:.
43
+ 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
44
  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.
45
+ 3. ~ WebAccount#driver(): The window is 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
46
  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.
47
+ 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.
48
+ 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.
49
+ 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.
50
+ 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.
51
+ 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
52
  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.
53
+ 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?().
54
+ 12. ~ README.md: Describes Chrome and Firefox, profile and download directories, headless, window size, user agent, waiting for downloads, and the page helpers.
19
55
  13. ~ VERSION: /0.6.1/0.7.0/
20
56
 
21
57
 
@@ -25,9 +61,9 @@
25
61
 
26
62
  A logout which raised, its button not found or the logged-out marker not appearing, left the browser open and still logged in.
27
63
 
28
- 1. ~ WebAccount#shutdown(): driver.quit in an ensure.
29
- 2. ~ test/webaccount_test.rb: + shutdown logs out then quits, quits where logging out raises, and doesn't log out where not logged in. The second fails against 0.6.0.
30
- 3. ~ README.md: shutdown kills the session even if logging out fails.
64
+ 1. ~ WebAccount#shutdown(): Quits the driver in an ensure.
65
+ 2. ~ test/webaccount_test.rb: Tests that shutdown logs out then quits, quits where logging out raises, and doesn't log out where not logged in. The second fails against 0.6.0.
66
+ 3. ~ README.md: Says that shutdown kills the session even if logging out fails.
31
67
  4. ~ VERSION: /0.6.0/0.6.1/
32
68
 
33
69
 
@@ -37,8 +73,8 @@ A logout which raised, its button not found or the logged-out marker not appeari
37
73
 
38
74
  logged_in? answers from what login() found, and is not told when a session expires afterwards; logged_out? waits up to default_wait_timeout, 30s, for the logged-out marker, which is too long to ask of every page. Something driving the browser from page to page, as 0.5.0 lets it, had no quick way to notice that the session had ended underneath it.
39
75
 
40
- 1. + WebAccount#still_logged_in?(): whether the logout button is on the page now showing, by element_present?, which does not wait; and logged_in? is brought into line with the answer.
41
- 2. ~ test/webaccount_test.rb: + true and false with logged_in? following, and a single look rather than a wait, with a stand-in for the browser. All three fail against 0.5.0.
76
+ 1. + WebAccount#still_logged_in?(): Whether the logout button is on the page now showing, by element_present?, which does not wait; and logged_in? is brought into line with the answer.
77
+ 2. ~ test/webaccount_test.rb: Tests of true and false, with logged_in? following, and of a single look rather than a wait, with a stand-in for the browser. All three fail against 0.5.0.
42
78
  3. ~ README.md: + still_logged_in?, under Driving the browser once logged in; /WebAccount\/fork/webaccount\/fork/ in Contributing, the repository having been renamed webaccount.
43
79
  4. ~ CHANGELOG: /# CHANGELOG/# webaccount\/CHANGELOG/, after the repository.
44
80
  5. ~ VERSION: /0.5.0/0.6.0/
@@ -47,8 +83,8 @@ logged_in? answers from what login() found, and is not told when a session expir
47
83
 
48
84
  driver was private, so once login() had done its work the session it made could be used only by a sub-class. Something which wants the pages behind a login without being a WebAccount, a spider fetching and screenshotting them, had no way to reach the browser holding the session.
49
85
 
50
- 1. ~ WebAccount#driver(): public, after shutdown(); what it does is unchanged, the browser being made once, with browser() and driver_options(), upon first being asked for.
51
- 2. + test/webaccount_test.rb: driver public, made once, and made with the browser and driver options given, with Selenium::WebDriver.for swapped for a stand-in so that no browser is wanted. All three fail against 0.4.1.
86
+ 1. ~ WebAccount#driver(): Public, after shutdown(). What it does is unchanged, the browser being made once, with browser() and driver_options(), upon first being asked for.
87
+ 2. + test/webaccount_test.rb: Tests that driver is public, made once, and made with the browser and driver options given, with Selenium::WebDriver.for swapped for a stand-in so that no browser is wanted. All three fail against 0.4.1.
52
88
  3. ~ README.md: + Driving the browser once logged in.
53
89
  4. ~ VERSION: /0.4.1/0.5.0/
54
90
 
@@ -57,9 +93,9 @@ driver was private, so once login() had done its work the session it made could
57
93
 
58
94
  0.4.1: + LICENSE, which the gemspec has claimed without one being present.
59
95
 
60
- 1. + LICENSE: the MIT text, copyright 2020-2026 thoran. spec.license has named MIT with no licence text in the repository, so the gem shipped a licence nobody could read.
96
+ 1. + LICENSE: The MIT text, copyright 2020-2026 thoran. spec.license has named MIT with no licence text in the repository, so the gem shipped a licence nobody could read.
61
97
  2. ~ webaccount.gemspec: + 'LICENSE' to spec.files, without which the file sits in the repository and still does not ship.
62
- 3. + README.md: a License section, saying MIT, last.
98
+ 3. ~ README.md: + A License section, saying MIT, last.
63
99
  4. ~ VERSION: /0.4.0/0.4.1/
64
100
 
65
101
 
data/README.md CHANGED
@@ -33,7 +33,7 @@ $ gem install webaccount
33
33
  # => #<MyAccount @username=username, @password=password, @login_page_url=nil,
34
34
  # @login_page_password_field_selector=nil, @login_page_username_field_selector=nil,
35
35
  # @login_page_submit_button_selector=nil,
36
- # @logout_button_selector=nil, @logged_out_xpath=nil>
36
+ # @logout_button_selector=nil, @logged_out_selector=nil>
37
37
  ```
38
38
 
39
39
  #### 2. With explicit defaults, then in the comment the ultimate defaults if those values remain nil
@@ -47,14 +47,11 @@ $ gem install webaccount
47
47
  login_page_password_field_selector: nil, # Will default to: {name: 'password'}
48
48
  login_page_submit_button_selector: nil, # Will default to: {xpath: '//input[@type="submit" and @value="Login"]'}
49
49
  logout_button_selector: nil, # Will default to: {xpath: '//input[@type="submit" and @value="Logout"]'}
50
- logged_out_xpath: nil, # No default value for this, and logout and shutdown require it.
50
+ logged_out_selector: nil, # No default value for this, and logout and shutdown require it.
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
 
@@ -69,17 +66,59 @@ $ gem install webaccount
69
66
  login_page_password_field_selector: {id: 'password'},
70
67
  login_page_submit_button_selector: {xpath: '//button[@type="submit" and @value="Login"]'},
71
68
  logout_button_selector: {css: '#button-logout'},
72
- logged_out_xpath: '//div[@class="logged-out"]',
69
+ logged_out_selector: {xpath: '//div[@class="logged-out"]'},
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`, and has a setter, either of 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
+ my_account = MyAccount.new(username: username, password: password)
82
+ my_account.login_page_url = 'https://test.mysite.com/login'
83
+ ```
84
+ `login` raises `ArgumentError`, naming `login_page_url:`, if there's no login page URL, and `logout` likewise, naming `logged_out_selector:`, if there's no logged-out selector.
85
+
86
+ ### Where each page selector comes from
87
+
88
+ Each value is found by a public reader of the same name as its keyword, as `login_page_url` or `logout_button_selector`, which takes the first of:
89
+
90
+ 1. the value given to `new` or set;
91
+ 2. that key of `page_selectors`, which is the class's `PAGE_SELECTORS`;
92
+ 3. for the four selectors on the login page and the logout button, `default_` and the same name, as `default_logout_button_selector`. The login page URL and the logged-out selector have no default.
93
+
94
+ The logged-out selector was a bare XPath named `logged_out_xpath` before 0.9.0. That name stays until 0.10.0, as a keyword, a key of `PAGE_SELECTORS` and an accessor, its XPath being taken as `{xpath: ...}`, and `logged_out_selector` given the same way winning over it.
95
+
96
+ So a sub-class can give its values at any of three levels, from the coarsest to the finest.
97
+
98
+ `PAGE_SELECTORS`, for values which are fixed, as in 2 and 3 above.
99
+
100
+ `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:
101
+ ```ruby
102
+ class MyBusinessAccount < MyAccount
103
+ private
104
+
105
+ def page_selectors
106
+ super.merge(login_page_url: 'https://business.mysite.com/login')
107
+ end
108
+ end
109
+ ```
110
+
111
+ A single reader, for one value which has to be worked out. Starting with `super ||` keeps the keyword winning:
112
+ ```ruby
113
+ class MyAccount < WebAccount
114
+ PAGE_SELECTORS = {...}
115
+
116
+ def login_page_url
117
+ super || "https://mysite.com/login?t=#{Time.now.to_i}"
118
+ end
119
+ end
120
+ ```
121
+
83
122
  ### Choosing a browser
84
123
 
85
124
  `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.9.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
@@ -19,6 +22,7 @@ class WebAccount
19
22
  login_page_username_field_selector: nil, login_page_password_field_selector: nil,
20
23
  login_page_submit_button_selector: nil,
21
24
  logout_button_selector: nil,
25
+ logged_out_selector: nil,
22
26
  logged_out_xpath: nil
23
27
  )
24
28
  @username, @password = username, password
@@ -30,6 +34,7 @@ class WebAccount
30
34
  @login_page_username_field_selector, @login_page_password_field_selector = login_page_username_field_selector, login_page_password_field_selector
31
35
  @login_page_submit_button_selector = login_page_submit_button_selector
32
36
  @logout_button_selector = logout_button_selector
37
+ @logged_out_selector = logged_out_selector
33
38
  @logged_out_xpath = logged_out_xpath
34
39
  end
35
40
 
@@ -78,8 +83,9 @@ class WebAccount
78
83
  end
79
84
 
80
85
  def logged_out?
86
+ selector = required(:logged_out_selector)
81
87
  driver_wait.until do
82
- driver.element_present?(:xpath, @logged_out_xpath)
88
+ driver.element_present?(selector.keys.first, selector.values.first)
83
89
  end
84
90
  end
85
91
 
@@ -87,8 +93,57 @@ class WebAccount
87
93
  @logged_in = driver.element_present?(logout_button_selector.keys.first, logout_button_selector.values.first)
88
94
  end
89
95
 
96
+ # page selectors
97
+
98
+ attr_writer :login_page_url
99
+ attr_writer :login_page_username_field_selector
100
+ attr_writer :login_page_password_field_selector
101
+ attr_writer :login_page_submit_button_selector
102
+ attr_writer :logout_button_selector
103
+ attr_writer :logged_out_selector
104
+
105
+ def login_page_url
106
+ @login_page_url || page_selectors[:login_page_url]
107
+ end
108
+
109
+ def login_page_username_field_selector
110
+ @login_page_username_field_selector || page_selectors[:login_page_username_field_selector] || default_login_page_username_field_selector
111
+ end
112
+
113
+ def login_page_password_field_selector
114
+ @login_page_password_field_selector || page_selectors[:login_page_password_field_selector] || default_login_page_password_field_selector
115
+ end
116
+
117
+ def login_page_submit_button_selector
118
+ @login_page_submit_button_selector || page_selectors[:login_page_submit_button_selector] || default_login_page_submit_button_selector
119
+ end
120
+
121
+ def logout_button_selector
122
+ @logout_button_selector || page_selectors[:logout_button_selector] || default_logout_button_selector
123
+ end
124
+
125
+ def logged_out_selector
126
+ @logged_out_selector || xpath_selector(@logged_out_xpath) || page_selectors[:logged_out_selector] || xpath_selector(page_selectors[:logged_out_xpath])
127
+ end
128
+
129
+ # logged_out_xpath deprecated for logged_out_selector. Keep till 0.10.0.
130
+
131
+ attr_writer :logged_out_xpath
132
+
133
+ def logged_out_xpath
134
+ @logged_out_xpath || page_selectors[:logged_out_xpath]
135
+ end
136
+
90
137
  private
91
138
 
139
+ def required(name)
140
+ send(name) || raise(ArgumentError, "missing keyword: :#{name}")
141
+ end
142
+
143
+ def xpath_selector(xpath)
144
+ xpath && {xpath: xpath}
145
+ end
146
+
92
147
  # driver construction
93
148
 
94
149
  def default_browser
@@ -227,10 +282,14 @@ class WebAccount
227
282
  Dir.children(File.dirname(filename)).any?{|child| child == "#{basename}.crdownload" || child.match?(firefox_part_file)}
228
283
  end
229
284
 
285
+ def page_selectors
286
+ self.class::PAGE_SELECTORS
287
+ end
288
+
230
289
  # logging in
231
290
 
232
291
  def login_page
233
- driver.get(@login_page_url)
292
+ driver.get(required(:login_page_url))
234
293
  end
235
294
  alias_method :get_login_page, :login_page
236
295
 
@@ -239,16 +298,12 @@ class WebAccount
239
298
  username_field.send_keys(username)
240
299
  end
241
300
 
242
- def default_username_field_selector
301
+ def default_login_page_username_field_selector
243
302
  {name: 'username'}
244
303
  end
245
304
 
246
- def username_field_selector
247
- @login_page_username_field_selector || default_username_field_selector
248
- end
249
-
250
305
  def username_field
251
- find(username_field_selector)
306
+ find(login_page_username_field_selector)
252
307
  end
253
308
 
254
309
  def enter_password(password = nil)
@@ -256,26 +311,18 @@ class WebAccount
256
311
  password_field.send_keys(password)
257
312
  end
258
313
 
259
- def default_password_field_selector
314
+ def default_login_page_password_field_selector
260
315
  {name: 'password'}
261
316
  end
262
317
 
263
- def password_field_selector
264
- @login_page_password_field_selector || default_password_field_selector
265
- end
266
-
267
318
  def password_field
268
- find(password_field_selector)
319
+ find(login_page_password_field_selector)
269
320
  end
270
321
 
271
322
  def default_login_page_submit_button_selector
272
323
  {xpath: '//input[@type="submit" and @value="Login"]'}
273
324
  end
274
325
 
275
- def login_page_submit_button_selector
276
- @login_page_submit_button_selector || default_login_page_submit_button_selector
277
- end
278
-
279
326
  def login_page_submit_button
280
327
  find(login_page_submit_button_selector)
281
328
  end
@@ -286,10 +333,6 @@ class WebAccount
286
333
  {xpath: '//input[@type="submit" and @value="Logout"]'}
287
334
  end
288
335
 
289
- def logout_button_selector
290
- @logout_button_selector || default_logout_button_selector
291
- end
292
-
293
336
  def logout_button
294
337
  find(logout_button_selector)
295
338
  end
@@ -131,6 +131,145 @@ 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: nil,
141
+ login_page_username_field_selector: {name: 'username'},
142
+ login_page_password_field_selector: {name: 'password'},
143
+ login_page_submit_button_selector: {xpath: '//input[@type="submit" and @value="Login"]'},
144
+ logout_button_selector: {xpath: '//input[@type="submit" and @value="Logout"]'},
145
+ logged_out_selector: nil,
146
+ }.each do |name, default|
147
+ describe "##{name}" do
148
+ it "is public, as is its setter" do
149
+ _(WebAccount.public_method_defined?(name)).must_equal(true)
150
+ _(WebAccount.public_method_defined?(:"#{name}=")).must_equal(true)
151
+ end
152
+
153
+ it "is WebAccount's own where neither #{name}: nor PAGE_SELECTORS gives one" do
154
+ value = WebAccount.new.public_send(name)
155
+ default ? _(value).must_equal(default) : _(value).must_be_nil
156
+ end
157
+
158
+ it "is a sub-class's PAGE_SELECTORS[:#{name}]" do
159
+ _(account_class(name => 'from the sub-class').new.public_send(name)).must_equal('from the sub-class')
160
+ end
161
+
162
+ it "is #{name}: over a sub-class's PAGE_SELECTORS" do
163
+ _(account_class(name => 'from the sub-class').new(name => 'given').public_send(name)).must_equal('given')
164
+ end
165
+
166
+ it "is the value set, over a sub-class's PAGE_SELECTORS" do
167
+ account = account_class(name => 'from the sub-class').new
168
+ account.public_send(:"#{name}=", 'set')
169
+ _(account.public_send(name)).must_equal('set')
170
+ end
171
+ end
172
+ end
173
+ end
174
+
175
+ describe "logged_out_xpath, until 0.10.0" do
176
+ def account_class(page_selectors)
177
+ Class.new(WebAccount){const_set(:PAGE_SELECTORS, page_selectors)}
178
+ end
179
+
180
+ it "is given as a keyword, as the XPath of logged_out_selector" do
181
+ _(WebAccount.new(logged_out_xpath: '//div[@id="given"]').logged_out_selector).must_equal({xpath: '//div[@id="given"]'})
182
+ end
183
+
184
+ it "is given in PAGE_SELECTORS, as the XPath of logged_out_selector" do
185
+ _(account_class(logged_out_xpath: '//div[@id="sub-class"]').new.logged_out_selector).must_equal({xpath: '//div[@id="sub-class"]'})
186
+ end
187
+
188
+ it "is set, as the XPath of logged_out_selector" do
189
+ account = WebAccount.new
190
+ account.logged_out_xpath = '//div[@id="set"]'
191
+ _(account.logged_out_selector).must_equal({xpath: '//div[@id="set"]'})
192
+ _(account.logged_out_xpath).must_equal('//div[@id="set"]')
193
+ end
194
+
195
+ it "gives way to logged_out_selector, given the same way" do
196
+ _(WebAccount.new(logged_out_selector: {css: '#given'}, logged_out_xpath: '//div').logged_out_selector).must_equal({css: '#given'})
197
+ _(account_class(logged_out_selector: {css: '#sub-class'}, logged_out_xpath: '//div').new.logged_out_selector).must_equal({css: '#sub-class'})
198
+ end
199
+
200
+ it "as a keyword, wins over a sub-class's PAGE_SELECTORS[:logged_out_selector]" do
201
+ _(account_class(logged_out_selector: {css: '#sub-class'}).new(logged_out_xpath: '//div').logged_out_selector).must_equal({xpath: '//div'})
202
+ end
203
+ end
204
+
205
+ describe "the names before 0.8.0's" do
206
+ it "are gone" do
207
+ %i{username_field_selector password_field_selector default_username_field_selector default_password_field_selector}.each do |name|
208
+ _(WebAccount.method_defined?(name) || WebAccount.private_method_defined?(name)).must_equal(false)
209
+ end
210
+ end
211
+ end
212
+
213
+ describe "#page_selectors" do
214
+ it "is the class's PAGE_SELECTORS" do
215
+ account_class = Class.new(WebAccount){const_set(:PAGE_SELECTORS, {login_page_url: 'https://subclass.example/login'})}
216
+ _(account_class.new.send(:page_selectors)).must_equal({login_page_url: 'https://subclass.example/login'})
217
+ end
218
+
219
+ it "is overridden for every reader, and can keep a parent's values" do
220
+ parent = Class.new(WebAccount){const_set(:PAGE_SELECTORS, {login_page_url: 'https://parent.example/login', logged_out_selector: {id: 'parent'}})}
221
+ child = Class.new(parent){private def page_selectors = super.merge(login_page_url: 'https://child.example/login')}
222
+ _(child.new.login_page_url).must_equal('https://child.example/login')
223
+ _(child.new.logged_out_selector).must_equal({id: 'parent'})
224
+ end
225
+ end
226
+
227
+ describe "#login_page" do
228
+ def account_with_browser(account)
229
+ browser = Object.new
230
+ browser.define_singleton_method(:get){|url| (@got ||= []) << url}
231
+ browser.define_singleton_method(:got){@got}
232
+ account.instance_variable_set(:@driver, browser)
233
+ [account, browser]
234
+ end
235
+
236
+ it "gets login_page_url" do
237
+ account, browser = account_with_browser(WebAccount.new(login_page_url: 'https://given.example/login'))
238
+ account.send(:login_page)
239
+ _(browser.got).must_equal(['https://given.example/login'])
240
+ end
241
+
242
+ it "names login_page_url: where there is none" do
243
+ account, browser = account_with_browser(WebAccount.new)
244
+ error = _{account.send(:login_page)}.must_raise(ArgumentError)
245
+ _(error.message).must_equal('missing keyword: :login_page_url')
246
+ _(browser.got).must_be_nil
247
+ end
248
+ end
249
+
250
+ describe "#logged_out?" do
251
+ def account_with_page(account)
252
+ page = Object.new
253
+ page.define_singleton_method(:element_present?){|how, what| (@asked ||= []) << [how, what]; true}
254
+ page.define_singleton_method(:asked){@asked}
255
+ account.instance_variable_set(:@driver, page)
256
+ [account, page]
257
+ end
258
+
259
+ it "looks for logged_out_selector" do
260
+ account, page = account_with_page(WebAccount.new(logged_out_selector: {css: '#given'}))
261
+ _(account.logged_out?).must_equal(true)
262
+ _(page.asked).must_equal([[:css, '#given']])
263
+ end
264
+
265
+ it "names logged_out_selector: where there is none" do
266
+ account, page = account_with_page(WebAccount.new)
267
+ error = _{account.logged_out?}.must_raise(ArgumentError)
268
+ _(error.message).must_equal('missing keyword: :logged_out_selector')
269
+ _(page.asked).must_be_nil
270
+ end
271
+ end
272
+
134
273
  describe "#driver_options" do
135
274
  def driver_options(**args)
136
275
  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.9.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