Execute java script command on Android device using appium studio

Execute java script command on Android device using appium studio

I want to execute below Java Script from Visual stduio C#

Var pageLoadState = ((IJavaScriptExecutor)mkConstants.webDriver).ExecuteScript(@“return document.readyState;”).ToString();

Hi Michael

We return the value that is stored in an object called “result” in your javascript context

e.g -

  var result1 = driver.ExecuteScript(@"var result = document.readyState;");

result1 will be “complete” when the page finished loading

Hi Tom,

I have tried your code,

var result1 = driver.ExecuteScript(@“var result = document.readyState;”);

And as well as my another code,

var pageLoadState1 = driver.ExecuteScript(@“if (document != undefined && document.readyState) { return document.readyState;} else { return undefined;}”).ToString();

both throwing following exception,

unknown error (An unknown server-side error occurred. status=‘false’. Failed to complete internal method: ‘hybridRunJavascript args: [, 0, if (document != undefined && document.readyState) { return document.readyState;} else { return undefined;}]’, details: Failed to run javascript ‘’. invalid return (line#1). invalid return (line#1))

Can you provide solution for this issue?

Hi Michael

Just a quick confirmation on the context of the application when you are running the script, is it a web application?

for your second script, i’d try :slight_smile:

var pageLoadState1 = driver.ExecuteScript(@"var result = (document != undefined && document.readyState)  ? true : false").ToString();

we are returning the the execution the value of a variable named result, so you must place your returned value in the var result

Hi Tom,

I tried your code[var pageLoadState1 = driver.ExecuteScript(@“var result = (document != undefined && document.readyState) ? true : false”).ToString();]

,but i got the the same error

unknown error (An unknown server-side error occurred. status=‘false’. Failed to complete internal method: ‘hybridRunJavascript args: [, 0, if (document != undefined && document.readyState) { return document.readyState;} else { return undefined;}]’, details: Failed to run javascript ‘’. invalid return (line#1). invalid return (line#1))

This error indicates that your script is still the one from the old post [quote=“michealarun, post:3, topic:596”]
var pageLoadState1 = driver.ExecuteScript(@“if (document != undefined && document.readyState) { return document.readyState;} else { return undefined;}”).ToString();
[/quote]

Hi tom,

I have updated error information below,

unknown error (An unknown server-side error occurred. status=‘false’. Failed to complete internal method: ‘hybridRunJavascript args: [, 0, var result = (document != undefined && document.readyState) ? true: false]’, details: complete is not defined STACK: ReferenceError: complete is not defined

at <anonymous>:2:64)

I guess it’s something specific to your application…

if you’d like to share your application, you can send it to me - tom_p@experitest.com

Reviving this one because I have the same issue.

I get the error message: Failed to run javascript ":
no matter what the script command is