Articles → SELENIUM → Test Validations In Web Application Using Selenium
Test Validations In Web Application Using Selenium
Steps
- Go to http://gyansangrah.com/ContactUs.aspx
- You will see the following web form.
Click to Enlarge
- Click on ‘Submit’ button.
- Alert message should be appearing with this text – ‘Invalid email’.
Code
package com.FirstSeleniumProject;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MyClass {
public static void main(String[] args) {
WebDriver driver;
String service = "IEDriverServer path";
System.setProperty("webdriver.ie.driver", service);
driver = new InternetExplorerDriver();
driver.get("http://gyansangrah.com/ContactUs.aspx");
driver.findElement(By.name("ctl00$cphGyanSangrah$btnSubmit")).click();
//driver.switchTo().alert().accept();
Alert alert = driver.switchTo().alert();
String alertMessage = alert.getText();
alert.accept();
WebElement emailId = driver.findElement(By.name("ctl00$cphGyanSangrah$txtEmailID"));
String textInsideInputBox = emailId.getAttribute("value");
// Check whether input field is blank
if (textInsideInputBox.trim().equalsIgnoreCase("Your Email ID") && alertMessage.trim().equalsIgnoreCase("Invalid email")) {
System.out.println("Email Validation is fine");
}
}
}
Output
Click to Enlarge
Click to Enlarge