質問

In my Robot test automation script, I'm trying to detect if an element exists or not. If it does, I'm trying to set a variable to the value of element's attributes. If it doesn't exist, I'd like to set that variable to empty. I can't for the life of me get this to work though:

My Feature Step Keyword
    :FOR  ${rowIndex}  IN  @{rowIndexes}
    \   ${xpathColumnIndex} =  Evaluate  ${columnIndex} + 1
    \   ${xpathRowIndex} =  Evaluate  ${rowIndex} + 1
    \   ${rowImageExist} =  Element Exists  table[contains(@class,'datatable')]/tbody/tr[${xpathRowIndex}]/td[${xpathColumnIndex}]/img
    \   Run Keyword If  ${rowImageExist}  ${rowAutoBidStatus} =  Get Element Attribute  jquery=tbody tr:eq(${rowIndex}) td:eq(${columnIndex}) img@data-status
    \   Run Keyword Unless  ${rowImageExist}  ${rowAutoBidStatus} =  ${EMPTY}
    \   Log  ${rowAutoBidStatus}

Element Exists
    [Documentation]  Determines if the desired element exists
    [Arguments]  ${xpath}
    ${imageCount} =  Get Matching Xpath Count  ${xpath}
    ${imageExists} =  Evaluate  ${imageCount} > 0
    [Return]  ${imageExists}

It keeps saying Non-existing variable ${rowAutoBidStatus} on the "Run Keyword If" keyword. I've tried other combinations, like Set Variable If, but I think robot expects me to give it a value instead of another keyword to evaluate. The problem is, I can't run the keyword "Get Element Attribute" until I make sure that the element exists. Otherwise it will throw an error.

There has got to be a simple way to do this. Its not that complicated and I can't imagine I'm the only person that has run into this problem.

役に立ちましたか?

解決

Overview

You are using Run keyword if incorrectly. You must give it a keyword as the first argument after the condition, not a variable reference. It will return whatever the keyword returns, which you can save in a variable.

In other words, instead of this (using pipe-separated values for clarity):

| | # incorrect
| | Run keyword if | <some condition> | ${variable}= | some keyword

... you need to do this:

| | # correct
| | ${variable}= | Run keyword if | <some condition> | some keyword

If the keyword is not run, the variable will be set to None (ie: the same value that is in the automatic variable ${None})

Example using your code

In your case, you would replace the Run Keyword If and Run Keyword Unless statments with a single Run Keyword If:

${rowAutoBidStatus}=  Run Keyword If  ${rowImageExist}  Get Element Attribute  jquery=tbody tr:eq(${rowIndex}) td:eq(${columnIndex}) img@data-status

If you really want the value to be ${Empty} rather than ${None}, starting with Robot Framework 2.7.4 you can add an ELSE clause. It would look something like this (I'm going to spread it across multiple lines for clarity:

${rowAutoBidStatus}=  Run Keyword If  ${rowImageExist}  
...  Get Element Attribute  jquery=tbody tr:eq(${rowIndex}) td:eq(${columnIndex}) img@data-status 
...  ELSE  
...  Set variable | ${Empty}

他のヒント

I finally got it!

My Feature Step Keyword
    :FOR  ${rowIndex}  IN  @{rowIndexes}
    \   ${xpathColumnIndex} =  Evaluate  ${columnIndex} + 1
    \   ${xpathRowIndex} =  Evaluate  ${rowIndex} + 1
    \   ${xpathNextRowIndex} =  Evaluate  ${xpathRowIndex} + 1
    \   Get Element Info  //table[contains(@class,'datatable')]/tbody/tr[${xpathRowIndex}]/td[${xpathColumnIndex}]/img  data-status  data-attributes

Get Element Info
    [Arguments]  ${xpath}  ${attribute1}  ${attribute2}
    ${imageExist} =  Element Exists  ${xpath}
    ${attr1Locator} =  CATENATE  SEPARATOR=  xpath=  ${xpath}  @  ${attribute1}
    ${attr2Locator} =  CATENATE  SEPARATOR=  xpath=  ${xpath}  @  ${attribute2}
    Run Keyword If  ${imageExist}  Get Element Attribute1  ${attr1Locator}
    Run Keyword Unless  ${imageExist}  Set Element Attribute1 To Empty
    Run Keyword If  ${imageExist}  Get Element Attribute2  ${attr2Locator}
    Run Keyword Unless  ${imageExist}  Set Element Attribute2 To Empty

Get Element Attribute1
    [Arguments]  ${locator}
    ${attributes} =  Get Element Attribute  ${locator}
    Set Test Variable  ${elementAttribute1}  ${attributes}

Set Element Attribute1 To Empty
    Set Test Variable  ${elementAttribute1}  ${EMPTY}

Get Element Attribute2
    [Arguments]  ${locator}
    ${attributes} =  Get Element Attribute  ${locator}
    Set Test Variable  ${elementAttribute2} ${attributes}

Set Element Attribute2 To Empty
    Set Test Variable  ${elementAttribute2}  ${EMPTY}

Element Exists
    [Documentation]  Determines if the desired element exists
    [Arguments]  ${xpath}
    ${imageCount} =  Get Matching Xpath Count  ${xpath}
    ${imageExists} =  Evaluate  ${imageCount} > 0
    [Return]  ${imageExists}

It looks like there are some occasions where I cannot chain keywords together, otherwise they don't get evaluated by robot correctly. As long as I passed a keyword to "Run Keyword If" and "Run Keyword Unless", then it worked fine. So I just made separate keywords for getting the element attributes and for setting the variable to empty.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top