使用Selenium WebDriver读取JavaScript变量 [英] Reading JavaScript variables using Selenium WebDriver

查看:1238
本文介绍了使用Selenium WebDriver读取JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium WebDriver(Java)和TestNG在我创建的网站上进行一些测试。在这个网站上,我也有JavaScript,在一些函数中,它通过 console.log()返回值并输出值到浏览器控制台。

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().

我想知道Selenium WebDriver是否有一种简单的方法可以访问某些JavaScript信息,因此我可以使用TestNG执行断言。

我对Selenium很新,但我知道你可以这样做:

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();

所以我可以使用 WebDriver 阅读网站上的JavaScript?

So is there anything similar I can do using WebDriver to read the JavaScript on the site?

看起来人们假设我正试图通过Selenium执行JavaScript代码。

It looks like people are making the assumption that I'm trying to "execute" JavaScript code through Selenium.

但事实并非如此。相反,我正在尝试使用Selenium存储已定义的JavaScript变量。

Thats not the case. Instead, I'm trying to store already-defined JavaScript variable using Selenium.

基本上,我希望Selenium能够获取JavaScript变量的值,将其存储在本地,然后对它进行断言测试。

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.

说我的网站有以下JS代码:

Say I have the following JS code for my website:

$(document).ready(function() {
    var foo = $(#"input-field-val").val();

    function returnFoo() {
        return foo;
    }
});

根据我的阅读和理解,在我单独的Selenium测试文件(Selenium.java)中,我应该可以做这样的事情吗?:

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

我很确定我不明白当它说JS时它意味着什么变量

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

我还尝试在 $(文件).ready(function()... 之上定义一个全局变量,并且设置在<$ c $之内c>函数returnFoo()但仍然不起作用。

I have also tried defining a global variable above the $(document).ready(function()... and setting is within function returnFoo() but still doesn't work.

我移动了 foo returnFoo() $(document).ready(function()... 。这已经修复了 ReferenceError 消息,我是进入上面的尝试1

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.

我还给了 foo 一个值,所以我的JS代码看起来像这样:

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;
}

现在,我很难分配<$ c的回报$ c> returnFoo()到我的Selenium测试中的局部变量。以下是我的尝试:

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);
    } 

但控制台显示 null 而不是Selenium test run的实际值

看起来如果我做 Object val = js.executeScript(alert(returnFoo());); 我得到<$ c的值$ c> foo 。

It looks like if I do Object val = js.executeScript("alert(returnFoo());"); I get the value of foo.

所以这是解决方案我已经找到了我的问题,感谢Martin Foot的解决方案。

So here's the solution I've come up w/ to my problem thanks to the solution by Martin Foot below.

在我的JavaScript文件中,我创建了一个 var 以及类似的setter / getter函数:

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);
    }
}

因此,只要我的JavaScript中的某些代码设置了我的 seleniumGlobal 变量通过setter方法,我可以通过我的Selenium测试调用它并对其进行断言。

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.

推荐答案

在Ruby中你可以使用 page.execute_script 来评估JavaScript变量(如果它可以从Web浏览器的范围访问)。看起来Java中存在类似的方法此处

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.

编辑:这可能是一个更适合JavaScript单元测试框架的用例,例如 Jasmine

This might be a use case that is more suited to a JavaScript unit testing framework such as Jasmine.

这篇关于使用Selenium WebDriver读取JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆