Skip to content

Instantly share code, notes, and snippets.

@lczub
Created December 23, 2019 17:22
Show Gist options
  • Save lczub/869f065d216f01618c285b68e43ec213 to your computer and use it in GitHub Desktop.
Save lczub/869f065d216f01618c285b68e43ec213 to your computer and use it in GitHub Desktop.
Robot Framework sample, how to handle JavaScript Promises
*** Settings ***
Documentation Sample Robot Suite showing how to handle JavaScript Promises, using the [https://github.com/robotframework/SeleniumLibrary/|SeleniumLibrary] keyword [https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Execute%20Async%20Javascript|Execute Asyn Javascript].
...
... Interacts with sample page [https://googlesamples.github.io/web-fundamentals/fundamentals/primers/async-example.html|Promises tests], provided by
... [https://developers.google.com/web/fundamentals/primers/promises|JavaScript Promises: an Introduction]
...
... Main suite keyword to handles promises is *PS Execute Async Promise*
Suite Setup PS Open Browser
Suite Teardown Close Browser
Library SeleniumLibrary
*** Variables ***
${PS_BROWSER} FF
${PS_PROMISES_TEST_URL_BASE} https://googlesamples.github.io/web-fundamentals/fundamentals/primers
${PS_PROMISES_TEST_URL_SITE} async-example.html
${PS_PROMISES_TEST_URL} ${PS_PROMISES_TEST_URL_BASE}/${PS_PROMISES_TEST_URL_SITE}
${PS_DEFAULT_DELAY} 0.1sec
${PS_DEFAULT_TIMEOUT} 5sec
${PS_MAX_WAIT} 2sec
${PS_MAX_WAIT_WITH_FAKE} 15sec
*** Test Cases ***
Get chapter content - synchron call - getJsonSync
Go To ${PS_PROMISES_TEST_URL}
&{content}= Execute Javascript return getJsonSync('chapter-1.json')
Should Be Equal As Integers ${content.chapter} 1
Should Match ${content.html} <p>Chapter 1 text*
Get chapter content - asynchron call - getJson
Go To ${PS_PROMISES_TEST_URL}
&{content}= PS Execute Async Promise getJson('chapter-1.json')
Should Be Equal As Integers ${content.chapter} 1
Should Match ${content.html} <p>Chapter 1 text*
Get chapter content - asynchron call - get
Go To ${PS_PROMISES_TEST_URL}
&{content}= PS Execute Async Promise get('chapter-1.json') return_function=JSON.parse(response)
Should Be Equal As Integers ${content.chapter} 1
Should Match ${content.html} <p>Chapter 1 text*
Get unknown chapter error - asynchron call - getJson
Go To ${PS_PROMISES_TEST_URL}
Run Keyword And Expect Error *UNEXPECTED_STATE - Error - Not Found - getJson('chapter-x.json')* PS Execute Async Promise getJson('chapter-x.json')
Get unknown chapter error - asynchron call - get
Go To ${PS_PROMISES_TEST_URL}
Run Keyword And Expect Error *UNEXPECTED_STATE - Error - Not Found - get('chapter-x.json').then( function(response) { return JSON.parse(response); } )* PS Execute Async Promise get('chapter-x.json') return_function=JSON.parse(response)
Open Promises Sample Page without delay
Go To ${PS_PROMISES_TEST_URL}
Checkbox Should Not Be Selected class:network-fake label input
Wait Until Page Contains Cras gravida bibendum turpis at varius ${PS_MAX_WAIT}
Open Promises Sample Page with delay
Go To ${PS_PROMISES_TEST_URL}
Checkbox Should Not Be Selected class:network-fake label input
Select Checkbox class:network-fake label input
Checkbox Should Be Selected class:network-fake label input
Wait Until Page Contains Cras gravida bibendum turpis at varius ${PS_MAX_WAIT_WITH_FAKE}
*** Keywords ***
PS Set Delay and Timeout to Default
Set Selenium Speed ${PS_DEFAULT_DELAY}
Set Selenium Timeout ${PS_DEFAULT_TIMEOUT}
PS Open Browser
Open Browser browser=${PS_BROWSER}
Maximize Browser Window
PS Set Delay and Timeout to Default
PS Execute Async Promise
[Arguments] ${promise} ${timeout}=${PS_MAX_WAIT} ${return_function}=${None} ${failure_marker}=UNEXPECTED_STATE
[Documentation] Wrapper calling a asynchronous JavaScript ${promise} and wait till max ${timeout}, if it's promise returns \ a success or failure.
...
... Failure case:
... - \ the error.message is catched and reported
...
... Success case:
... - When ${return_function} is None, the promise response will be returned directly (default)
... - Otherwise, an additional *then* chain step is added to modify / prepare promise response .
...
... Sample: If ${return_function} is _JSON.stringify(response)_ , a chain step _then( function(response) { return JSON.stringify(response); } )_ is added
[Tags] internal
Set Selenium Timeout ${timeout}
${promise_chain}= Set Variable If '${return_function}'!='${None}' ${promise}.then( function(response) { return ${return_function}; } ) ${promise}
${error_return}= Set Variable '${failure_marker} - ' + error.name + ' - ' + error.message
${result}= Execute Async Javascript var callback = arguments[arguments.length-1]; ${promise_chain}.then(function(response) { callback( response ); }).catch(function(error) { callback( ${error_return} ); });
Should Not Contain ${result} ${failure_marker} js call aborts with ${result} - ${promise_chain}
[Teardown] PS Set Delay and Timeout to Default
[Return] ${result}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment