How to check firefox console output with Selenium/javascript? - javascript

When I enter the following command into the firefox console I get 65. This is the number I want to check with selenium.
$('#grid').data('kendoGrid').dataSource.total()
Is there anyway I can get selenium to check this?
I have tried the following,
To test out javaScript, (this correctly checks title),
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("return document.title");
Console.Write(var);
I have tried, both failed.
String num = (String)((IJavaScriptExecutor)driver).ExecuteScript("$('#grid').data('kendoGrid').dataSource.total()");
Console.Write(num);
&
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
String script = "$('grid').data('kendoGrid').dataSource.total()";
String num = (String)js.ExecuteScript("return", script);
Console.Write(num);
I'm just using Console.Write to see the output.

Related

Can not Execute JavaScript command using Selenium WebDriver in C#

I can not execute js command using selenium web driver. for example, I used 3 ways for this URL but all of them return null.
web.FindElementByJs("StackExchange.init.length");
IJavaScriptExecutor js = (IJavaScriptExecutor)web;
string title = (string)js.ExecuteScript("StackExchange.init.length");
web.ExecuteJavaScript("StackExchange.init.length");
I get it!
For an object, I should use 'window'.
IJavaScriptExecutor js = (IJavaScriptExecutor)web;
string title = (string)js.ExecuteScript("return window.StackExchange.init.length");

Extract and Store javascript variable value inside script written inside executeScript to normal java variable

In below-written code, I want to use the URL variable value inside the javascript written and I want to store it in String URL variable of java.
I tried to use the getParameter method but that is used in JSP. I tried to Search it elsewhere but everywhere the value extraction is explained through JSP. Please help. Is there any way other than JSP for using the value inside the script?
System.setProperty("webdriver.chrome.driver","F:\\Browser Chrome Driver files\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//WebDriver driver1 = null;
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("var url = prompt(\"URL\");");
String url = request.getParameter("url");
2nd attempt:
When I use this code,
System.setProperty("webdriver.chrome.driver","F:\\Browser Chrome Driver
files\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
//WebDriver driver1 = null;
JavascriptExecutor js = (JavascriptExecutor)driver;
//String script = ;
String url =(String)js.executeScript("var user = prompt(\"Enter your
name, please:\", \"https://myeg/login\");\r\n" +
"if (user != null) {\r\n" +
" return user;\r\n" +
"}");
Thread.sleep(5000);
driver.get(url);
I am getting Exception in thread "main" java.lang.NullPointerException: null value in entry: url=null. I want the URL to be accessed by the driver through javascript prompt. But getting null pointer exception on 2nd attempt.
You can use return in the JavaScript
String script = "var url = prompt(\"URL\");"
+ "return url;";
String url = (String)js.executeScript(script);
I found the solution myself. I used java and succeeded. I imported javax.swing.JOptionPane library and created object of JOptionPane class. Then used showInput dialog method and I stored the value I put in that dialog in a variable. It worked.

javascript executor gives null when trying to access window.propertyname

I can see these many properties in console if I type window in Chrome console and hit enter.
But when I am trying to access same properties via Javascript Executor it gives me null.
I tried:
String homepage = jse().executeScript("window.origin", "found homepage").toString();
System.out.println("home page is "+homepage);
and
String location = "function show_homepage() {var homepage = window.location.origin;return homepage;}"
String homepage = jse().executeScript(location, "found if email validated or not").toString();
System.out.println("Answer is "+homepage);
Reference:
window object from selenium webdriver is empty array
https://www.softwaretestingmaterial.com/javascriptexecutor-selenium-webdriver/
View list of all JavaScript variables in Google Chrome Console
I found the solution. The mistake I was doing, I was not returning function in JS snippet. I added one more line return show_homepage(); and it worked.
String location = "function show_homepage() {"+
"var homepage = window.location.origin;"+
"return homepage;"+
"}"+
"return show_homepage();";
Object str = js().executeScript(location, "");
System.out.println(str.toString());

How to get javascript tooltip message in Selenium?

I want to get a tool tip message which appear when mouse hover over an icon. I want to get the message in Selenium gettext() method and assign it to a String.
Below is my code:
JavascriptExecutor jse = (JavascriptExecutor)driver;
String script = "return driver.findElement(By.xpath(ObjectRepository.tooltipMsg)).getText();";
String message = ((JavascriptExecutor) driver).executeScript(script).toString();
Thread.sleep(3000);
System.out.println("message "+message);
This is not working and I'm getting the error driver is not defined
Error driver is not defined is thrown because of the below statement.
return driver.findElement(By.xpath(ObjectRepository.tooltipMsg)).getText();
This is actually not a Javascript, but a Java code to find the text of the webelement. Hence, driver defined in java cannot be used as such while creating a javascript.
Using Javascript:
String script = "return document.getElementById("your-id").innerHTML;";
String message = ((JavascriptExecutor) driver).executeScript(script).toString();
Using Java:
String message = driver.findElement(By.xpath("your XPath")).getText();
You need to provide the correct XPath or Id above to find the element. Let me know if you have any queries.
first, you need define your drvier:
WebDriver driver = new FirefoxDriver();
later, you can get attribute of element has tooltip
String message = driver.findElement(By.xpath("ObjectRepository.tooltipMsg")).getAttribute("title");

How to get a return value from IJavaScriptExecutor

I am using Selenium WebDriver (.net) and have an IJavaScriptExecutor that I call .ExecuteScript on. I want it to return a value to me, but I can't seem to get it to do so.
string displayedCallNumber = (string) _driver.JavaScript.ExecuteScript("return $('#CallNumberSearch').val();");
I can use jQuery as the page that I am using has jQuery automatically. What am I doing wrong?
P.S. $('#CallNumberSearch').val(); works when I am in the browser w/o Selenium
You could try:
JavascriptExecutor js = (JavascriptExecutor)driver;
String str = js.executeScript("script");

Categories