cWarren cWarren - 1 year ago 58
AngularJS Question

Is Selenium Web Driver (Firefox) able to read AngularJS ng-model injected initial values?

I have a text input field that is bound to a javascript object using ng-model. The javascript object provides an initial value for the field.

<input class="class1 class2" type="text" ng-model="scopeVar"></input>

scopeVar is a text value.

When the page loads, Selenium (using the firefox driver) can find the input field, verify that it is visible, can enter text into the field and then read that text back, but it can not read the text that was initially in the field.

If I execute $('.class1.class2').val() in the firefox browser window it returns the expected default value.

If I execute ((JavascriptExecutor) driver).executeScript("$('.class1.class2').val()") it returns null.
If I execute driver.driver.findElement(By.cssSelector(".class1.class2")).getText() it returns null.

So it looks like Angular is correctly populating the DOM (as far a jQuery is concerned) but Selenium Web Driver can't read it.

Note: I do verify that I have an element and that it is visible before I try to read it.

Any assistance is appreciated.


I don't know about your app specifically, but this works against a ng-model mapped input field:

    WebDriver wd = new FirefoxDriver();
    WebElement input = wd.findElement(By.tagName("input"));
    input = wd.findElement(By.tagName("input"));
    System.out.println("fieldValue=" + input.getAttribute("value"));