Reputation: 21856
I am checking validations on login form with Selenium webdriver. The password field does not get cleared with clear. Similar code is working for username field. Am I missing something?
it('Password is required', function(done) {
usernameElem.clear();
// skipping the test because clearing password field does not work!
passwordElem.clear();
loginElem.click();
driver.findElements(By.className('text-danger')).then(elements => {
elements.length.should.equal(2);
elements[1].getText().then(text => {
text.should.equal('Password is required.');
done();
});
});
});
Upvotes: 0
Views: 1228
Reputation: 76
I am assuming we are not getting any exception for the line:
passwordElem.clear();
This should not be necessary, but you can first click on the password field (so it will get focus) and then clear it.
passwordElem.click();
passwordElem.clear();
You can put a sleep before trying to clear password field.
Thread.sleep(3000); // Wait for 3 seconds
You can use explicit wait to make sure the password field is ready, before you attempt to clear it.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(passwordElem)).clear();
You can confirm once that the password field is enabled and displayed as per the source code of the webpage:
System.out.println("Password field is enabled: " + passwordElem.isEnabled());
System.out.println("Password field is displayed: " + passwordElem.isDisplayed());
If any of above statement says false, then we will know the reason why the password field is not getting cleared.
Upvotes: 2