Warm tip: This article is reproduced from stackoverflow.com, please click
javascript robotframework

How to make Execute JavaScript of RobotFramework's keyword return a value

发布于 2020-04-20 10:01:08

How to make Execute JavaScript of RobotFramework return a value

I need the text of a class element be returned using javascript code to a variable within RF. I have tried the following code but I get ${search_result} = None. It's not returning the text within the element selected.

:FOR    ${index}                    IN RANGE                            0   23
\  ${search_result} =             Execute JavaScript                  floatMaster.querySelectorAll('#gContact_searcResultshWindow .gContactDetailClientListDiv .gContactListName')[${index}].innerText

I have also tried below but I just get JavascriptException: Message: javascript error: Unexpected number

${search_result} =              Execute JavaScript                   function myFunction(${index}) {return floatMaster.querySelectorAll('#gContact_searcResultshWindow .gContactDetailClientListDiv .gContactListName')[${index}].innerText}()

The reason of the for loop is that the javascript locator has multiple results and I want to make sure each of them carry a specific text. I have a "should contain" command at the end of the loop.

javascript locator result from browser console

When I try the javascript locator command with the index in browser console, I get a value just fine. enter image description here

I am also aware of the Execute Async JavaScript command but it seems complicated and I don't understand how it works. Somebody help please.

Questioner
Gie
Viewed
107
asprtrmp 2020-02-05 15:44

Try:

${search_result}=    Execute JavaScript    return floatMaster.querySelectorAll('#gContact_searcResultshWindow .gContactDetailClientListDiv .gContactListName')[${index}].innerText