1

I am new to this run keyword if method.

I wanted to enter different number based on specific page.

e.g. if page1 element is detected then input number 1, if page2 then input number 2.

*** Settings ***
Library    Selenium2Library
Library    Collections
Resource   ../Resources/nine-res-work.robot

*** Variables ***
${LOGIN-BUTTON-NUMBER-1}   ${ANDROID-WIDGET-TEXT-VIEW}\[@resource-id="com.test.abc.work.cac:id/btn_number" and @text="1"]
${LOGIN-BUTTON-NUMBER-2}   ${ANDROID-WIDGET-TEXT-VIEW}\[@resource-id="com.test.abc.work.cac:id/btn_number" and @text="2"]
${LOGIN-BUTTON-NUMBER-3}   ${ANDROID-WIDGET-TEXT-VIEW}\[@resource-id="com.test.abc.work.cac:id/btn_number" and @text="3"]

${LOGIN-PAGE-HEARDER-page1}         ${ANDROID-WIDGET-TEXT-VIEW}\[@resource-id="com.test.abc.work.cac:id/headerText" and @text="Enter your PIN."]
${LOGIN-PAGE-HEARDER-page2}               ${ANDROID-WIDGET-TEXT-VIEW}\[@resource-id="com.test.abc.work.cac:id/headerText" and @text="Enter your passcode."]


*** Keywords ***
Smart Card Login
    Run Keyword If  ${LOGIN-PAGE-HEARDER-page1} == 'PASS'   Tap     ${LOGIN-BUTTON-NUMBER-1}
    Run Keyword If  ${LOGIN-PAGE-HEARDER-page2} == 'PASS'   Tap     ${LOGIN-BUTTON-NUMBER-2}

*** Test Cases ***
Test 1
    Launch Application
    Smart Card Login

error

Test 1  | FAIL |
Evaluating expression '//android.widget.TextView[@resource-id="com.test.abc.work.cac:id/headerText" and @text="Enter your PIN."] == 'PASS'' failed: SyntaxError: invalid syntax (<string>, line 1)

I have tried another way, this time no error but the tap action isn't execute.

*** Variables ***
${LOGIN-BUTTON-NUMBER-1}   ${ANDROID-WIDGET-TEXT-VIEW}\[@resource-id="com.test.abc.work.cac:id/btn_number" and @text="1"]
${LOGIN-BUTTON-NUMBER-2}   ${ANDROID-WIDGET-TEXT-VIEW}\[@resource-id="com.test.abc.work.cac:id/btn_number" and @text="2"]
${LOGIN-BUTTON-NUMBER-3}   ${ANDROID-WIDGET-TEXT-VIEW}\[@resource-id="com.test.abc.work.cac:id/btn_number" and @text="3"]

${LOGIN-PAGE-HEARDER-page1}         ${ANDROID-WIDGET-TEXT-VIEW}\[@text="Enter your PIN."]
${LOGIN-PAGE-HEARDER-page2}               ${ANDROID-WIDGET-TEXT-VIEW}\[@text="Enter your passcode."]


*** Keywords ***
Input App Passcode
    Tap     ${LOGIN-BUTTON-NUMBER-2}


*** Test Cases ***
Launch App
    Open Nine Folders Application
    Sleep   5s

Input Password
    ${Page1} =    Page Should Contain Element    ${LOGIN-PAGE-HEARDER-page1}
    Run Keyword If      '${Page1}' == 'PASS'      Input App Passcode

1 Answer 1

1

You got syntax error because Run Keyword If expects a valid Python condition as first argument. This is not the case in your code. In your case this is what happens, assuming ${ANDROID-WIDGET-TEXT-VIEW} is just view in this example:

Run Keyword If  ${LOGIN-PAGE-HEARDER-page1} == 'PASS'   Tap     ${LOGIN-BUTTON-NUMBER-1}

which is

Run Keyword If  view\[@resource-id="com.test.abc.work.cac:id/headerText" and @text="Enter your PIN."] == 'PASS'   Tap     ${LOGIN-BUTTON-NUMBER-1}

this is equivalent with the following Python code:

if view\[@resource-id="com.test.abc.work.cac:id/headerText" and @text="Enter your PIN."] == 'PASS':
    call_tap_function(LOGIN_BUTTON_NUMBER_1)

There are a bunch of invalid characters there because the string is not enclosed in '. So correctly it should be:

Run Keyword If  '${LOGIN-PAGE-HEARDER-page1}' == 'PASS'   Tap     ${LOGIN-BUTTON-NUMBER-1}

which will translate to:

if 'view\[@resource-id="com.test.abc.work.cac:id/headerText" and @text="Enter your PIN."]' == 'PASS':
    call_tap_function(LOGIN_BUTTON_NUMBER_1)

Note that this will never be equal with PASS.


As for your second approach, Page Should Contain Element does not have a return value, it will fail or the execution continues as usual. To achieve what you want you should use the Run Keyword And Return Status that will return if the keyword called has passed or failed.

Input Password
    ${Page1} =    Run Keyword And Return Status    Page Should Contain Element    ${LOGIN-PAGE-HEARDER-page1}
    Run Keyword If      ${Page1}      Input App Passcode

Here ${Page1} variable will be true if Page Should Contain Element passed, aka if login page header page1 was on the page.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.