This is an example of an online form that contains several components that are frequently seen on web pages like a radio button, drop-downs, date picker, checkboxes, file upload, etc.
You will become familiar with all the fundamental WebDriver commands required for web page automation by automating this form.
package exercises;
import java.util.List;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Exercise5 {
public static void main(String[] args) {
//Launch chrome browser
WebDriver driver = new ChromeDriver();
//Open Url
driver.get("https://proleed.academy/exercises/selenium/automate-the-signup-form-using-selenium-webdriver.php");
driver.findElement(By.name("firstname")).sendKeys("enter name");
driver.findElement(By.name("lastname")).sendKeys("enter last name");
driver.findElement(By.id("male")).click();
//Select class for drop-down
Select experience = new Select(driver.findElement(By.name("experience")));
experience.selectByVisibleText("5");
driver.findElement(By.id("date")).sendKeys("16/08/2023");
driver.findElement(By.id("automation")).click();
//Find all elements for multiple selection of check boxes
List skills = driver.findElements(By.name("skills"));
System.out.println("No. of checkboxes are " + skills.size());
for (int i = 0; i < skills.size(); i++)
{
// Uncomment below line to click all check boxes and comment next two lines
// skills.get(i).click();
skills.get(2).click();
skills.get(5).click();
}
//Select class for drop-down
Select country = new Select(driver.findElement(By.name("country")));
country.selectByVisibleText("India");
driver.findElement(By.name("photo")).sendKeys("C:\\Users\\Win-10\\Pictures\\Saved Pictures\\Sunscreen1.jpg");
driver.findElement(By.id("add")).click();
//Switch to alert
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println(alertText);
if ("Form submitted".equals(alertText))
{
System.out.println("Alert message is correct");
}
else
{
System.out.println("Unexpected alert message: " + alertText);
}
alert.accept();
}
}
Copyright © 2023 - Proleed Academy | All Rights Reserved.