Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Selenium WebDriver’s JavascriptExecutor will wrap all JavaScript and evaluate it as an anonymous expression. Therefore, the “return” keyword must be used when executing JavaScript within the “Target” field in an ASM scenario. For example, the command browserbot.getCurrentWindow().document.title in Selenium becomes return document.title; in an ASM Scenario.

If your Selenium IDE looks like the following:

...

Command

...

Target

...

Value

...

assertEval

for the following commands:

GotoIf

GetEval

StoreEval

StoreExpression

The following commands do not require the use of “return”:

RunScript

RunScriptAndWait

For example, if you are trying to determine whether the result of the expression ${stored_var} > 0

...

true

Add a “return” keyword to the target so it looks like is true, use the following code:

Command

Target

Value

assertEval

return ${stored_var} > 0

true

The following commands result in javascript evaluation or expect a javascript snippet as an argument. Therefore, they also require the use of “return”:

GotoIf

RunScript

RunScriptAndWait

GetEval

StoreEval

StoreExpressionscreenshot (supplemental text provided) shows an example in which JavaScript is being used to execute another check upon completion of the current scenario. Basically, the check kicks off another check when it’s finished.

Within the scenario:

...

Run execution log:

...

Javascript snippet:

Code Block
var xhr = new XMLHttpRequest();

xhr.open('POST', 'https://api-asm.apica.io/v3/checks/3393731/job?auth_ticket=${auth_ticket}', true);

xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');

xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) { 
        if (xhr.status === 200) { 
            var response = JSON.parse(xhr.responseText);
            console.log('Success:', response); 
        } else {
            console.error('Error:', xhr.statusText);
        }
    }
};

xhr.send();

return "check ID 3393731 has been executed!"

The “runScript” or “runScriptAndWait” commands could also be used with the above Javascript if the “return” statement is omitted.

Note

Javascript comments are NOT allowed in the body of a Javascript snippet!

Type & sendKeys

Even though the type command works in most cases, Apica strongly recommends using sendKeys instead of type. The type command edits the HTML format of the code, while the sendKeys command attempts to simulate actual typing and is therefore able to trigger javascript code in fields.

...