How to use JavaScriptExecutor class of com.github.epadronu.balin.core package

Best Balin code snippet using com.github.epadronu.balin.core.JavaScriptExecutor

Run Balin automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

JavaScriptExecutor.kt

Source: JavaScriptExecutor.kt Github

copy
1/******************************************************************************
2 * Copyright 2016 Edinson E. Padrón Urdaneta
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17/* ***************************************************************************/
18package com.github.epadronu.balin.core
19/* ***************************************************************************/
20
21/* ***************************************************************************/
22/**
23 * Describes an easier way to interact with
24 * [JavascriptExecutor.executeScript][org.openqa.selenium.JavascriptExecutor.executeScript] &
25 * [JavascriptExecutor.executeAsyncScript][org.openqa.selenium.JavascriptExecutor.executeAsyncScript],
26 * allowing the execution of synchronous and asynchronous JavaScript code if
27 * such functionality is supported by the underlying driver.
28 *
29 * ### Synchronous code
30 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_invoke_operator
31 *
32 * ### Asynchronous code
33 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_an_asynchronous_javascript_code
34 */
35interface JavaScriptExecutor {
36
37    /**
38     * Executes JavaScript in the context of the currently selected frame or
39     * window. The script fragment provided will be executed as the body of an
40     * anonymous function.
41     *
42     * Within the script, use document to refer to the current document. Note
43     * that local variables will not be available once the script has finished
44     * executing, though global variables will persist.
45     *
46     * If the script has a return value (i.e. if the script contains a return
47     * statement), then the following steps will be taken:
48     *
49     * - For an HTML element, this method returns a WebElement
50     * - For a decimal, a Double is returned
51     * - For a non-decimal number, a Long is returned
52     * - For a boolean, a Boolean is returned
53     * - For all other cases, a String is returned.
54     * - For an array, return a List<Object> with each object following the rules above. We support nested lists.
55     * - For a map, return a Map<String, Object> with values following the rules above.
56     * - Unless the value is null or there is no return value, in which null is returned
57     *
58     * Arguments must be a number, a boolean, a String, WebElement, or a List
59     * of any combination of the above. An exception will be thrown if the
60     * arguments do not meet these criteria. The arguments will be made
61     * available to the JavaScript via the "`arguments`" magic variable, as if
62     * the function were called via "`Function.apply`"
63     *
64     * In the case of `async = true`, unlike executing synchronous JavaScript,
65     * scripts executed with this method must explicitly signal they are
66     * finished by invoking the provided callback. This callback is always
67     * injected into the executed function as the last argument.
68     *
69     * The default timeout for a script to be executed is 0ms. In most cases,
70     * including the examples below, one must set the script timeout
71     * ([Timeouts.setScriptTimeout][org.openqa.selenium.WebDriver.Timeouts.setScriptTimeout])
72     * beforehand to a value sufficiently large enough.
73     *
74     * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_execute_method
75     * @see org.openqa.selenium.JavascriptExecutor.executeScript
76     * @see org.openqa.selenium.JavascriptExecutor.executeAsyncScript
77     *
78     * @param args optional arguments that can be passed to the JS code
79     * @param async indicates if the JS code should be executed asynchronously or not
80     * @param script provides the JS code to be executed
81     * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.
82     */
83    fun execute(vararg args: Any, async: Boolean = false, script: () -> String): Any?
84
85    /**
86     * Executes JavaScript in the context of the currently selected frame or
87     * window. The script fragment provided will be executed as the body of an
88     * anonymous function.
89     *
90     * Within the script, use document to refer to the current document. Note
91     * that local variables will not be available once the script has finished
92     * executing, though global variables will persist.
93     *
94     * If the script has a return value (i.e. if the script contains a return
95     * statement), then the following steps will be taken:
96     *
97     * - For an HTML element, this method returns a WebElement
98     * - For a decimal, a Double is returned
99     * - For a non-decimal number, a Long is returned
100     * - For a boolean, a Boolean is returned
101     * - For all other cases, a String is returned.
102     * - For an array, return a List<Object> with each object following the rules above. We support nested lists.
103     * - For a map, return a Map<String, Object> with values following the rules above.
104     * - Unless the value is null or there is no return value, in which null is returned
105     *
106     * Arguments must be a number, a boolean, a String, WebElement, or a List
107     * of any combination of the above. An exception will be thrown if the
108     * arguments do not meet these criteria. The arguments will be made
109     * available to the JavaScript via the "`arguments`" magic variable, as if
110     * the function were called via "`Function.apply`"
111     *
112     * In the case of `async = true`, unlike executing synchronous JavaScript,
113     * scripts must explicitly signal they are finished by invoking the
114     * provided callback. This callback is always injected into the executed
115     * function as the last argument.
116     *
117     * The default timeout for a script to be executed is 0ms. In most cases,
118     * including the examples below, one must set the script timeout
119     * ([Timeouts.setScriptTimeout][org.openqa.selenium.WebDriver.Timeouts.setScriptTimeout])
120     * beforehand to a value sufficiently large enough.
121     *
122     * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_call_method
123     * @see org.openqa.selenium.JavascriptExecutor.executeScript
124     * @see org.openqa.selenium.JavascriptExecutor.executeAsyncScript
125     *
126     * @param args optional arguments that can be passed to the JS code
127     * @param async indicates if the JS code should be executed asynchronously or not
128     * @param script provides the JS code to be executed
129     * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.
130     */
131    fun call(vararg args: Any, async: Boolean = false, script: () -> String): Any? = execute(
132        *args, async = async, script = script
133    )
134
135    /**
136     * Executes JavaScript in the context of the currently selected frame or
137     * window. The script fragment provided will be executed as the body of an
138     * anonymous function.
139     *
140     * Within the script, use document to refer to the current document. Note
141     * that local variables will not be available once the script has finished
142     * executing, though global variables will persist.
143     *
144     * If the script has a return value (i.e. if the script contains a return
145     * statement), then the following steps will be taken:
146     *
147     * - For an HTML element, this method returns a WebElement
148     * - For a decimal, a Double is returned
149     * - For a non-decimal number, a Long is returned
150     * - For a boolean, a Boolean is returned
151     * - For all other cases, a String is returned.
152     * - For an array, return a List<Object> with each object following the rules above. We support nested lists.
153     * - For a map, return a Map<String, Object> with values following the rules above.
154     * - Unless the value is null or there is no return value, in which null is returned
155     *
156     * Arguments must be a number, a boolean, a String, WebElement, or a List
157     * of any combination of the above. An exception will be thrown if the
158     * arguments do not meet these criteria. The arguments will be made
159     * available to the JavaScript via the "`arguments`" magic variable, as if
160     * the function were called via "`Function.apply`"
161     *
162     * In the case of `async = true`, unlike executing synchronous JavaScript,
163     * scripts executed with this method must explicitly signal they are
164     * finished by invoking the provided callback. This callback is always
165     * injected into the executed function as the last argument.
166     *
167     * The default timeout for a script to be executed is 0ms. In most cases,
168     * including the examples below, one must set the script timeout
169     * ([Timeouts.setScriptTimeout][org.openqa.selenium.WebDriver.Timeouts.setScriptTimeout])
170     * beforehand to a value sufficiently large enough.
171     *
172     * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_run_method
173     * @see org.openqa.selenium.JavascriptExecutor.executeScript
174     * @see org.openqa.selenium.JavascriptExecutor.executeAsyncScript
175     *
176     * @param args optional arguments that can be passed to the JS code
177     * @param async indicates if the JS code should be executed asynchronously or not
178     * @param script provides the JS code to be executed
179     * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.
180     */
181    fun run(vararg args: Any, async: Boolean = false, script: () -> String): Any? = execute(
182        *args, async = async, script = script
183    )
184
185    /**
186     * Executes JavaScript in the context of the currently selected frame or
187     * window. The script fragment provided will be executed as the body of an
188     * anonymous function.
189     *
190     * Within the script, use document to refer to the current document. Note
191     * that local variables will not be available once the script has finished
192     * executing, though global variables will persist.
193     *
194     * If the script has a return value (i.e. if the script contains a return
195     * statement), then the following steps will be taken:
196     *
197     * - For an HTML element, this method returns a WebElement
198     * - For a decimal, a Double is returned
199     * - For a non-decimal number, a Long is returned
200     * - For a boolean, a Boolean is returned
201     * - For all other cases, a String is returned.
202     * - For an array, return a List<Object> with each object following the rules above. We support nested lists.
203     * - For a map, return a Map<String, Object> with values following the rules above.
204     * - Unless the value is null or there is no return value, in which null is returned
205     *
206     * Arguments must be a number, a boolean, a String, WebElement, or a List
207     * of any combination of the above. An exception will be thrown if the
208     * arguments do not meet these criteria. The arguments will be made
209     * available to the JavaScript via the "`arguments`" magic variable, as if
210     * the function were called via "`Function.apply`"
211     *
212     * In the case of `async = true`, unlike executing synchronous JavaScript,
213     * scripts executed with this method must explicitly signal they are
214     * finished by invoking the provided callback. This callback is always
215     * injected into the executed function as the last argument.
216     *
217     * The default timeout for a script to be executed is 0ms. In most cases,
218     * including the examples below, one must set the script timeout
219     * ([Timeouts.setScriptTimeout][org.openqa.selenium.WebDriver.Timeouts.setScriptTimeout])
220     * beforehand to a value sufficiently large enough.
221     *
222     * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_invoke_operator
223     * @see org.openqa.selenium.JavascriptExecutor.executeScript
224     * @see org.openqa.selenium.JavascriptExecutor.executeAsyncScript
225     *
226     * @param args optional arguments that can be passed to the JS code
227     * @param async indicates if the JS code should be executed asynchronously or not
228     * @param script provides the JS code to be executed
229     * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.
230     */
231    operator fun invoke(vararg args: Any, async: Boolean = false, script: () -> String): Any? = execute(
232        *args, async = async, script = script
233    )
234
235    /**
236     * Get the value of a global-JavaScript variable.
237     *
238     * @sample com.github.epadronu.balin.core.JavaScriptTests.set_a_global_js_variable_and_retrieve_it_via_a_get
239     *
240     * @param value the name of the variable which value will be retrieved.
241     * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.
242     */
243    operator fun get(value: String): Any? = execute { "return $value;" }
244
245    /**
246     * Set the value of a global-JavaScript variable.
247     *
248     * @sample com.github.epadronu.balin.core.JavaScriptTests.set_a_global_js_variable_and_retrieve_it_via_a_get
249     *
250     * @param name the name of the variable.
251     * @param value the value of the variable. (It can be null.)
252     */
253    operator fun set(name: String, value: Any?) {
254        when (value) {
255            null -> execute { "window.$name = null;" }
256            else -> execute(value) { "window.$name = arguments[0];" }
257        }
258    }
259}
260/* ***************************************************************************/
261
Full Screen

JavaScriptSupport.kt

Source: JavaScriptSupport.kt Github

copy
1/******************************************************************************
2 * Copyright 2016 Edinson E. Padrón Urdaneta
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17/* ***************************************************************************/
18package com.github.epadronu.balin.core
19/* ***************************************************************************/
20
21/* ***************************************************************************/
22/**
23 * Describes the `js` property support, which aims to ease the use of
24 * [JavascriptExecutor.executeScript][org.openqa.selenium.JavascriptExecutor.executeScript] &
25 * [JavascriptExecutor.executeAsyncScript][org.openqa.selenium.JavascriptExecutor.executeAsyncScript].
26 *
27 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_invoke_operator
28 */
29interface JavaScriptSupport {
30
31    /**
32     * Allows the execution of synchronous and asynchronous JavaScript code if
33     * such functionality is supported by the underlying driver.
34     */
35    val js: JavaScriptExecutor
36}
37/* ***************************************************************************/
38
Full Screen

BrowserImpl.kt

Source: BrowserImpl.kt Github

copy
1/******************************************************************************
2 * Copyright 2016 Edinson E. Padrón Urdaneta
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17/* ***************************************************************************/
18package com.github.epadronu.balin.core
19/* ***************************************************************************/
20
21/* ***************************************************************************/
22import com.github.epadronu.balin.config.ConfigurationSetup
23import org.openqa.selenium.JavascriptExecutor
24import org.openqa.selenium.WebDriver
25import org.openqa.selenium.support.ui.ExpectedCondition
26import org.openqa.selenium.support.ui.WebDriverWait
27/* ***************************************************************************/
28
29/* ***************************************************************************/
30internal class BrowserImpl(
31    override val configurationSetup: ConfigurationSetup,
32    override val driver: WebDriver = configurationSetup.driverFactory()) : Browser, WebDriver by driver {
33
34    override val js = object : JavaScriptExecutor {
35        override fun execute(vararg args: Any, async: Boolean, script: () -> String): Any? {
36            if (driver is JavascriptExecutor) {
37                return when (async) {
38                    false -> driver.executeScript(script(), *args)
39                    else -> driver.executeAsyncScript(script(), *args)
40                }
41            }
42
43            throw UnsupportedOperationException()
44        }
45    }
46}
47/* ***************************************************************************/
48
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in JavaScriptExecutor

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie