Best Balin code snippet using com.github.epadronu.balin.core.JavaScriptExecutor.execute
JavaScriptExecutor.kt
Source:JavaScriptExecutor.kt
...18/* ***************************************************************************/19/* ***************************************************************************/20/**21 * Describes an easier way to interact with22 * [JavascriptExecutor.executeScript][org.openqa.selenium.JavascriptExecutor.executeScript] &23 * [JavascriptExecutor.executeAsyncScript][org.openqa.selenium.JavascriptExecutor.executeAsyncScript],24 * allowing the execution of synchronous and asynchronous JavaScript code if25 * such functionality is supported by the underlying driver.26 *27 * ### Synchronous code28 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_invoke_operator29 *30 * ### Asynchronous code31 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_an_asynchronous_javascript_code32 */33interface JavaScriptExecutor {34 /**35 * Executes JavaScript in the context of the currently selected frame or36 * window. The script fragment provided will be executed as the body of an37 * anonymous function.38 *39 * Within the script, use document to refer to the current document. Note40 * that local variables will not be available once the script has finished41 * executing, though global variables will persist.42 *43 * If the script has a return value (i.e. if the script contains a return44 * statement), then the following steps will be taken:45 *46 * - For an HTML element, this method returns a WebElement47 * - For a decimal, a Double is returned48 * - For a non-decimal number, a Long is returned49 * - For a boolean, a Boolean is returned50 * - For all other cases, a String is returned.51 * - For an array, return a List<Object> with each object following the rules above. We support nested lists.52 * - For a map, return a Map<String, Object> with values following the rules above.53 * - Unless the value is null or there is no return value, in which null is returned54 *55 * Arguments must be a number, a boolean, a String, WebElement, or a List56 * of any combination of the above. An exception will be thrown if the57 * arguments do not meet these criteria. The arguments will be made58 * available to the JavaScript via the "`arguments`" magic variable, as if59 * the function were called via "`Function.apply`"60 *61 * In the case of `async = true`, unlike executing synchronous JavaScript,62 * scripts executed with this method must explicitly signal they are63 * finished by invoking the provided callback. This callback is always64 * injected into the executed function as the last argument.65 *66 * The default timeout for a script to be executed is 0ms. In most cases,67 * including the examples below, one must set the script timeout68 * ([Timeouts.setScriptTimeout][org.openqa.selenium.WebDriver.Timeouts.setScriptTimeout])69 * beforehand to a value sufficiently large enough.70 *71 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_execute_method72 * @see org.openqa.selenium.JavascriptExecutor.executeScript73 * @see org.openqa.selenium.JavascriptExecutor.executeAsyncScript74 *75 * @param args optional arguments that can be passed to the JS code76 * @param async indicates if the JS code should be executed asynchronously or not77 * @param script provides the JS code to be executed78 * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.79 */80 fun execute(vararg args: Any, async: Boolean = false, script: () -> String): Any?81 /**82 * Executes JavaScript in the context of the currently selected frame or83 * window. The script fragment provided will be executed as the body of an84 * anonymous function.85 *86 * Within the script, use document to refer to the current document. Note87 * that local variables will not be available once the script has finished88 * executing, though global variables will persist.89 *90 * If the script has a return value (i.e. if the script contains a return91 * statement), then the following steps will be taken:92 *93 * - For an HTML element, this method returns a WebElement94 * - For a decimal, a Double is returned95 * - For a non-decimal number, a Long is returned96 * - For a boolean, a Boolean is returned97 * - For all other cases, a String is returned.98 * - For an array, return a List<Object> with each object following the rules above. We support nested lists.99 * - For a map, return a Map<String, Object> with values following the rules above.100 * - Unless the value is null or there is no return value, in which null is returned101 *102 * Arguments must be a number, a boolean, a String, WebElement, or a List103 * of any combination of the above. An exception will be thrown if the104 * arguments do not meet these criteria. The arguments will be made105 * available to the JavaScript via the "`arguments`" magic variable, as if106 * the function were called via "`Function.apply`"107 *108 * In the case of `async = true`, unlike executing synchronous JavaScript,109 * scripts must explicitly signal they are finished by invoking the110 * provided callback. This callback is always injected into the executed111 * function as the last argument.112 *113 * The default timeout for a script to be executed is 0ms. In most cases,114 * including the examples below, one must set the script timeout115 * ([Timeouts.setScriptTimeout][org.openqa.selenium.WebDriver.Timeouts.setScriptTimeout])116 * beforehand to a value sufficiently large enough.117 *118 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_call_method119 * @see org.openqa.selenium.JavascriptExecutor.executeScript120 * @see org.openqa.selenium.JavascriptExecutor.executeAsyncScript121 *122 * @param args optional arguments that can be passed to the JS code123 * @param async indicates if the JS code should be executed asynchronously or not124 * @param script provides the JS code to be executed125 * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.126 */127 fun call(vararg args: Any, async: Boolean = false, script: () -> String): Any? = execute(128 *args, async = async, script = script129 )130 /**131 * Executes JavaScript in the context of the currently selected frame or132 * window. The script fragment provided will be executed as the body of an133 * anonymous function.134 *135 * Within the script, use document to refer to the current document. Note136 * that local variables will not be available once the script has finished137 * executing, though global variables will persist.138 *139 * If the script has a return value (i.e. if the script contains a return140 * statement), then the following steps will be taken:141 *142 * - For an HTML element, this method returns a WebElement143 * - For a decimal, a Double is returned144 * - For a non-decimal number, a Long is returned145 * - For a boolean, a Boolean is returned146 * - For all other cases, a String is returned.147 * - For an array, return a List<Object> with each object following the rules above. We support nested lists.148 * - For a map, return a Map<String, Object> with values following the rules above.149 * - Unless the value is null or there is no return value, in which null is returned150 *151 * Arguments must be a number, a boolean, a String, WebElement, or a List152 * of any combination of the above. An exception will be thrown if the153 * arguments do not meet these criteria. The arguments will be made154 * available to the JavaScript via the "`arguments`" magic variable, as if155 * the function were called via "`Function.apply`"156 *157 * In the case of `async = true`, unlike executing synchronous JavaScript,158 * scripts executed with this method must explicitly signal they are159 * finished by invoking the provided callback. This callback is always160 * injected into the executed function as the last argument.161 *162 * The default timeout for a script to be executed is 0ms. In most cases,163 * including the examples below, one must set the script timeout164 * ([Timeouts.setScriptTimeout][org.openqa.selenium.WebDriver.Timeouts.setScriptTimeout])165 * beforehand to a value sufficiently large enough.166 *167 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_run_method168 * @see org.openqa.selenium.JavascriptExecutor.executeScript169 * @see org.openqa.selenium.JavascriptExecutor.executeAsyncScript170 *171 * @param args optional arguments that can be passed to the JS code172 * @param async indicates if the JS code should be executed asynchronously or not173 * @param script provides the JS code to be executed174 * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.175 */176 fun run(vararg args: Any, async: Boolean = false, script: () -> String): Any? = execute(177 *args, async = async, script = script178 )179 /**180 * Executes JavaScript in the context of the currently selected frame or181 * window. The script fragment provided will be executed as the body of an182 * anonymous function.183 *184 * Within the script, use document to refer to the current document. Note185 * that local variables will not be available once the script has finished186 * executing, though global variables will persist.187 *188 * If the script has a return value (i.e. if the script contains a return189 * statement), then the following steps will be taken:190 *191 * - For an HTML element, this method returns a WebElement192 * - For a decimal, a Double is returned193 * - For a non-decimal number, a Long is returned194 * - For a boolean, a Boolean is returned195 * - For all other cases, a String is returned.196 * - For an array, return a List<Object> with each object following the rules above. We support nested lists.197 * - For a map, return a Map<String, Object> with values following the rules above.198 * - Unless the value is null or there is no return value, in which null is returned199 *200 * Arguments must be a number, a boolean, a String, WebElement, or a List201 * of any combination of the above. An exception will be thrown if the202 * arguments do not meet these criteria. The arguments will be made203 * available to the JavaScript via the "`arguments`" magic variable, as if204 * the function were called via "`Function.apply`"205 *206 * In the case of `async = true`, unlike executing synchronous JavaScript,207 * scripts executed with this method must explicitly signal they are208 * finished by invoking the provided callback. This callback is always209 * injected into the executed function as the last argument.210 *211 * The default timeout for a script to be executed is 0ms. In most cases,212 * including the examples below, one must set the script timeout213 * ([Timeouts.setScriptTimeout][org.openqa.selenium.WebDriver.Timeouts.setScriptTimeout])214 * beforehand to a value sufficiently large enough.215 *216 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_invoke_operator217 * @see org.openqa.selenium.JavascriptExecutor.executeScript218 * @see org.openqa.selenium.JavascriptExecutor.executeAsyncScript219 *220 * @param args optional arguments that can be passed to the JS code221 * @param async indicates if the JS code should be executed asynchronously or not222 * @param script provides the JS code to be executed223 * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.224 */225 operator fun invoke(vararg args: Any, async: Boolean = false, script: () -> String): Any? = execute(226 *args, async = async, script = script227 )228 /**229 * Get the value of a global-JavaScript variable.230 *231 * @sample com.github.epadronu.balin.core.JavaScriptTests.set_a_global_js_variable_and_retrieve_it_via_a_get232 *233 * @param value the name of the variable which value will be retrieved.234 * @return One of Boolean, Long, Double, String, List, Map or WebElement. Or null.235 */236 operator fun get(value: String): Any? = execute { "return $value;" }237 /**238 * Set the value of a global-JavaScript variable.239 *240 * @sample com.github.epadronu.balin.core.JavaScriptTests.set_a_global_js_variable_and_retrieve_it_via_a_get241 *242 * @param name the name of the variable.243 * @param value the value of the variable. (It can be null.)244 */245 operator fun set(name: String, value: Any?) {246 when (value) {247 null -> execute { "window.$name = null;" }248 else -> execute(value) { "window.$name = arguments[0];" }249 }250 }251}252/* ***************************************************************************/...
BrowserImpl.kt
Source:BrowserImpl.kt
...27internal class BrowserImpl(28 override val configurationSetup: ConfigurationSetup,29 override val driver: WebDriver = configurationSetup.driverFactory()) : Browser, WebDriver by driver {30 override val js = object : JavaScriptExecutor {31 override fun execute(vararg args: Any, async: Boolean, script: () -> String): Any? {32 if (driver is JavascriptExecutor) {33 return when (async) {34 false -> driver.executeScript(script(), *args)35 else -> driver.executeAsyncScript(script(), *args)36 }37 }38 throw UnsupportedOperationException()39 }40 }41}42/* ***************************************************************************/...
JavaScriptSupport.kt
Source:JavaScriptSupport.kt
...18/* ***************************************************************************/19/* ***************************************************************************/20/**21 * Describes the `js` property support, which aims to ease the use of22 * [JavascriptExecutor.executeScript][org.openqa.selenium.JavascriptExecutor.executeScript] &23 * [JavascriptExecutor.executeAsyncScript][org.openqa.selenium.JavascriptExecutor.executeAsyncScript].24 *25 * @sample com.github.epadronu.balin.core.JavaScriptTests.execute_javaScript_code_with_arguments_via_the_invoke_operator26 */27interface JavaScriptSupport {28 /**29 * Allows the execution of synchronous and asynchronous JavaScript code if30 * such functionality is supported by the underlying driver.31 */32 val js: JavaScriptExecutor33}34/* ***************************************************************************/...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!