Reputation: 395
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));
Upvotes: 2
Views: 960
Reputation: 395
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));
Upvotes: 1