Browse Source

convert Selenium based tasks to Browser tasks

Daniel Sheffield 3 years ago
parent
commit
da9f5dc0ad
4 changed files with 45 additions and 100 deletions
  1. 43 86
      Download Statements.robot
  2. 0 10
      EdgeUtils.py
  3. 1 1
      red.xml
  4. 1 3
      requirements.txt

+ 43 - 86
Download Statements.robot

@@ -1,16 +1,17 @@
 *** Settings ***
+Library    Browser
 Library    Dialogs
-Library    SeleniumLibrary
 Library    String
 Library    Collections
 Library    DateTime
 Library    OperatingSystem
 Library    Process
-Library    EdgeUtils
 
 Suite Setup   Run Keywords     Set Executable Status
 ...    AND    Suite Can Continue
-...    AND    Open ${BROWSER}
+...    AND    New Browser    downloadsPath=${OUTPUTDIR}     headless=yes
+...    AND    New Context    acceptDownloads=yes
+...    AND    New Page    ${HOME_PAGE}
 ...    AND    Go To Login Page
 ...    AND    Set Date Range
 ...    AND    Enter Username And Password
@@ -24,8 +25,6 @@ Task Template    Download Statement
 
 
 *** Variables ***
-# Edge and Firefox are supported
-${BROWSER}    Edge
 ${HOME_PAGE}    https://www.asb.co.nz
 ${USERNAME}    login username
 @{ACCOUNTS}    Account 1    Account 2    Account 3    Account N
@@ -36,6 +35,11 @@ ${TO_DATE}    ${EMPTY}
 ${SKIP}    no
 
 *** Keywords ***
+Page Should Contain
+    [Arguments]    ${expected}
+    ${page}=    Get Page Source
+    Should Contain    ${page}    ${expected}
+    
 Output Directory Is Dirty
     File Should Exist    ${OUTPUTDIR}/Export*.ofx
 
@@ -50,35 +54,7 @@ Suite Can Continue
     
 Pass Execution If Skipped
     ${SKIP}=    Evaluate    robot.utils.is_truthy('${SKIP}')    modules=robot.utils
-    Pass Execution If    ${SKIP}   Suite is skipped. 
-
-Open Firefox
-    ${profile}=    Catenate    SEPARATOR=;
-    ...    set_preference("browser.download.folderList",2)
-    ...    set_preference("browser.download.manager.showWhenStarting",False)
-    ...    set_preference("browser.download.dir",'${OUTPUTDIR}')
-    ...    set_preference("browser.helperApps.neverAsk.saveToDisk",'text/plain,text/csv,application/csv,application/download,application/octet-stream,text/ofx')
-    ...    set_preference('permissions.default.stylesheet', 2)
-    ...    set_preference('permissions.default.image', 2)
-    ...    set_preference('dom.ipc.plugins.enabled.libflashplayer.so','false')
-    Log    ${profile}
-    ${exec_path}=    Catenate    SEPARATOR=${/}
-    ...    %{HOME}    bin    gecko-driver    geckodriver
-    Open Browser    ${HOME_PAGE}    headlessfirefox    ff_profile_dir=${profile}
-    ...    executable_path=${exec_path}
-
-Open Edge
-    ${binary_path}=    Run Process    which    microsoft-edge
-    ${binary_path}=    Set variable    ${binary_path.stdout}
-    ${exec_path}=    Catenate    SEPARATOR=${/}
-    ...    %{HOME}    bin    edge-driver    edgedriver_linux64
-    ${options}=    Get Edge Options    ${OUTPUTDIR}
-    ${options._ignore_local_proxy}=    Set Variable    ${TRUE}
-    Set Environment Variable    PATH    %{PATH}:${exec_path}
-    ${driver}=    Create Webdriver    Edge    executable_path=${exec_path}
-    ...    options=${options}
-    Go To    ${HOME_PAGE}
-    
+    Pass Execution If    ${SKIP}   Suite is skipped.
 
 Set Date Range
     Return From Keyword If    '${FROM_DATE}' != '${EMPTY}'
@@ -93,7 +69,7 @@ Set Date Range
     Set Suite Variable    ${TO_DATE}
 
 Go To Login Page
-    Click Link    Log In
+    Click    xpath=//a[@class="btn login-button" and contains(text(), 'Log In')]
     
 Go To Search And Export Page
     [Documentation]    Assumes freshly logged in.
@@ -103,50 +79,51 @@ Go To Search And Export Page
 
 Enter Username And Password
     [Documentation]    Enters user/password into text fields of login page.
-    ${url}=    Get Location
+    ${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}
-    Input Text    dUsername    ${USERNAME}
-    Input Password    password    ${password}
+    Fill Text    xpath=//input[@id="dUsername"]    ${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.
-    Click Button    loginBtn
-    Wait Until Page Contains    ${ACCOUNT_HOLDER_NAME}    timeout=2 seconds
-    ${url}=    Get Location
+    Click    xpath=//button[@id="loginBtn"]
+    Wait Until Keyword Succeeds    5x    1 second
+    ...    Page Should Contain    ${ACCOUNT_HOLDER_NAME}
+    ${url}=    Get Url
     ${parts}=    Split String    ${url}    /
     ${parts}=    Get Slice From List    ${parts}     0    -2
     ${LANDING_PAGE}=    Catenate    SEPARATOR=/    @{parts}
     Set Suite Variable    ${LANDING_PAGE}
 
-Select From List By Partial Label
-    [Arguments]    ${locator}    ${value}
-    ${items}=    Get List Items    ${locator}
-    FOR    ${item}    IN    @{items}
-        Run Keyword If    ${value} in ${item}    Select From List By Label    ${locator}    ${item}
-        Exit For Loop IF    ${value} in ${item}
-    END
-    Fail
-
 Download Statement
     [Arguments]    ${account}
-    Click Element    id:Request_AccountKey_input
-    Mouse Down    xpath=//li[contains(.,'${account}')]
-    Click Element  xpath=//li[contains(.,'${account}')]
+    Click    xpath=//*[@id="Request_AccountKey_input"]
+    Hover    xpath=//li[contains(.,'${account}')]
+    Mouse Button    down    None    None    left
+    Mouse Button    up    None    None    left
     ${FROM_DATE}=    Convert Date    ${FROM_DATE}    result_format=datetime
     ${TO_DATE}=    Convert Date    ${TO_DATE}    result_format=datetime
-    Input Text    Request_FromDate_inputYear    ${FROM_DATE.year}
-    Input Text    Request_FromDate_inputMonth    ${FROM_DATE.month}
-    Input Text    Request_FromDate_inputDate    ${FROM_DATE.day}
-    Input Text    Request_ToDate_inputYear    ${TO_DATE.year}
-    Input Text    Request_ToDate_inputMonth    ${TO_DATE.month}
-    Input Text    Request_ToDate_inputDate    ${TO_DATE.day}
-    Click Button    Export
-    #Click Element    //table[@id='statementSelector']//input[@value='Export']
-    ${status}=    Run Keyword And Return Status    Wait Until Created    ${OUTPUTDIR}/Export*.ofx    timeout=5 seconds
-    Run Keyword Unless     ${status}    Capture Page Screenshot
+    ${from_date_year}=    Convert To String    ${FROM_DATE.year}
+    ${from_date_month}=    Convert To String    ${FROM_DATE.month}
+    ${from_date_day}=    Convert To String    ${FROM_DATE.day}
+    ${to_date_year}=    Convert To String    ${TO_DATE.year}
+    ${to_date_month}=    Convert To String    ${TO_DATE.month}
+    ${to_date_day}=    Convert To String    ${TO_DATE.day}
+    Fill Text    xpath=//*[@id="Request_FromDate_inputYear"]    ${FROM_DATE_year}
+    Fill Text    xpath=//*[@id="Request_FromDate_inputMonth"]    ${FROM_DATE_month}
+    Fill Text    xpath=//*[@id="Request_FromDate_inputDate"]    ${FROM_DATE_day}
+    Fill Text    xpath=//*[@id="Request_ToDate_inputYear"]    ${TO_DATE_year}
+    Fill Text    xpath=//*[@id="Request_ToDate_inputMonth"]    ${TO_DATE_month}
+    Fill Text    xpath=//*[@id="Request_ToDate_inputDate"]    ${TO_DATE_day}
+    Promise To Wait For Download    ${OUTPUTDIR}/Export-${account}.ofx
+    Click    xpath=//input[@value="Export"]
+    Wait For All Promises
+    ${status}=    Run Keyword And Return Status
+    ...    Wait Until Created    ${OUTPUTDIR}/Export*.ofx    timeout=5 seconds
+    Run Keyword Unless     ${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}
@@ -156,33 +133,13 @@ Download Statement
     END
     Fail    msg=Failed to save downloaded account statement to ${OUTPUTDIR}: ${account}
 
-Scroll To Top Of Page
-    Execute Javascript    window.scrollTo(0,0);
-
-Scroll One Page Down
-    [Arguments]    ${page_height}
-    Execute Javascript    window.scrollBy(0,${page_height});
-
-Firefox Scroll Element Into View
-    [Arguments]    ${locator}    ${start_page}=0    ${max_pages}=3
-    [Documentation]    Firefox behaves differently to Chromium. This is compatable with both.
-    Log    start_page=${start_page} max_pages=${max_pages}
-    Run Keyword If    ${max_pages} <= 0    Fail    Could not scroll ${locator} into view after ${start_page} pages
-    ${start_page}=    Convert To Integer    ${start_page}
-    ${max_pages}=    Convert To Integer    ${max_pages}
-    Run Keyword If    ${start_page} == 0    Scroll To Top Of Page
-    ${status}=    Run Keyword And Return Status    Scroll Element Into View    ${locator}
-    Return From Keyword If    ${status}
-    ${w}   ${h}=    Get Window Size   yes 
-    Scroll One Page Down    ${h}
-    Firefox Scroll Element Into View    ${locator}    start_page=${start_page+1}    max_pages=${max_pages-1}
-
 Capture Account Balances
     [Documentation]    Assumes current page is Balances.
     FOR    ${account}    IN    @{ACCOUNTS}
-        Wait Until Page Contains    ${account}    timeout=1 seconds
+        Wait Until Keyword Succeeds    2x    1 second
+        ...    Page Should Contain    ${account}
     END
-    Capture Page Screenshot
+    Take Screenshot
 
 *** Tasks ***
 Download Statement

+ 0 - 10
EdgeUtils.py

@@ -1,10 +0,0 @@
-from msedge.selenium_tools import EdgeOptions, Edge
-from selenium import webdriver
-		
-class EdgeUtils():
-	def get_edge_options(self, download_dir):
-		options = EdgeOptions()
-		options.use_chromium = True
-		prefs = {"download.default_directory": download_dir}
-		options.add_experimental_option("prefs", prefs)
-		return options

+ 1 - 1
red.xml

@@ -4,7 +4,7 @@
     <robotExecEnvironment path="/usr/bin" interpreter="Python3"/>
     <relativeTo>WORKSPACE</relativeTo>
     <referencedLibrary type="PYTHON" name="SeleniumLibrary" path="/home/das/.local/lib/python3.7/site-packages/SeleniumLibrary/__init__.py" dynamic="false"/>
-    <referencedLibrary type="PYTHON" name="EdgeUtils" path="asb-accounts/EdgeUtils.py" dynamic="false"/>
+    <referencedLibrary type="PYTHON" name="Browser" path="/home/das/.local/lib/python3.9/site-packages/Browser/__init__.py" dynamic="false"/>
     <pythonpath/>
     <classpath/>
     <excludedForValidation/>

+ 1 - 3
requirements.txt

@@ -1,5 +1,3 @@
 robotframework
-robotframework-seleniumlibrary
-msedge-selenium-tools
-selenium==4.00-alpha07
+robotframework-browser