Issue
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)?
Solution
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();
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.