Browse Source

initial commit

Daniel Sheffield 2 years ago
commit
d78fe747ff
3 changed files with 127 additions and 0 deletions
  1. 124 0
      Download Statements.robot
  2. 2 0
      requirements.txt
  3. 1 0
      run.sh

+ 124 - 0
Download Statements.robot

@@ -0,0 +1,124 @@
+*** Settings ***
+Library    Browser
+Library    Dialogs
+Library    String
+Library    Collections
+Library    DateTime
+Library    OperatingSystem
+Library    Process
+Resource    Variables.robot
+
+Suite Setup   Run Keywords     Set Executable Status
+...    AND    Suite Can Continue
+...    AND    New Browser    downloadsPath=${OUTPUTDIR}     headless=no
+...    AND    New Context    acceptDownloads=yes
+...    AND    New Page    ${HOME_PAGE}
+...    AND    Go To Login Page
+...    AND    Enter Username And Password
+...    AND    Login
+Suite Teardown  Run Keywords    Pass Execution If Skipped
+...    AND    Capture Account Balances
+...    AND    Close Browser
+Task Template    Download Statement
+
+
+*** Variables ***
+${HOME_PAGE}    https://sbsbank.co.nz
+${LANDING_PAGE}    ${EMPTY}
+${FROM_DATE}    ${EMPTY}
+${TO_DATE}    ${EMPTY}
+${SKIP}    no
+
+*** Keywords ***
+New Page
+    [Arguments]    ${url}    @{args}    &{kwargs}
+    ${old_timeout} =    Set Browser Timeout    30 seconds
+    Browser.New Page    ${url}    @{args}    &{kwargs}
+    [Teardown]    Set Browser Timeout    ${old_timeout}
+
+Page Should Contain
+    [Arguments]    ${expected}
+    ${page}=    Get Page Source
+    Should Contain    ${page}    ${expected}
+    
+Output Directory Is Dirty
+    File Should Exist    ${OUTPUTDIR}/Export*.ofx
+
+Set Executable Status
+    ${status}=    Run Keyword And Return Status    Output Directory Is Dirty
+    ${SKIP}=    Set Variable If    ${status}   yes    no
+    Set Suite Variable    ${SKIP}
+
+Suite Can Continue
+    ${SKIP}=    Evaluate    robot.utils.is_truthy('${SKIP}')    modules=robot.utils
+    Run Keyword If    ${SKIP}    Fail    msg=Output directory is dirty
+    
+Pass Execution If Skipped
+    ${SKIP}=    Evaluate    robot.utils.is_truthy('${SKIP}')    modules=robot.utils
+    Pass Execution If    ${SKIP}   Suite is skipped.
+
+Go To Login Page
+    Click    xpath=//div[@class="loginDD dropdown"]
+    Click    xpath=//ul[@class="dropdown__menu"]/li/a[contains(text(), 'PERSONAL')]
+    
+Enter Username And Password
+    [Documentation]    Enters user/password into text fields of login page.
+    ${url}=    Get Url
+    ${log_level}=    Set Log Level    NONE
+    ${password}=    Get Value From User    Enter password for ${url}    hidden=yes
+    Set Log Level    ${log_level}
+    Fill Text    xpath=//input[@id="login"]    ${USERNAME}
+    Fill Secret    xpath=//input[@id="password"]    $password
+    [Teardown]    Set Log Level    ${log_level}
+
+Login
+    [Documentation]    Login and Return Home URL. Assumes user/password already entered.
+    ${old_timeout} =    Set Browser Timeout    30 seconds
+    Click    xpath=//button[@id="loginButton"]
+    Execute Manual Step    Complete 2-factor authentication
+    Wait Until Keyword Succeeds    5x    1 second
+    ...    Page Should Contain    ${ACCOUNT_HOLDER_NAME}
+    [Teardown]    Set Browser Timeout    ${old_timeout}
+
+Download Statement
+    [Arguments]    ${account}
+    ${old_timeout}=    Set Browser Timeout    30 seconds
+    Click    xpath=//div/h2[contains(.,'Account Manager')]/..//div[@data-focusable="true"]/div[contains(.,'${account.upper()}')]
+    ${account_code}=    Get Text    xpath=//iframe >>> div.chosen-container >> span.account-bsb-number    #css=div.chosen-container >> span.account-bsb-number
+    Click    xpath=//iframe >>> xpath=//label[contains(.,'date range')]/.. >> .ui-selectmenu-icon
+    Click    xpath=//iframe >>> .ui-selectmenu-menu >> .ui-menu-item >> xpath=//*[contains(.,'The last 3 months')]
+    Click    xpath=//iframe >>> .btn--export-all
+    Click    xpath=//iframe >>> xpath=//label[contains(.,'Export Format')]/.. >> .ui-selectmenu-icon
+    Click    xpath=//iframe >>> .ui-selectmenu-menu >> .ui-menu-item >> xpath=//*[contains(.,'Comma Separated Values')]
+    ${now}=    Get Current Date    result_format=%Y%m%d
+    Promise To Wait For Download    ${OUTPUTDIR}/SB${account_code}_${now}.csv
+    Click    xpath=//iframe >>> .btn--download
+    Wait For All Promises
+    ${status}=    Run Keyword And Return Status
+    ...    Wait Until Created    ${OUTPUTDIR}/SB*.csv    timeout=5 seconds
+    Run Keyword If    not ${status}    Take Screenshot
+    Run Keyword If    not ${status}    Fail    msg=No file downloaded for account: ${account}
+    ${files}=    List Files In Directory    ${OUTPUTDIR}
+    FOR    ${f}    IN    @{files}
+        Continue For Loop If    not '${f}'.startswith('SB')
+        Move File    ${OUTPUTDIR}${/}${f}    ${OUTPUTDIR}${/}${account}-${f}
+        Return From Keyword
+    END
+    Fail    msg=Failed to save downloaded account statement to ${OUTPUTDIR}: ${account}
+    [Teardown]    Run Keywords    Set Browser Timeout    ${old_timeout}
+    ...           AND    Click    xpath=//iframe >>> .btn--return
+
+Capture Account Balances
+    [Documentation]    Assumes current page is Balances.
+    FOR    ${account}    IN    @{ACCOUNTS}
+        ${account}=    Convert To Uppercase    ${account}
+        Wait Until Keyword Succeeds    2x    5 seconds
+        ...    Page Should Contain    ${account}
+    END
+    Take Screenshot
+
+*** Tasks ***
+Download Statement
+    FOR    ${account}    IN    @{ACCOUNTS}
+        ${account}
+    END

+ 2 - 0
requirements.txt

@@ -0,0 +1,2 @@
+robotframework
+robotframework-browser

+ 1 - 0
run.sh

@@ -0,0 +1 @@
+/usr/bin/python3 -m robot.run -T --dotted --outputdir /tmp Download\ Statements.robot