2

I'm testing a webpage with Selenium WebDrive and want to make an assertion on a JavaScript file but not sure how.

I want to assert that servers (an array) is of length 2.

My JavaScript file config.js contains the following array I wish to assert is present:

var location = location || {};
location.Config = {
    servers: [
        {name: "a"}, 
        {name: "b"}
   ]
}

My first attempt was to use className but didn't do the trick:

Assert.assertThat(webDriver
        .findElement(className("config.js"))
        .getAttribute("servers")
        .length(), 
    Matchers.is(2));
3
  • Is the file "config.js" a resource of the page? Commented Mar 29, 2016 at 10:23
  • The "config.js" file is within the site being tested and the webdriver has a handle into the site. I hope I've not misunderstood your question Commented Mar 29, 2016 at 10:29
  • What do you get if you execute "location.Config.servers[0].name" is the console of the browser ? Commented Mar 29, 2016 at 10:45

1 Answer 1

1

Success!

Passing the browser a JavaScript command to run and capturing its result has done the trick.

long servers = (long) ((JavascriptExecutor) webDriver).executeScript("return location.Config.servers.length");

Assert.assertThat(servers, Matchers.greaterThan((long) 0));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.