70

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.

FilmiHero
  • 2,306
  • 7
  • 31
  • 46
  • 1
    a bit old, but I suggest you have a look at my solution. Some of my colleagues stumble upon this page and were doing the alert "hack" where actually you should just be returning the value as you can see on my Answer below :) – Jose Sutilo Aug 02 '13 at 22:14
  • 18
    Simply: `executeScript("return globalVar;")` – wtr Oct 04 '13 at 00:25
  • @wTyeRogers What is the purpose of `return` keyword here? If you write this into the console it throws and error. What is the human equivalent to what Selenium is doing with this command? – Sam Creamer Apr 02 '19 at 15:31

3 Answers3

54

All you have to do is:

Object val = js.executeScript("return returnFoo();");

That will give you what you are looking for.

fmsf
  • 36,317
  • 49
  • 147
  • 195
Jose Sutilo
  • 908
  • 1
  • 5
  • 7
  • 1
    For those using Ruby, an example is `val = driver.execute_script("return $('#something').offset().left")` – Jack Jun 04 '14 at 23:22
31

No JavaScript functions need be defined. Nor is alert() needed.

Object result = js.executeScript("return globalVar");

For Python:

result = driver.execute_script("return globalVar")
wtr
  • 762
  • 8
  • 16
14

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.

Martin Foot
  • 3,594
  • 3
  • 28
  • 35
  • Martin is right on both counts: you absolutely can run arbitrary Javascript code through (any language's) Selenium Webdriver. However, Selenium has a host of ... less than desirable aspects, so you'll likely find that you don't want to use it unless you absolutely have to (ie. for functional and integration testing only). For Javascript unit testing (and some integration testing) I highly recommend using a JS testing framework. Martin mentioned Jasmine, and QUnit is popular also, but if you want a JUnit (or TestNG)-like command line test runner I recommend JS-Test-Driver or Buster. – machineghost Dec 21 '12 at 17:30
  • Jasmine also has a JUnit compliant test reported, which I'm using for CI builds :) Edit: it's a bit of a pain to set up though! – Martin Foot Dec 21 '12 at 17:31
  • Thanks. I'll take a closer look at the `JavascriptExecutor`. When you say **"if it is accessable from the scope of the web browser"** do you just mean that as long as the webpage is importing it through ` – FilmiHero Dec 21 '12 at 17:31
  • BTW, all of those JS testing frameworks will run in real browsers, giving you the same "real world" benefits as Selenium (as opposed to say running head-less Nose tests). They won't however take anywhere close to as long to run as Selenium tests, nor will they have Selenium's other issues (eg. random test stops for no good reason). – machineghost Dec 21 '12 at 17:34
  • 1
    I mean that the variable you're accessing is not part of a closure or a local variable. I.E. it is accessible through some path from the window object. If in your console I can see window.bla.bla.yourvariable, you can access it from execute_script. Otherwise you'll need to manually set it. If this is the case, a JS unit test is better. Making things accessible from the window scope is generally bad. – Martin Foot Dec 21 '12 at 17:35
  • Correct: Selenium can execute any random JS you want, but that JS (just like any other random JS you run on the page) will only have access to the global scope. – machineghost Dec 21 '12 at 17:39
  • I've added **Attempt 1** section above in which I tried using `JavascriptExecutor` but am not getting the expected results. Could someone explain what I may be doing wrong? – FilmiHero Dec 21 '12 at 18:23
  • Looks like I can use `JavascriptExecutor` to get the values. I've added a solution section above. Please let me know if there is a better way. Thanks for all the help! – FilmiHero Dec 21 '12 at 19:48
  • Definitely a use for unit testing framework. Please could you clean up the question a bit, as the attempt section as is no longer necessary. Moving the function outside of the anonymous callback fixed the reference error because that attaches it to the window scope rather than the scope of the function itself. It is similar to the fact that in Java you cannot access a member variable from an anonymous in a class without a getter method. – Martin Foot Dec 22 '12 at 11:46
  • @MartinFoot, can you point where I can look into getting the local variables? I can see what I am after in the Dev Tools debug, but having a hard time connecting it to python Selenium to pull out those variables to store it? Any way you could help? – chitown88 Nov 11 '21 at 12:14