Get Attribute function to return attribute or None

So the keyword documentation says the following:

" When a attribute is selected that is not present and no assertion operator is set, the keyword fails. If an assertion operator is set and the attribute is not present, the returned value is None . This can be used to assert check the presents or the absents of an attribute."

I have now tried to use the assertions in order to return the attribute and if one is not present to return None. However I have failed. Is it even possible?

The reason why I need this functionality is that in order to test some functionalities on the product Iā€™m testing I need to fill in some mandatory fields. Some of these field need specific input and some general. The inputs are in different forms, text, numerical, checkboxā€™s, radio buttons, calendar entries etc. None of these elements have one attribute that is present in all, so I need to get multiple attributes that I use to auto-fill these informationā€™s. On Selenium it loos like this:

ā€¦
${class}= Get Element Attribute ${i} class
${id}= Get Element Attribute ${i} id
${aria_label}= Get Element Attribute ${i} aria-label
${text}= Get Text ${i}

Run Keyword If ā€œAction nameā€ in ā€œ${placeholder}ā€ Input Text ${i} ${action} ${number}
ā€¦ ELSE IF ā€œtextā€ in ā€œ${type}ā€ Input Text ${i} Some text inputed
Run Keyword If ā€œnumberā€ in ā€œ${type}ā€ Input Text ${i} 123
Run Keyword If ā€œlistboxā€ in ā€œ${role}ā€ Click Element ${i}
ā€¦

So as you see, I use the attribute type to determine what the element is and how it should be handled and that is why I ask for a bunch of attributes, so I donā€™t need to assert whether or not any specific attribute is present, I just need the attribute or none.

Would Run Keyword And Ignore Error from the BuitIn Library help? you could use this with Get Element Attribute and first check if that passed if it did you can then check the attribute and perform your action.

Iā€™m not sure which keyword documentation you were quoting there, as itā€™s not the current version of Get Element Attribute. Iā€™m guessing youā€™re using SeleniumLibrary? is it an old version?

Hope this helps,

Dave.

I have actually tried ā€œRun Keyword And Ignore Errorā€ but the funny thing about Browser is that it ignores that keyword and still fails.

I was quoting the Browser keyword documentation, Iā€™m trying to migrate our Selenium tests to Browser and the code I used was for Selenium and I gave it as an example of what I did with Selenium. Now Iā€™m trying to find out how to do it in Browser.

Now I could use ā€œGet Attribute Namesā€ and then run ā€œGet Attributeā€ if that attribute exists but something like this is not very sexy:

${list_of_attributes} Get Attribute Names ${element}
${type}= Set Variable None
${type} Run Keyword If ā€œtypeā€ in ${list_of_attributes} Get Attribute ${element} type

Hi Ari,

I would not have guessed you were using Browser from your first message, just a tip, make these things clear in your initial post and youā€™ll get better or quicker answers.

You could create your own keyword for ā€œGet Element Attributeā€ for your browser tests, it could look like something like this:

*** keywords ***
Get Element Attribute
    [Arguments]    ${element}    ${attr}
    ${list_of_attributes}    Get Attribute Names    ${element}
    ${value}=    Set Variable    None
    ${value}=    Run Keyword If    ${attr} in ${list_of_attributes}    Get Attribute    ${element} 
   ${attr}
    [Return]    ${value}

Would that work for you?

Dave.

1 Like

Iā€™m sorry, I just thought that since this is the sub section dedicated for Browser it would not be necessary to specifically state that I use Browser. Iā€™l be more clear in the future.

Anyways, thatā€™s pretty much what I went with, I was just thinking there might be built-in function and I just missed it.

Hi Ari,

So you did, sorry I missed that as I picked it up from the latest list.

If itā€™s not in the doc I treat it as doesnā€™t exist, Browser is not a drop in replacement for selenium, some things they take quite a different approach, if something was useful to me in Selenium library and there is no equivalent I just create a keyword for it, but I guess you could also raise a feature request with the Browser team, my usage of Browser has been pretty light.

Good luck with your migration,

Dave.

Hi Dave

Well the problem with this one was that the documentation was a bit vague.
It says that with no assertion it fails, but with some assertion it returns, so I figured that there might be some way for it to work in a way that it returns the attribute if it exists or none if it does not exist. Since it clearly capable of doing both of them in some situations.

But I suppose I might have to do some keywords of my own, I donā€™t think this is such a pressing issue to request a feature.

Thanks

Ari

1 Like