I have written my tests in Protractor and I've used an expect statement:

loginPage.email.sendKeys( params.login.email );
expect(loginPage.email.getText()).toEqual( params.login.email );

However, my test is failing because signUpPage.email.getText() is returning an empty string. I couldn't find what function to call in Selenium's documentation for input field to return the correct value?

In my Protractor.conf file:

 params: {
      login: {
        email: '[email protected]',
        password: 'blahblah123'
      }

Error in Terminal:

Expected '' to equal '[email protected]'.

So I'm trying to match that the input of the email field is the same as the email address I've sent through. Any suggestions how to do that in Selenium?

share|improve this question
up vote 2 down vote accepted

If this is an input field, you need to get the value of the value attribute:

expect(loginPage.email.getAttribute("value")).toEqual(params.login.email);

Similarly, to apply Saifur's idea here, if you need to wait for the element to contain the desired value, use textToBePresentInElementValue Expected Condition:

var EC = protractor.ExpectedConditions;
browser.wait(EC.textToBePresentInElementValue(loginPage.email, params.login.email), 5000);

Note that timeout value (in ms) has to be specified.

share|improve this answer
    
Thanks Alex, I was missing the timeout component at the end and didn't initially pick that up when I read through Saifur's link! – Ryan Drake May 14 '15 at 6:43
    
Your answer is more accurate. I totally missed the timeout. – Saifur May 14 '15 at 12:05
2  
"get the value of the value attribute". Nope. What developers 99.99..% of the time want is not the value of the value attribute but the value of the value property. When input is changed by the user, what changes it the property while the attribute remains constant (unless there is code added to change it). In their infinite lack of wisdom, the Selenium devs decided that .getAttribute("value") would be treated specially and return the value of the property rather than the value of the attribute. This is sure to mislead people when they use the DOM directly or other tools. – Louis May 14 '15 at 16:29
    
Hi guys, I'm getting this issue: TypeError: Cannot read property 'bind' of undefined Any luck with how you've dealt with this before? – Ryan Drake May 15 '15 at 0:41
    
@Louis that's just a great point! I love reading your detailed comments and answers, always learning something new. – alecxe May 15 '15 at 10:36

Probably element load time is the issue here. In that case you can use ExpectedCondition and even match the text you are looking for.

var EC = protractor.ExpectedConditions;
browser.wait(EC.textToBePresentInElement(loginPage.email ,params.login.email));

Update Missed the time of Expected condition. Thanks @alecxe

browser.wait(EC.textToBePresentInElementValue(loginPage.email, params.login.email), 5000);
share|improve this answer
    
Saifur, I've tried EC.textToBePresentInElement and EC.textToBePresentInElementValue without much luck. I am getting TypeError: Cannot read property 'bind' of undefined. Thanks for the link, however, as that's quite useful! – Ryan Drake May 14 '15 at 1:46

If you want to get the value of an input field managed by AngularJS using solely a WebDriver instance there is an easy way to achieve that.

I tested it using the FirefoxWebDriver and it worked perfectly.

Basically you'll have to use the JavascriptExecutor to execute a javascript code in the browser.

If your web project has the jQuery plugin, then you can use the following method:

WebDriver driver = DriverFactory.getDriver();

public String getTextAt(String cssId) {
    if (driver instanceof JavascriptExecutor) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
        return (String) javascriptExecutor.executeScript("return $('" + cssId + "').val();");
    }
    return null;
}

If you want to use raw javascript you can do it like this:

WebDriver driver = DriverFactory.getDriver();

public String getTextAt(String cssId) {
    if (driver instanceof JavascriptExecutor) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
        return (String) javascriptExecutor.executeScript("return documenet.getElementById('" + cssId + "').value;");
    }
    return null;
}

Note that in this case the cssId should be an id attribute.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.