Samples and Howtos of Advanced Topics
In this article, you will find some good practices and examples of complex variable structures in test cases. To learn how to create a test case, see the Creating Selenium Test Cases article.
Conditional and Loops in Executions
The following short script shows complex structures, such as conditional execution (if), and a numbered loop (for), along with assigning and using variables in Javascript and Selenese.
COMMAND | PARAM 1/TARGET | PARAM 2/VALUE |
store | 1 | loopVariable1 |
while | storedVars.loopVariable <= 10 | |
echo | Value in between while and endWhile is ${loopVariable} | |
storeEval | storedVars.loopVariable++ | |
endWhile | ||
comment | ### CONDITIONAL EXAMPLE | |
gotoIf | storedVars.loopVariable >= 1 | SKIP |
comment | This command will not be executed, because loopVariable >= 1 | |
label | SKIP |
Rich Text Fields
We offer limited support for rich text editors, allowing the "typing" of HTML in that kind of elements, but the recording cannot work.
In Salesforce Classic/Lightning, in order to set the value of a rich text field, you can right-click on the label of a field (not the field itself) and add it to Copado Recorder. Then change the command to type, the locator to copado=field:XXX (instead of copado=label:XXX ) and put the text in the value column. The end result should be something like this:
COMMAND | PARAM 1/TARGET | PARAM 2/VALUE |
type | copado=field:Project Name | Copado QA |
Testing Slow Actions
Some actions are slow and might require hard -to-predict delays. A simple way to deal with this is using the "pause <milliseconds>" command. This command inserts a fixed pause, which might slow down every execution.
A better alternative is to wait for something on the page to change, e.g. if there is a Status field in a Salesforce page that will automatically change to 'Closed' and might take up to 5 minutes ( 300000ms ), you could use:
COMMAND | PARAM 1/TARGET | PARAM 2/VALUE |
setTimeout | 300000 | |
waitForValue | copado:detail:Status | Closed |
This will wait for the value to change to 'Closed' for up to 5 min. Bear in mind that if you want to reset the timeout to its default value (30 seconds), instead of keeping it for 5 minutes, you need to add another setTimeout after the waitForValue.
Handling Numeric Values in Web Pages
Selenese and HTML in general deal with text only, so, in order to perform any arithmetic operation or comparison, it's necessary to convert the text to a javascript number.
In the following example, we are creating an opportunity. This process shows in bold, how to extract a fragment of text representing a number, how to convert it to a javascript number, and then how to perform arithmetic operations.
COMMAND | PARAM 1/TARGET | PARAM 2/VALUE |
open | /006/e?retURL=%2F006%2Fo | |
type | copado=field:Opportunity Name | {!RUN_ID} test opp |
type | copado=field:Close Date | {!TODAY} |
select | copado=field:Stage | Prospecting |
type | copado=field:Amount | 102.65 |
click | copado=button: Save | |
comment | Get the text in the locator, and store it in the variable ATEXT | |
storeValue | copado=detail:Amount | ATEXT |
echo | the locator contains the text: ${ATEXT} | |
comment | Convert it to a number, removing any non-number character ($, %, comma, whitespaces, etc) | |
storeEval | parseFloat(storedVars.ATEXT.replace(/[^0-9]+/g,"")) | ANUMBER |
comment | Now, you can treat storedVars.ANUMBER as a regular number in javascript | |
verifyEval | storedVars.ANUMBER+1000 > 1100 | TRUE |