boq_com_au 0.1.0 → 0.3.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: 4ee1609019ff26e0055fe57d7e98bb43efcaa7708da26b4e3df98f310ecffb3a
4
- data.tar.gz: 14545bd78bf644f95d33a0df3e0794a3a58ac2c88c070a2341a22e3acf2a91a9
3
+ metadata.gz: f547e1eca5d228aa0fbe37a82921b33370977e513f209b5e57bab82d8347b6bb
4
+ data.tar.gz: 0aa18118dc2ce6a0726fdbac97bff153f89cf7a55febf13240a5a2237447ecf5
5
5
  SHA512:
6
- metadata.gz: fdcf36437f2ce71868d7b4ce856b035b103f180fc517d3cd3e51b50bdba59db0bad95fcf7a36b2dde86562ef397fcdd352b182a62390b6b50950452b69a2c511
7
- data.tar.gz: 9a95829cdd54d422172206b69f0a869191a046ea7b6472fc23f89b655253baa0a64640fa1f373a6fbc5378b7c78e6d328afa3e8bb801ccfca085226b58ff3096
6
+ metadata.gz: 31711622ea4cf7f3ffbc005b00abe07c7859529badc32b338defa02a6fa65713ed9b10674f671b545d65ac0ed4933ac622d751e02dd3c919fa7b5fc67b678779
7
+ data.tar.gz: 537ee3dad673b7a379c15da75b271b3e5bff94a55fccbe94fb7c633168cd7e14a655892e1eac3c06284404122e6ae02098af35db237da2ca23e4a8e990cbb625
data/CHANGELOG CHANGED
@@ -1,5 +1,35 @@
1
1
  # boq_com_au/CHANGELOG
2
2
 
3
+ ## 20261111
4
+
5
+ 0.3.0: ~ BoqComAu::Account uses WebAccount 0.7.0's browser options and download waiting, so as it runs in Firefox as well as Chrome; /chrome_profile_directory:/profile_directory:/.
6
+
7
+ 1. ~ boq_com_au.gemspec: webaccount /'>= 0.6.1'/'~> 0.7'/
8
+ 2. ~ BoqComAu::Account#initialize: /chrome_profile_directory:/profile_directory:/; download_directory:, browser: and driver_options: go to WebAccount, as do headless:, window_size: and user_agent:.
9
+ 3. - BoqComAu::Account#default_driver_options, WebAccount's giving Chrome's options or Firefox's.
10
+ 4. - BoqComAu::Account#download_directory, #click, #stale?, #wait_for, #set_value, WebAccount's doing the same.
11
+ 5. - BoqComAu::Account#newest_csv_filename, #wait_for_download, WebAccount's #wait_for_download waiting longer, and past Firefox's downloads in progress as well as Chrome's.
12
+ 6. ~ test/BoqComAu/Account_test.rb: The tests of what's now WebAccount's are dropped; the options for Chrome and for Firefox, and profile_directory: in place of chrome_profile_directory:, are tested.
13
+ 7. ~ README.md: Firefox and profile_directory:.
14
+ 8. ~ BoqComAu::VERSION: /0.2.0/0.3.0/
15
+
16
+
17
+ ## 20261104
18
+
19
+ 0.2.0: + BoqComAu::Account#download_transactions(period:, from_date:, to_date:), choosing the dates.
20
+
21
+ 1. + BoqComAu::Account::TRANSACTIONS_SELECTORS: + advanced_search_link, text_fields, search_link.
22
+ 2. ~ BoqComAu::Account#download_transactions: + period: (:all, the last year, or :last_month), + from_date:, + to_date:; given any, the dates are set under Advanced Search and searched on before exporting.
23
+ 3. + BoqComAu::Account#dates, #last_month_start_date, #last_month_finish_date, #formatted_date
24
+ 4. + BoqComAu::Account#enter_dates: the displayed date fields set by JavaScript, so as a date picker doesn't open over them; and stops where the values didn't take, rather than exporting another range.
25
+ 5. + BoqComAu::Account#search: waits for the Export All link from before the search to be replaced, else what was there before would be exported.
26
+ 6. + BoqComAu::Account#set_value, #stale?, #date_fields
27
+ 7. ~ BoqComAu::Transactions.download_csv_file: + period:, from_date:, to_date:
28
+ 8. ~ test/BoqComAu/Account_test.rb, test/BoqComAu/Transactions_test.rb
29
+ 9. ~ README.md: choosing the dates.
30
+ 10. ~ BoqComAu::VERSION: /0.1.0/0.2.0/
31
+
32
+
3
33
  ## 20261103
4
34
 
5
35
  0.1.0: + BoqComAu::Account#download_transactions, exporting an account's transactions as a CSV file.
data/README.md CHANGED
@@ -31,17 +31,25 @@ $ gem install boq_com_au
31
31
  boq_account.login
32
32
  boq_account.download_transactions(account_number: '12345678')
33
33
  # => "/Users/me/Downloads/12345678_20261103.csv"
34
- boq_account.shutdown # Logs out if still logged in, then quits Chrome.
34
+ boq_account.shutdown # Logs out if still logged in, then quits the browser.
35
35
  ```
36
- The account is picked from the list by the digits of its number alone, so `'12345678'` and `'1234 5678'` are the same. Its transactions are those BOQ shows on choosing it, exported with Export All as Comma Delimited Format (CSV).
36
+ The account is picked from the list by the digits of its number alone, so `'12345678'` and `'1234 5678'` are the same. Its transactions are exported with Export All as Comma Delimited Format (CSV).
37
37
 
38
- `download_transactions` waits for the download to finish, and returns the newest CSV file to have appeared in the download directory since Download was clicked. BOQ names it after the account number and the day's date, and Chrome adds a number to that where a file of the name is there already.
38
+ Without dates, the transactions are those BOQ shows on choosing the account. Given a period or dates, they're set under Advanced Search and searched on first:
39
+ ```ruby
40
+ boq_account.download_transactions(account_number: '12345678', period: :last_month)
41
+ boq_account.download_transactions(account_number: '12345678', period: :all) # The last year.
42
+ boq_account.download_transactions(account_number: '12345678', from_date: '2026-01-01', to_date: '2026-06-30')
43
+ ```
44
+ Dates are Dates, or Strings `Date.parse` can read. Where only one of `from_date` and `to_date` is given, the other is a year ago or today.
45
+
46
+ `download_transactions` waits for the download to finish, and returns the newest CSV file to have appeared in the download directory since Download was clicked. BOQ names it after the account number and the day's date, and the browser adds a number to that where a file of the name is there already.
39
47
 
40
48
  Or in one step, logging in, downloading, and shutting down even where the download fails:
41
49
  ```ruby
42
- BoqComAu::Transactions.download_csv_file(account_number: '12345678')
50
+ BoqComAu::Transactions.download_csv_file(account_number: '12345678', period: :last_month)
43
51
  ```
44
- Any other keyword arguments are passed to `BoqComAu::Account.new`.
52
+ It takes `period:`, `from_date:` and `to_date:` as `download_transactions` does. Any other keyword arguments are passed to `BoqComAu::Account.new`.
45
53
 
46
54
  ### Credentials
47
55
 
@@ -54,15 +62,16 @@ Where either is missing, `login` stops, saying which.
54
62
 
55
63
  ### The browser
56
64
 
57
- Chrome is started with its automation flags masked. Given a `chrome_profile_directory`, it's started with that profile, kept between runs, so as the bank sees a returning browser rather than a fresh automated one; without one, each run has a new, temporary profile.
65
+ The browser is WebAccount's: Chrome, unless `browser: :firefox` is given, Chrome being started with its automation flags masked. Given a `profile_directory`, it's started with that profile, kept between runs, so as the bank sees a returning browser rather than a fresh automated one; without one, each run has a new, temporary profile.
58
66
  ```ruby
59
- BoqComAu::Account.new(chrome_profile_directory: '~/path/to/chrome-profile')
67
+ BoqComAu::Account.new(profile_directory: '~/path/to/profile')
68
+ BoqComAu::Account.new(browser: :firefox, profile_directory: '~/path/to/firefox-profile')
60
69
  ```
61
- Chrome downloads into `download_directory`, `~/Downloads` unless given, without asking where, and opens nothing it downloads:
70
+ It downloads into `download_directory`, `~/Downloads` unless given, without asking where, and opens nothing it downloads:
62
71
  ```ruby
63
72
  BoqComAu::Account.new(download_directory: '~/path/to/statements')
64
73
  ```
65
- Passing `browser:` or `driver_options:` to `BoqComAu::Account.new` replaces all of that, as for any WebAccount.
74
+ `headless:`, `window_size:` and `user_agent:` are passed to WebAccount too. Passing `driver_options:` replaces the browser's options altogether.
66
75
 
67
76
  Where BOQ asks for a one-time code after logging on, `login` waits up to 30 seconds for it to be entered in the browser.
68
77
 
@@ -70,8 +79,6 @@ Where BOQ asks for a one-time code after logging on, `login` waits up to 30 seco
70
79
 
71
80
  The selectors were taken from the live site. BOQ's ids are generated, so things are found by their labels and text, and a change to the wording of the site will break it.
72
81
 
73
- The date range is BOQ's own, as shown on choosing the account; it can't yet be chosen.
74
-
75
82
  The tests cover only what doesn't depend on BOQ's site.
76
83
 
77
84
  ## Contributing
data/boq_com_au.gemspec CHANGED
@@ -38,7 +38,7 @@ Gem::Specification.new do |spec|
38
38
 
39
39
  spec.dependencies = [
40
40
  ['selenium-webdriver', '>= 4.0'],
41
- ['webaccount', '>= 0.6.1']
41
+ ['webaccount', '~> 0.7']
42
42
  ]
43
43
 
44
44
  spec.development_dependencies = %w{
@@ -7,10 +7,12 @@
7
7
  # 3. BankFast's links are javascript:void(0) where they aren't the session's key and a callback number, and some, such as Transaction History, are in a menu which is closed. A link which isn't displayed is clicked by JavaScript.
8
8
  # 4. Dropdowns are Chosen's, which hides the select and draws its own: <select id>_chosen, opened by its a.chosen-single and chosen from its li.active-result. A select which is displayed is used as it is.
9
9
  # 5. Where a one-time code is asked for after logging on, login() waits default_wait_timeout seconds for it to be entered by hand.
10
- # 6. Chrome is started with its automation flags masked; and, given a chrome_profile_directory, with that profile, kept between runs, so as the bank sees a returning browser rather than a fresh automated one. It downloads into download_directory without asking, and opens nothing it downloads.
10
+ # 6. The browser, Chrome or Firefox, is WebAccount's: given a profile_directory, it's started with that profile, kept between runs, so as the bank sees a returning browser rather than a fresh automated one. It downloads into download_directory without asking, and opens nothing it downloads.
11
11
  # 7. A username or password not given stops login, saying which is missing.
12
- # 8. BOQ names the exported file after the account number and the date, as 12345678_20261103.csv, and Chrome numbers a second download of the same name; so the newest CSV file to appear in download_directory once Download is clicked is taken to be it. Chrome names a download in progress .crdownload, so a CSV file is a finished one.
12
+ # 8. BOQ names the exported file after the account number and the date, as 12345678_20261103.csv, and the browser numbers a second download of the same name; so the newest finished CSV file to appear in download_directory once Download is clicked is taken to be it.
13
+ # 9. Dates are BOQ's own, as shown on choosing the account, unless a period or a date is given; then they're set under Advanced Search and searched on before exporting.
13
14
 
15
+ require 'date'
14
16
  require 'webaccount'
15
17
 
16
18
  module BoqComAu
@@ -27,12 +29,14 @@ module BoqComAu
27
29
  TRANSACTIONS_SELECTORS = {
28
30
  transaction_history_link: {xpath: '//a[normalize-space()="Transaction History"]'},
29
31
  account_select_list: {xpath: '//select[option[contains(., "Select one of the Accounts")]]'},
32
+ advanced_search_link: {xpath: '//a[normalize-space()="Advanced Search"]'},
33
+ text_fields: {xpath: '//input[@type="text"]'},
34
+ search_link: {xpath: '//a[normalize-space()="Search"]'},
30
35
  export_all_link: {xpath: '//a[normalize-space()="Export All"]'},
31
36
  export_format_select_list: {xpath: '//select[option[contains(., "Comma Delimited Format (CSV)")]]'},
32
37
  download_button: {xpath: '//*[self::a or self::button][normalize-space()="Download"] | //input[@value="Download"]'}
33
38
  }
34
39
 
35
- attr_reader :download_directory
36
40
  def username
37
41
  @username || raise("No username: it's given as username:.")
38
42
  end
@@ -41,57 +45,26 @@ module BoqComAu
41
45
  @password || raise("No password: it's given as password:.")
42
46
  end
43
47
 
44
- # Returns the name of the CSV file downloaded.
45
- def download_transactions(account_number:)
48
+ # period is :all, the last year, or :last_month. Without it, from_date and to_date, either of which defaults to a year ago and today respectively where the other is given. Returns the name of the CSV file downloaded.
49
+ def download_transactions(account_number:, period: nil, from_date: nil, to_date: nil)
50
+ from_date, to_date = dates(period: period, from_date: from_date, to_date: to_date)
46
51
  get_transactions_page
47
52
  select_bank_account(account_number)
53
+ if from_date
54
+ enter_dates(from_date: from_date, to_date: to_date)
55
+ search
56
+ end
48
57
  export_csv
49
58
  end
50
59
 
51
60
  private
52
61
 
53
- def initialize(username: nil, password: nil, chrome_profile_directory: nil, download_directory: File.join('~', 'Downloads'), browser: nil, driver_options: nil)
54
- @chrome_profile_directory = chrome_profile_directory && File.expand_path(chrome_profile_directory)
55
- @download_directory = File.expand_path(download_directory)
56
- super(
57
- username: username, password: password,
58
- browser: browser, driver_options: driver_options,
59
- login_page_url: PAGE_SELECTORS[:login_page_url],
60
- login_page_username_field_selector: PAGE_SELECTORS[:login_page_username_field_selector],
61
- login_page_password_field_selector: PAGE_SELECTORS[:login_page_password_field_selector],
62
- login_page_submit_button_selector: PAGE_SELECTORS[:login_page_submit_button_selector],
63
- logout_button_selector: PAGE_SELECTORS[:logout_button_selector],
64
- logged_out_xpath: PAGE_SELECTORS[:logged_out_xpath],
65
- )
66
- end
67
-
68
- # driver construction
69
-
70
- def default_driver_options
71
- return unless browser == :chrome
72
- Selenium::WebDriver::Chrome::Options.new.tap do |o|
73
- o.add_argument("--user-data-dir=#{@chrome_profile_directory}") if @chrome_profile_directory
74
- o.add_argument('--disable-blink-features=AutomationControlled')
75
- o.exclude_switches << 'enable-automation'
76
- o.add_preference(:credentials_enable_service, false)
77
- o.add_preference(:download, default_directory: @download_directory, prompt_for_download: false, directory_upgrade: true, extensions_to_open: '')
78
- end
62
+ def initialize(username: nil, password: nil, **web_account_args)
63
+ super(username: username, password: password, **web_account_args, **PAGE_SELECTORS)
79
64
  end
80
65
 
81
66
  # page interaction
82
67
 
83
- # Found afresh on each attempt, so as an element replaced by a redrawn page is retried rather than ending in StaleElementReferenceError.
84
- def click(selector)
85
- driver.attempt do
86
- element = find(selector)
87
- element.displayed? ? element.click : driver.execute_script('arguments[0].click();', element)
88
- end
89
- end
90
-
91
- def wait_for(selector)
92
- driver_wait.until{driver.find_elements(*selector.first).any?(&:displayed?)}
93
- end
94
-
95
68
  def chosen_list(select_element)
96
69
  driver.find_elements(id: "#{select_element.attribute('id')}_chosen").first
97
70
  end
@@ -110,14 +83,29 @@ module BoqComAu
110
83
  option
111
84
  end
112
85
 
113
- # downloads
86
+ # dates
87
+
88
+ def last_month_start_date
89
+ Date.today.prev_month - (Date.today.prev_month.day - 1)
90
+ end
91
+
92
+ def last_month_finish_date
93
+ Date.today - Date.today.day
94
+ end
114
95
 
115
- def newest_csv_filename(since:)
116
- Dir.glob(File.join(@download_directory, '*.csv')).select{|filename| File.mtime(filename) >= since}.max_by{|filename| File.mtime(filename)}
96
+ # The from and to dates of the period, or of the dates given; nil and nil where neither is given, BOQ's own dates being used.
97
+ def dates(period:, from_date:, to_date:)
98
+ case period
99
+ when :all then [Date.today.prev_year, Date.today]
100
+ when :last_month then [last_month_start_date, last_month_finish_date]
101
+ when nil then (from_date || to_date) ? [from_date || Date.today.prev_year, to_date || Date.today] : [nil, nil]
102
+ else raise ArgumentError, "Unknown period #{period.inspect}: it's :all or :last_month."
103
+ end
117
104
  end
118
105
 
119
- def wait_for_download(since:)
120
- driver_wait.until{newest_csv_filename(since: since)}
106
+ def formatted_date(date)
107
+ date = Date.parse(date) if date.is_a?(String)
108
+ date.strftime('%d/%m/%Y')
121
109
  end
122
110
 
123
111
  # transactions
@@ -137,6 +125,31 @@ module BoqComAu
137
125
  wait_for(TRANSACTIONS_SELECTORS[:export_all_link])
138
126
  end
139
127
 
128
+ # The from and to fields are the two displayed text fields holding a date, as BOQ fills them in. Hidden ones are the form before Advanced Search has redrawn it.
129
+ def date_fields
130
+ driver.find_elements(*TRANSACTIONS_SELECTORS[:text_fields].first).select{|field| field.displayed? && field.attribute('value').to_s.match?(%r{\A\d\d/\d\d/\d{4}\z})}
131
+ rescue Selenium::WebDriver::Error::StaleElementReferenceError
132
+ []
133
+ end
134
+
135
+ def enter_dates(from_date:, to_date:)
136
+ click(TRANSACTIONS_SELECTORS[:advanced_search_link])
137
+ from_date_field, to_date_field = driver_wait.until{fields = date_fields; fields if fields.size >= 2}
138
+ set_value(from_date_field, formatted_date(from_date))
139
+ set_value(to_date_field, formatted_date(to_date))
140
+ unless [from_date_field, to_date_field].map{|field| field.attribute('value')} == [formatted_date(from_date), formatted_date(to_date)]
141
+ raise "The dates #{formatted_date(from_date)} to #{formatted_date(to_date)} didn't take."
142
+ end
143
+ end
144
+
145
+ # Export All is already there before the search, and is replaced when the results are, so the old link is waited out first; otherwise it'd export what was there before.
146
+ def search
147
+ export_all_link = driver.find_elements(*TRANSACTIONS_SELECTORS[:export_all_link].first).first
148
+ click(TRANSACTIONS_SELECTORS[:search_link])
149
+ driver_wait.until{stale?(export_all_link)} if export_all_link
150
+ wait_for(TRANSACTIONS_SELECTORS[:export_all_link])
151
+ end
152
+
140
153
  def export_csv
141
154
  click(TRANSACTIONS_SELECTORS[:export_all_link])
142
155
  driver_wait.until{driver.element_present?(*TRANSACTIONS_SELECTORS[:export_format_select_list].first)}
@@ -7,9 +7,9 @@ module BoqComAu
7
7
  module Transactions
8
8
  class << self
9
9
  # Returns the name of the CSV file downloaded.
10
- def download_csv_file(account_number:, **account_args)
10
+ def download_csv_file(account_number:, period: nil, from_date: nil, to_date: nil, **account_args)
11
11
  boq_account = BoqComAu::Account.new(**account_args)
12
- boq_account.download_transactions(account_number: account_number)
12
+ boq_account.download_transactions(account_number: account_number, period: period, from_date: from_date, to_date: to_date)
13
13
  ensure
14
14
  boq_account&.shutdown
15
15
  end
@@ -2,5 +2,5 @@
2
2
  # BoqComAu::VERSION
3
3
 
4
4
  module BoqComAu
5
- VERSION = '0.1.0'
5
+ VERSION = '0.3.0'
6
6
  end
@@ -5,9 +5,9 @@
5
5
 
6
6
  require_relative '../../lib/boq_com_au'
7
7
 
8
+ require 'date'
8
9
  require 'minitest/autorun'
9
10
  require 'minitest-spec-context'
10
- require 'tmpdir'
11
11
 
12
12
  describe BoqComAu::Account do
13
13
  def stand_in_element(text: '', displayed: true, id: nil)
@@ -38,12 +38,12 @@ describe BoqComAu::Account do
38
38
  link
39
39
  end
40
40
 
41
- # Finds the elements by id given, though only after the number of looks given in drawn_after, as for a list Chosen hasn't drawn yet.
42
- def stand_in_driver(elements_by_id: {}, drawn_after: 0)
41
+ # Finds the elements by id given, and otherwise those given in elements; though only after the number of looks given in drawn_after, as for a list Chosen hasn't drawn yet.
42
+ def stand_in_driver(elements_by_id: {}, elements: [], drawn_after: 0)
43
43
  driver = Object.new
44
44
  looks = 0
45
45
  driver.define_singleton_method(:attempt){|&block| block.call(self)}
46
- driver.define_singleton_method(:find_elements){|*, **by| (looks += 1) > drawn_after ? [elements_by_id[by[:id]]].compact : []}
46
+ driver.define_singleton_method(:find_elements){|*, **by| (looks += 1) > drawn_after ? (by[:id] ? [elements_by_id[by[:id]]].compact : elements) : []}
47
47
  driver.define_singleton_method(:element_present?){|*| true}
48
48
  driver.define_singleton_method(:execute_script){|script, *args| (@scripts ||= []) << [script, *args]}
49
49
  driver.define_singleton_method(:scripts){@scripts || []}
@@ -67,11 +67,6 @@ describe BoqComAu::Account do
67
67
  [account, opener, options]
68
68
  end
69
69
 
70
- def touch(filename, mtime)
71
- File.write(filename, '')
72
- File.utime(mtime, mtime, filename)
73
- end
74
-
75
70
  describe "#username and #password" do
76
71
  it "are those given" do
77
72
  account = BoqComAu::Account.new(username: 'customer', password: 'secret')
@@ -97,76 +92,92 @@ describe BoqComAu::Account do
97
92
  end
98
93
  end
99
94
 
100
- describe "#default_driver_options" do
101
- it "masks Chrome's automation flags, leaving the profile to Chrome where none is given" do
102
- options = BoqComAu::Account.new.send(:default_driver_options)
103
- _(options.args).must_include('--disable-blink-features=AutomationControlled')
104
- _(options.exclude_switches).must_include('enable-automation')
105
- _(options.args.grep(/--user-data-dir/)).must_be_empty
95
+ describe "#download_transactions" do
96
+ def account_recording_steps
97
+ account = account_with_driver(stand_in_driver)
98
+ steps = []
99
+ %i{get_transactions_page select_bank_account search export_csv}.each{|step| account.define_singleton_method(step){|*| steps << step}}
100
+ account.define_singleton_method(:enter_dates){|from_date:, to_date:| steps << [:enter_dates, from_date, to_date]}
101
+ [account, steps]
102
+ end
103
+
104
+ it "uses BOQ's own dates where neither a period nor a date is given" do
105
+ account, steps = account_recording_steps
106
+ account.download_transactions(account_number: '12345678')
107
+ _(steps).must_equal(%i{get_transactions_page select_bank_account export_csv})
108
+ end
109
+
110
+ it "sets the dates and searches on them before exporting where they're given" do
111
+ account, steps = account_recording_steps
112
+ account.download_transactions(account_number: '12345678', from_date: '2026-10-01', to_date: '2026-10-31')
113
+ _(steps).must_equal([:get_transactions_page, :select_bank_account, [:enter_dates, '2026-10-01', '2026-10-31'], :search, :export_csv])
106
114
  end
115
+ end
107
116
 
108
- it "starts Chrome with the profile given, expanded" do
109
- options = BoqComAu::Account.new(chrome_profile_directory: '~/profiles/boq').send(:default_driver_options)
117
+ describe "the browser" do
118
+ it "is Chrome, started with the profile given, expanded, downloading into the download directory given" do
119
+ options = BoqComAu::Account.new(profile_directory: '~/profiles/boq', download_directory: '~/statements').send(:default_driver_options)
120
+ _(options).must_be_kind_of(Selenium::WebDriver::Chrome::Options)
110
121
  _(options.args).must_include("--user-data-dir=#{File.expand_path('~/profiles/boq')}")
122
+ _(options.prefs[:download][:default_directory]).must_equal(File.expand_path('~/statements'))
111
123
  end
112
124
 
113
- it "downloads into the download directory without asking, opening nothing" do
114
- options = BoqComAu::Account.new(download_directory: '~/statements').send(:default_driver_options)
115
- download = options.prefs[:download]
116
- _(download[:default_directory]).must_equal(File.expand_path('~/statements'))
117
- _(download[:prompt_for_download]).must_equal(false)
118
- _(download[:extensions_to_open]).must_equal('')
125
+ it "is Firefox where asked for, started with the profile given, downloading into the download directory given" do
126
+ options = BoqComAu::Account.new(browser: :firefox, profile_directory: '~/profiles/boq', download_directory: '~/statements').send(:default_driver_options)
127
+ _(options).must_be_kind_of(Selenium::WebDriver::Firefox::Options)
128
+ _(options.args).must_equal(['-profile', File.expand_path('~/profiles/boq')])
129
+ _(options.prefs['browser.download.dir']).must_equal(File.expand_path('~/statements'))
119
130
  end
120
131
 
121
132
  it "downloads into ~/Downloads where no directory is given" do
122
133
  _(BoqComAu::Account.new.download_directory).must_equal(File.expand_path('~/Downloads'))
123
134
  end
124
135
 
125
- it "is nil for browsers other than Chrome" do
126
- _(BoqComAu::Account.new(browser: :firefox).send(:default_driver_options)).must_be_nil
136
+ it "takes the profile as profile_directory:, no longer as chrome_profile_directory:" do
137
+ _{BoqComAu::Account.new(chrome_profile_directory: '~/profiles/boq')}.must_raise(ArgumentError)
138
+ end
139
+
140
+ it "logs in to BOQ, whatever else is passed to WebAccount" do
141
+ account = BoqComAu::Account.new(headless: true, window_size: [1280, 800])
142
+ _(account.send(:username_field_selector)).must_equal(BoqComAu::Account::PAGE_SELECTORS[:login_page_username_field_selector])
143
+ _(account.send(:logout_button_selector)).must_equal(BoqComAu::Account::PAGE_SELECTORS[:logout_button_selector])
127
144
  end
128
145
  end
129
146
 
130
- describe "#click" do
131
- it "finds the element afresh where the one found first has been redrawn" do
132
- old_link, new_link = stand_in_link, stand_in_link
133
- old_link.stale!
134
- driver = stand_in_driver
135
- attempts = 0
136
- driver.define_singleton_method(:attempt) do |&block|
137
- block.call(self)
138
- rescue Selenium::WebDriver::Error::StaleElementReferenceError
139
- attempts += 1
140
- retry if attempts < 3
141
- raise
142
- end
143
- account = account_with_driver(driver)
144
- links = [old_link, new_link]
145
- account.define_singleton_method(:find){|*| links.shift}
146
- account.send(:click, {xpath: '//a'})
147
- _(new_link.clicked?).must_equal(true)
147
+ describe "#dates" do
148
+ def dates(**args)
149
+ BoqComAu::Account.new.send(:dates, **{period: nil, from_date: nil, to_date: nil}.merge(args))
150
+ end
151
+
152
+ it "is the last year for :all" do
153
+ _(dates(period: :all)).must_equal([Date.today.prev_year, Date.today])
154
+ end
155
+
156
+ it "is last month for :last_month" do
157
+ today = Date.today
158
+ _(dates(period: :last_month)).must_equal([Date.new(today.prev_month.year, today.prev_month.month, 1), Date.new(today.year, today.month, 1) - 1])
159
+ end
160
+
161
+ it "is the dates given, defaulting the other to a year ago or today" do
162
+ _(dates(from_date: '2026-01-01', to_date: '2026-02-01')).must_equal(['2026-01-01', '2026-02-01'])
163
+ _(dates(from_date: '2026-01-01')).must_equal(['2026-01-01', Date.today])
164
+ _(dates(to_date: '2026-02-01')).must_equal([Date.today.prev_year, '2026-02-01'])
165
+ end
166
+
167
+ it "is none where neither a period nor a date is given" do
168
+ _(dates).must_equal([nil, nil])
169
+ end
170
+
171
+ it "refuses a period it doesn't know" do
172
+ _{dates(period: :fortnight)}.must_raise(ArgumentError)
148
173
  end
149
174
  end
150
175
 
151
- describe "#newest_csv_filename" do
152
- it "is the newest CSV file in the download directory since the time given" do
153
- Dir.mktmpdir do |directory|
154
- since = Time.now - 60
155
- touch(File.join(directory, 'old.csv'), since - 60)
156
- touch(File.join(directory, 'new.csv'), since + 10)
157
- touch(File.join(directory, 'newer.csv'), since + 20)
158
- touch(File.join(directory, 'newest.csv.crdownload'), since + 30)
159
- account = BoqComAu::Account.new(download_directory: directory)
160
- _(account.send(:newest_csv_filename, since: since)).must_equal(File.join(directory, 'newer.csv'))
161
- end
162
- end
163
-
164
- it "is nil where none has appeared since" do
165
- Dir.mktmpdir do |directory|
166
- since = Time.now
167
- touch(File.join(directory, 'old.csv'), since - 60)
168
- _(BoqComAu::Account.new(download_directory: directory).send(:newest_csv_filename, since: since)).must_be_nil
169
- end
176
+ describe "#formatted_date" do
177
+ it "is day/month/year, from a Date or a String" do
178
+ account = BoqComAu::Account.new
179
+ _(account.send(:formatted_date, Date.new(2026, 3, 4))).must_equal('04/03/2026')
180
+ _(account.send(:formatted_date, '2026-03-04')).must_equal('04/03/2026')
170
181
  end
171
182
  end
172
183
 
@@ -209,4 +220,57 @@ describe BoqComAu::Account do
209
220
  _(options.map(&:clicked?)).must_equal([false])
210
221
  end
211
222
  end
223
+
224
+ describe "#enter_dates" do
225
+ def stand_in_date_field(value, displayed: true)
226
+ field = Object.new
227
+ field.instance_variable_set(:@value, value)
228
+ field.define_singleton_method(:displayed?){displayed}
229
+ field.define_singleton_method(:attribute){|name| @value if name == 'value'}
230
+ field.define_singleton_method(:value=){|value| @value = value}
231
+ field
232
+ end
233
+
234
+ # A driver whose set_value() sets the field, or, where the page throws the value away, doesn't.
235
+ def account_with_date_fields(fields, taking: true)
236
+ driver = stand_in_driver(elements: fields)
237
+ driver.define_singleton_method(:execute_script){|script, element = nil, value = nil| element.value = value if taking && element.respond_to?(:value=)}
238
+ account = account_with_driver(driver)
239
+ account.define_singleton_method(:click){|*| true}
240
+ account
241
+ end
242
+
243
+ it "sets the displayed date fields, leaving the hidden ones of the form before it was redrawn" do
244
+ hidden_from, hidden_to = stand_in_date_field('28/10/2026', displayed: false), stand_in_date_field('03/11/2026', displayed: false)
245
+ from, to = stand_in_date_field('28/10/2026'), stand_in_date_field('03/11/2026')
246
+ account = account_with_date_fields([hidden_from, hidden_to, from, to])
247
+ account.send(:enter_dates, from_date: Date.new(2026, 10, 1), to_date: Date.new(2026, 10, 31))
248
+ _([from, to].map{|field| field.attribute('value')}).must_equal(['01/10/2026', '31/10/2026'])
249
+ _([hidden_from, hidden_to].map{|field| field.attribute('value')}).must_equal(['28/10/2026', '03/11/2026'])
250
+ end
251
+
252
+ it "says so where the dates didn't take, rather than searching another range" do
253
+ account = account_with_date_fields([stand_in_date_field('28/10/2026'), stand_in_date_field('03/11/2026')], taking: false)
254
+ error = _{account.send(:enter_dates, from_date: Date.new(2026, 10, 1), to_date: Date.new(2026, 10, 31))}.must_raise(RuntimeError)
255
+ _(error.message).must_include('01/10/2026 to 31/10/2026')
256
+ end
257
+ end
258
+
259
+ describe "#search" do
260
+ it "waits for the results to replace the Export All link that was there before the search" do
261
+ old_export_all_link = stand_in_link
262
+ account = account_with_driver(stand_in_driver(elements: [old_export_all_link]))
263
+ account.define_singleton_method(:click){|*| old_export_all_link.stale!}
264
+ account.define_singleton_method(:wait_for){|*| true}
265
+ account.send(:search)
266
+ _(account.send(:stale?, old_export_all_link)).must_equal(true)
267
+ end
268
+
269
+ it "doesn't wait to see a link replaced where there was none" do
270
+ account = account_with_driver(stand_in_driver(elements: []))
271
+ account.define_singleton_method(:click){|*| true}
272
+ account.define_singleton_method(:wait_for){|*| true}
273
+ _(account.send(:search)).must_equal(true)
274
+ end
275
+ end
212
276
  end
@@ -11,7 +11,7 @@ require 'minitest-spec-context'
11
11
  describe BoqComAu::Transactions do
12
12
  def with_stand_in_account(download: ->{'/downloads/transactions.csv'})
13
13
  account = Object.new
14
- account.define_singleton_method(:download_transactions){|account_number:| (@asked ||= []) << account_number; download.call}
14
+ account.define_singleton_method(:download_transactions){|account_number:, **dates| (@asked ||= []) << [account_number, dates]; download.call}
15
15
  account.define_singleton_method(:asked){@asked}
16
16
  account.define_singleton_method(:shutdown){@shut_down = true}
17
17
  account.define_singleton_method(:shut_down?){!!@shut_down}
@@ -24,10 +24,10 @@ describe BoqComAu::Transactions do
24
24
  end
25
25
 
26
26
  describe ".download_csv_file" do
27
- it "is the file downloaded, for the account given, the other arguments going to the account, which is shut down" do
27
+ it "is the file downloaded, for the account and period given, the other arguments going to the account, which is shut down" do
28
28
  with_stand_in_account do |account, arguments|
29
- _(BoqComAu::Transactions.download_csv_file(account_number: '12345678', download_directory: '/downloads')).must_equal('/downloads/transactions.csv')
30
- _(account.asked).must_equal(['12345678'])
29
+ _(BoqComAu::Transactions.download_csv_file(account_number: '12345678', period: :last_month, download_directory: '/downloads')).must_equal('/downloads/transactions.csv')
30
+ _(account.asked).must_equal([['12345678', {period: :last_month, from_date: nil, to_date: nil}]])
31
31
  _(arguments).must_equal([{download_directory: '/downloads'}])
32
32
  _(account.shut_down?).must_equal(true)
33
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boq_com_au
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -27,16 +27,16 @@ dependencies:
27
27
  name: webaccount
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
- - - ">="
30
+ - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: 0.6.1
32
+ version: '0.7'
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.6.1
39
+ version: '0.7'
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: minitest
42
42
  requirement: !ruby/object:Gem::Requirement