Reputation:
I am working with Selenium and Java to write automated tests. I was given a task to validate mandatory fields in forms. The idea is that I try to submit the form without filling a mandatory field and I check the popup message. If it's there with the message, then it's a success. One form has several mandatory fields.
The issue is that the popup message - as I was told by dev collegues - is part of the browser and it's not part of the HTML code in any way, so I can't use findElement(By)
as usual. Also the warning is not a separate window, you cannot click on it (because it disappears), it's just a bubble of some sorts which says: "Please fill out this field"
How can I locate this message with Selenium(Java)?
Upvotes: 1
Views: 4450
Reputation: 1
I was actually looking for the same issue and decided to consolidate my findings in this article: https://www.linkedin.com/pulse/working-font-end-web-form-validations-using-test-automation-mohie/
In the above scenario, you can do three validations
validate that the field is required.
// this boolean value can be either true if the attribute exists or null if it doesn't
Boolean isRequired = driver.findElement(username).getAttribute("required");
validate that the message text is configured correctly.
driver.findElement(username).getAttribute("validationMessage");
validate that the message is actually displayed.
This is the tricky part where you can use visual validation in case the element is not displayed on the main page DOM
Upvotes: 0
Reputation: 29362
There can be two thing.
First : I am assuming input
fields which contains required attribute.
Something like this : Username: <input type="text" name="usrname" required>
If you do not provide anything in this input and hit on submit button, you would get this error message : "Please fill out this field"
, which is outcome of HTML5 required attribute(You may refer it as first level of verification).
You can catch this exception message like this :
String message = driver.findElement(By.name("usrname")).getAttribute("validationMessage");
If you will print message String , you will get this : Please fill out this field
Second : There may be a Javascript validation alert which pops up when there are improper input given by the user. In such cases, you will have to switch to alert prompt and then retrieve the message from the the specific alert.
Code you can try out in this case :
Alert alert = driver.switchTo().alert();
alert.getText();
Upvotes: 4