I have written one method in java file and calling one method which is written in java script file with the help org.openqa.selenium.JavascriptExecutor
from that java file method. here is the code snippet:
public void validateFilename() {
JavascriptExecutor jsExec = (JavascriptExecutor) webDriver;
jsExec.executeScript("getFileName();");
}
function() {
window.getFileName = function() {
var fileName = "sampleFile.txt";
return fileName;
}
};
I am able to call method getFileName()
from java file but I am able to get the value of file name. If I give alert(fileName)
it is showing fileName but not able to return that method in java file.
Is there any way to return the value from js file to java file with the help of API of selenium JavascriptExecutor?
I have written one method in java file and calling one method which is written in java script file with the help org.openqa.selenium.JavascriptExecutor
from that java file method. here is the code snippet:
public void validateFilename() {
JavascriptExecutor jsExec = (JavascriptExecutor) webDriver;
jsExec.executeScript("getFileName();");
}
function() {
window.getFileName = function() {
var fileName = "sampleFile.txt";
return fileName;
}
};
I am able to call method getFileName()
from java file but I am able to get the value of file name. If I give alert(fileName)
it is showing fileName but not able to return that method in java file.
Is there any way to return the value from js file to java file with the help of API of selenium JavascriptExecutor?
Share Improve this question edited Jun 21, 2017 at 11:59 Avinash Jadhav asked Jun 21, 2017 at 10:19 Avinash JadhavAvinash Jadhav 1,3933 gold badges12 silver badges21 bronze badges 1-
Just curious,.. Why wrap
getFileName
in an IIFE to then assign it to thewindow
? – evolutionxbox Commented Jun 21, 2017 at 10:23
4 Answers
Reset to default 3JavascriptExecutor.executeScript("<javascript code>")
allows you to execute JavaScript code, but when the code you pass to executeScript returns a value, Selenium won't know what the exact return type is at run time as JavaScript can return anything like Number
, String
, Array
, Object
etc.
To handle all return types, executeScript
returns an object of 'Object' class which in turn can handle any return type of JavaScript. We can typecast the object returned to any one of following supported objects:
- For an HTML element in js return, this method returns a
WebElement
- For a decimal, a
Double
is returned - For a non-decimal number, a
Long
is returned - For a boolean, a
Boolean
is returned - For all other cases, a
String
is returned. - For an array, return a
List<Object>
with each object following the rules above. - For a map, return a
Map<String, Object>
with values following the rules above. Unless the value is null or there is no return value, in which null is returned
You need to cast it to String when trying to get the response. Something like this:
String fileName = (String) jsExec.executeScript("return getFileName();");
Try the following code,
jsExec.executeScript( "return getFileName()");
Try the following:
String txt = "return document.title";
JavascriptExecutor js = (JavascriptExecutor) driver;
String res = (String)js.executeScript(txt);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742316090a4420865.html
评论列表(0条)