使用 Selenium WebDriver 读取 JavaScript 变量 [英] Reading JavaScript variables using Selenium WebDriver
问题描述
我正在使用 Selenium WebDriver (Java) 和 TestNG 在我创建的网站上进行一些测试.在这个网站中,我也有 JavaScript,在一些函数中,它返回值,并通过 console.log()
将值输出到浏览器控制台.
我想知道是否有一种简单的方法可以让 Selenium WebDriver 访问一些 JavaScript 信息,以便我可以使用 TestNG 执行断言.
我对 Selenium 很陌生,但我知道您可以执行以下操作:
WebDriver driver = new ChromeDriver();driver.findElement(By.id("btn")).click();
那么我可以使用 WebDriver
来读取网站上的 JavaScript 吗?
澄清
看起来人们假设我正在尝试通过 Selenium执行"JavaScript 代码.
事实并非如此.相反,我正在尝试使用 Selenium 存储已定义的 JavaScript 变量.
基本上,我希望 Selenium 能够获取 JavaScript 变量的值,将其存储在本地,然后对其进行断言测试.
<小时>尝试 1
假设我的网站有以下 JS 代码:
$(document).ready(function() {var foo = $(#"input-field-val").val();函数 returnFoo() {返回 foo;}});
根据我的阅读和理解,在我单独的 Selenium 测试文件 (Selenium.java) 中,我应该能够做这样的事情?:
公共类硒{WebDriver 驱动程序 = 新的 FirefoxDriver();JavascriptExecutor js = (JavascriptExecutor) 驱动程序;@测试公共无效 testSample() {driver.get("www.mywebsite.com");js.executeScript("alert(returnFoo());");}}
我做了与上面类似的事情,但没有弹出警告框.相反,我收到一条错误消息:
线程main"org.openqa.selenium.WebDriverException 中的异常:ReferenceError: returnFoo 未定义
我很确定我不明白它所说的 JS 变量是什么意思
<块引用>不应是闭包或局部变量的一部分
我也尝试在 $(document).ready(function()...
上方定义一个全局变量,并且设置在 function returnFoo()
内,但仍然不起作用.
尝试 2
我已经将 foo
和 returnFoo()
移到了 $(document).ready(function()...
之外. 修复了我在上面 Attempt 1 中得到的 ReferenceError
消息.
我还给了 foo
一个值,所以我的 JS 代码看起来像这样:
var foo = "Selenium 测试运行";$(document).ready(function() {...});函数 returnFoo() {返回 foo;}
现在,我很难将 returnFoo()
的返回值分配给 Selenium 测试中的局部变量.这是我尝试过的:
public static void main(String[] args) {WebDriver 驱动程序 = 新的 FirefoxDriver();JavascriptExecutor js = (JavascriptExecutor) 驱动程序;driver.get("http://localhost:8080/HTML5TestApp/prod.html");Object val = js.executeScript("window.returnFoo();");System.out.println(val);}
但是控制台显示null
而不是Selenium test run"的实际值.
尝试 2 - 解决方案
看起来如果我执行 Object val = js.executeScript("alert(returnFoo());");
我得到了 foo
的值.><小时>
解决方案
感谢下面 Martin Foot 的解决方案,这是我针对我的问题提出的解决方案.
在我的 JavaScript 文件中,我创建了一个 var
和一个 setter/getter 函数,如下所示:
index.js
var seleniumGlobal;$(document).ready(function() {...)};函数 setSG(toSet) {seleniumGlobal = toSet;}函数 getSG() {返回 seleniumGlobal;}
SampleTest.java
//做所有必要的导入公共类 SampleTest {WebDriver 驱动程序 = 新的 FirefoxDriver();JavascriptExecutor js = (JavascriptExecutor) 驱动程序;@测试公共无效 testPrintVal() {String sgVal = (String) js.executeScript("alert(getSG());");Assert.assertEquals("seleniumGlobal 的新值", sgVal);}}
因此,每当我的 JavaScript 中的某些代码通过 setter 方法设置我的 seleniumGlobal
变量时,我都可以通过我的 Selenium 测试调用它并对其进行断言.
这可能不是最有效的方法,但如果其他人有更好的解决方案,请告诉我.
在 Ruby 中,您可以使用 page.execute_script
来评估 JavaScript 变量(如果它可以从 Web 浏览器的范围内访问).看起来在 Java here.
这可能是一个更适合 JavaScript 单元测试框架的用例,例如 Jasmine一>.
I'm using Selenium WebDriver (Java) and TestNG to do some testing on a website I created. In this website, I also have JavaScript and in some of the functions, it returns values and also outputs values to the browser console through console.log()
.
I was wondering if there is an easy way for Selenium WebDriver to access some of this JavaScript information so I can perform assertions using TestNG.
I'm quite new to Selenium but I understand that you can do something like:
WebDriver driver = new ChromeDriver();
driver.findElement(By.id("btn")).click();
So is there anything similar I can do using WebDriver
to read the JavaScript on the site?
Clarification
It looks like people are making the assumption that I'm trying to "execute" JavaScript code through Selenium.
Thats not the case. Instead, I'm trying to store already-defined JavaScript variable using Selenium.
Basically, I want Selenium to be able to grab the JavaScript variable's value, store it locally, and then do an assertion test on it.
Attempt 1
Say I have the following JS code for my website:
$(document).ready(function() {
var foo = $(#"input-field-val").val();
function returnFoo() {
return foo;
}
});
From what I've reading and understanding, in my seperate Selenium test file (Selenium.java), I should be able to do something like this?:
public class Selenium {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
@Test
public void testSample() {
driver.get("www.mywebsite.com");
js.executeScript("alert(returnFoo());");
}
}
I do something similar to what's above but no alert box pops up. Instead, I get an error message:
Exception in thread "main" org.openqa.selenium.WebDriverException: ReferenceError: returnFoo is not defined
I'm pretty sure I'm not understanding what it means when its said that the JS variable
should not be part of a closure or local variable
I have also tried defining a global variable above the $(document).ready(function()...
and setting is within function returnFoo()
but still doesn't work.
Attempt 2
I've moved both foo
and returnFoo()
outside of the $(document).ready(function()...
. That has fixed ReferenceError
message that I was getting in Attempt 1 above.
I hav also given foo
a value so my JS code looks something like this:
var foo = "Selenium test run";
$(document).ready(function() {
...
});
function returnFoo() {
return foo;
}
Now, I'm having a tough time assigning the return of returnFoo()
to a local variable within my Selenium test. Here's what I've attempted:
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("http://localhost:8080/HTML5TestApp/prod.html");
Object val = js.executeScript("window.returnFoo();");
System.out.println(val);
}
But the console display null
instead of the actual value of "Selenium test run".
Attempt 2 - SOLUTION
It looks like if I do Object val = js.executeScript("alert(returnFoo());");
I get the value of foo
.
SOLUTION
So here's the solution I've come up w/ to my problem thanks to the solution by Martin Foot below.
In my JavaScript file, I created a var
and a setter/getter function like so:
index.js
var seleniumGlobal;
$(document).ready(function() {
...
)};
function setSG(toSet) {
seleniumGlobal = toSet;
}
function getSG() {
return seleniumGlobal;
}
SampleTest.java
// Do all the necessary imports
public class SampleTest {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
@Test
public void testPrintVal() {
String sgVal = (String) js.executeScript("alert(getSG());");
Assert.assertEquals("new value for seleniumGlobal", sgVal);
}
}
So whenever some code in my JavaScript sets my seleniumGlobal
variable through the setter method, I can call it through my Selenium test and do assertions on it.
This is probably not the most efficient way to do but if someone else has a better solution, please let me know.
In Ruby you can use page.execute_script
to evaluate a JavaScript variable (if it is accessable from the scope of the web browser). It looks like there is a similar method in Java here.
Edit: This might be a use case that is more suited to a JavaScript unit testing framework such as Jasmine.
这篇关于使用 Selenium WebDriver 读取 JavaScript 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!