Skip to content

execute_script

roei sabag edited this page Jul 12, 2020 · 1 revision

Description

Executes JavaScript in the context of the currently selected frame or window.

Scope

Web, Mobile Web, Mobile Native or any other Web Driver implementation which implements Execute Script.

Mobile Native support is limited by Appium capabilities and implemnetaions.

Properties

Property Description
argument Plugin conditions and additional information.
onElement The locator value by which the element will be found.
locator The locator type by which the element will be found.

Command Line Arguments (CLI)

args

Object array to pass into this script.

Value Description
array An array of JSON Formatted objects.

src

The JavaScript code to execute.

Value Description
string A valid JavaScript script.

W3C Web Driver Protocol

https://www.w3.org/TR/webdriver/#execute-script

Examples

Example no. 1

Can be tested on

Executes script to change an element value, from document level.

Action Rule (JSON)

{
    "action": "ExecuteScript",
    "argument": "{document.getElementById('input_enabled').setAttribute('value', 'foo bar')}"
}

Rhino Literal

execute script {document.getElementById('input_enabled').setAttribute('value', 'foo bar')}

CSharp

var actionRule = new ActionRule
{
    Action = PluginsList.ExecuteScript,
    Argument = "{document.getElementById('input_enabled').setAttribute('value', 'foo bar')}"
};

Python

action_rule = {
    "action": "ExecuteScript",
    "argument": "{document.getElementById('input_enabled').setAttribute('value', 'foo bar')}"
}

Java Script

var actionRule = {
    action: "ExecuteScript",
    argument: "{document.getElementById('input_enabled').setAttribute('value', 'foo bar')}"
};

Java

ActionRule actionRule = new ActionRule()
        .setAction("ExecuteScript")
        .setArgument("{document.getElementById('input_enabled').setAttribute('value', 'foo bar')}");

Example no. 2

Can be tested on

Executes script to click on an element from Action Rule level.

Action Rule (JSON)

{
    "action": "ExecuteScript",
    "argument": "{arguments[0].click();}",
    "onElement": "click_button",
    "locator": "Id"
}

Rhino Literal

execute script {arguments[0].click();} on {click_button} using {id}

CSharp

var actionRule = new ActionRule
{
    Action = PluginsList.ExecuteScript,
    Argument = "{arguments[0].click();}",
    OnElement = "click_button",
    Locator = LocatorsList.Id
};

Python

action_rule = {
    "action": "ExecuteScript",
    "argument": "{arguments[0].click();}",
    "onElement": "click_button",
    "locator": "Id"
}

Java Script

var actionRule = {
    action: "ExecuteScript",
    argument: "{arguments[0].click();}",
    onElement: "click_button",
    locator: "Id"
};

Java

ActionRule actionRule = new ActionRule()
        .setAction("ExecuteScript")
        .setArgument("{arguments[0].click();}")
        .setOnElement("click_button")
        .setLocator("Id");

Example no. 3

Can be tested on

Executes script to scroll the page document, from document level with additional arguments.

Action Rule (JSON)

{
    "action": "ExecuteScript",
    "argument": "{{$ --src:window.scrollTo(arguments[0], arguments[1]); --args:[0, 100]}}"    
}

Rhino Literal

execute script {{$ --src:window.scrollTo(arguments[0], arguments[1]); --args:[0, 100]}}

CSharp

var actionRule = new ActionRule
{
    Action = PluginsList.ExecuteScript,
    Argument = "{{$ --src:window.scrollTo(arguments[0], arguments[1]); --args:[0, 100]}}",  
};

Python

action_rule = {
    "action": "ExecuteScript",
    "argument": "{{$ --src:window.scrollTo(arguments[0], arguments[1]); --args:[0, 100]}}"   
}

Java Script

var actionRule = {
    action: "ExecuteScript",
    argument: "{{$ --src:window.scrollTo(arguments[0], arguments[1]); --args:[0, 100]}}"
};

Java

ActionRule actionRule = new ActionRule()
        .setAction("ExecuteScript")
        .setArgument("{{$ --src:window.scrollTo(arguments[0], arguments[1]); --args:[0, 100]}}");

Example no. 4

Can be tested on

Check the element under extraction. This action applies on current element when using ExtractionRules. This action assumes the element already found and will inject it into the script. For an instance, instead of document.findElementById('id').checked=true; you will provide only the part of the script after the '.' - .checked=true; because the element which is now under extraction was already found and will be injected into your code.

Action Rule (JSON)

// action rule
{
    "action": "ExtractFromDom"   
}

// extraction rule
{
    "onRootElement": "//input[@id='input_selected']",
    "onElements": [
        {
            "key": "inner_text",
            "actions": [
                {
                    "action": "ExecuteScript",
                    "argument": ".checked=false;"
                }
            ]
        }
    ]
}

Rhino Literal

extract from page on {//input[@id='input_selected']}
    < column {inner_text}
        > execute script {.checked=false;}

CSharp

// action rule
var actionRule = new ActionRule
{
    Action = PluginsList.ExtractFromDom
};

// extraction rule
var extraction = new ExtractionRule
{
    OnRootElement = "//input[@id='input_selected']",
    OnElements = new[]
    {
        new ContentEntry
        {
            Key = "inner_text",
            Actions = new[]
            {
                new ActionRule
                {
                    Action = PluginsList.ExecuteScript,
                    Argument = ".checked=false;"
                }
            }
        }
    }
};

Python

# action rule
action_rule = {
    "action": "ExtractFromDom"  
}

# extraction rule
extraction_rule = {
    "onRootElement": "//input[@id='input_selected']",
    "onElements": [
        {
            "key": "inner_text",
            "actions": [
                {
                    "action": "ExecuteScript",
                    "argument": ".checked=false;"                    
                }
            ]
        }
    ]    
}

Java Script

// action rule
actionRule = {
    action: "ExtractFromDom"  
}

// extraction rule
extractionRule = {
    onRootElement: "//input[@id='input_selected']",
    onElements: [
        {
            key: "inner_text",
            actions: [
                {
                    action: "ExecuteScript",
                    argument: ".checked=false;"
                }
            ]
        }
    ]    
}

Java

// action rule
ActionRule actionRule = new ActionRule().setAction("ExtractFromDom");

// extraction rule
ActionRule actionRule = new ActionRule()
        .setAction("ExecuteScript")
        .setArgument(".checked=false;");

ContentEntry contentEntry = new ContentEntry().setKey("inner_text").setActions(actionRule);

ExtractionRule extractionRule = new ExtractionRule()
        .setRooElementToExtractFrom("//input[@id='input_selected']")
        .setOnElements(contentEntry);

Clone this wiki locally