Reputation: 41
I'm new in automation testing and have a task to implement the automation to Register Student using a given Registration Form.
The scenario is letting me a little bit confused on how to validate the input field in the registration form.
I mean, if I don't fill a required field, the browser will indicate that I must fill that field.
Another thing is that I'm using a for loop
for 2 students, where I want the first time to leave the input fields empty. After submit I have to see it was validated and then fill the empty input fields to submit it again (now with data inserted in the fields).
I'm sharing my code and would appreciate any help.
public class Cartus {
public static void fill() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "E:\\workspace\\chromeDriver\\chromeDriver.exe");
ChromeDriver driver = new ChromeDriver();
for(int i=0; i<2; i++) {
driver.get("http://qa-5.ls.vu/v2/landing-page/cartus/en");
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[1]/div")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[1]/div/select/option[2]")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys(/*"sami"*/);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys(/*"Mohaname"*/);
WebElement ele=driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input"));/*.sendKeys("0221-1234567");*/
String date = FastDateFormat.getInstance("yyyyMMddHHmmss").format(System.currentTimeMillis());
Thread.sleep(1000);
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input"))
.sendKeys(/*"aali_"+date+"@outlook.com"*/);
driver.findElement(By.id("submitButton")).click();
if(ele.getAttribute("value").isEmpty()){
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys("abcds");
}
if(ele.getAttribute("value").isEmpty()){
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys("abcds");
}
if(ele.getAttribute("value").isEmpty()){
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input")).sendKeys("abcds");
}
if(ele.getAttribute("value").isEmpty()){
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input")).sendKeys("aali_"+date+"@outlook.com");
}
System.out.println("Test case succesfully run");
}
}
}
Thank you.
Upvotes: 0
Views: 40816
Reputation: 45
Providing you text is within VALUE of the HTML then the below will work. For example -
value="YourText"
var UniqueName = YourElement.NameGoesHere.GetAttribute("value");
When debugging put a break point just after the above and hover over the UniqueName and youll see your text is returned and you do not get a NULL and thus can validate this works alot easier than running the whole test to just see it die.
Upvotes: 0
Reputation: 1411
Taking a look at your code and reading that you're new to automation tests, the first thing I can tell you is to try to avoid XPath
and use cssSelector
when possible.
If you have an ID or CSS class that can identify your element, it's safer and shorter.
Here's a good source to read and understand.
After that, I believe you have 2 problems in your question:
When you don't fill the information, the page has a few ways to validate the field (e.g. after the input has lost its focus, after clicking the button to submit the form etc).
In any way, you must check the HTML that was hidden and is then shown (with the warning message - e.g. "Field name is required") and use Selenium
to check if this element is now present.
// findElements (using the plural) will not result in "Element not found exception"
WebElement nameMessage = driver.findElements(By.id("nameErrorMessage"));
if (nameMessage.size > 0) {
nameMessage.get(0).getText(); // do your validation to check if the message is the one expected
} else {
throw new Exception("Warning message was not displayed properly");
}
In this situation, you could have an object to hold the information used in the form, put it in a list and use it.
Here an example of how it could be:
class Student {
private String name;
private Date birthday;
private String className;
public Student(String name, Date birthday, String class) {
this.name = name;
this.birthday = birthday;
this.className = class;
}
// all getters and setters
}
class StudentTest {
Student emptyStudent = new Student("", null, "");
Student student = new Student("John", new Date(), "7th grade");
List<Student> students = Arrays.asList(emptyStudent, student);
// Set up your selenium
// Loop your data to be tested
students.forEach(student -> {
// Fill the information with the student info
driver.findElement(By.id("name")).sendKeys(student.getName());
driver.findElement(By.id("birthday")).sendKeys(student.getBirthday());
driver.findElement(By.id("className")).sendKeys(student.getClassName());
// Submit the form
driver.findElement(By.id("save")).click();
// Check if warning message is shown (code the logic properly to work with and without it)
});
}
Hope it helps you solve your problem.
Upvotes: 3