I am currently developing a test to verify that a link on a page is correctly referencing the intended destination (link generated dynamically). In order to test this link, I plan to use an AJAX call in Selenium to retrieve the correct URL from the server and compare it. However, I am facing difficulty in retrieving the value from the AJAX call made in Selenium.
JavascriptExecutor jse = (JavascriptExecutor)driver;
String jsCode =
"var str = $.get('/User/GetDynamicURL/', function (result) {"
+ "console.log('result: ' + result); "
+ "return result;"
+ "})"
+ "return str;";
String dynamicURL = (String) jse.executeScript(jsCode);
Despite seeing the expected output in the console, my code is not functioning properly. I am encountering the following exception:
java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to java.lang.String
When running the code:
var str = $.get('/User/GetDynamicURL/', function (result) {
console.log('result: ' + result);
return result;
});
str;
If I inspect str
in the console of my browser, I receive an object resembling:
{readyState: 1, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
Additionally, accessing str.responseJSON
returns the desired URL.
I would appreciate any guidance on resolving this issue with my code. Thank you!