- Import the required packages:
package com.beginningselenium.selenium;
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;
import java.util.List;
- Locate an element by using a ID. Use lastname as the ID. Print the output when it's displayed and when it's not.
WebElement lastName = driver.findElement(By.id("lastName"));
if (lastName.isDisplayed()) {
System.out.println("Automation script worked, lastName element is visible");
} else {
System.out.println("Something went wrong with the test, lastName element is not visible");
}
- Locate an element by using a name. Use hobbies as the ID. Print the output when it's displayed and when it's not.
WebElement hobbies = driver.findElement(By.name("hobbies"));
if (hobbies.isDisplayed()) {
System.out.println("Automation script worked, hobbies element is visible");
} else {
System.out.println("Something went wrong with the test, hobbies element is not visible");
}
- Locate an element by using a class name. Use form-control as the class name. Print the output when the first name is displayed and when it's not.
WebElement firstName = driver.findElement(By.className("form-control"));
if (firstName.isDisplayed()) {
System.out.println("Automation script worked, hobbies element is visible");
} else {
System.out.println("Something went wrong with the test, hobbies element is not visible");
}
- Locate an element by using an HTML tag. Use div as the class name. Print the output when div.size() > 0 and otherwise.
List<WebElement> div = driver.findElements(By. tagName("div"));
if (div.size() > 0) {
System.out.println("Automation script worked, there are more than zero divs on the page");
} else {
System.out.println("Something went wrong with the test, divs could not be located");
}
- Locate an element by using a link. Use Spanish as the class name. Print the output when link = spanishLink.getAttribute("href") and when it's not.
WebElement spanishLink = driver.findElement(By. linkText("Spanish")); String link = spanishLink.getAttribute("href"); if (!"".equals(link)) { System.out.println("Automation script worked, the link was found"); } else { System.out.println("Something went wrong with the test, link was not found"); }
- Locate an element by using xpath. Use //select as the xpath. Print the output when dayOfBirth.getOptions().size() > 0 and when this condition is not met.
Select dayOfBirth = new Select(driver.findElement(By.xpath("//select")));
if (dayOfBirth.getOptions().size() > 0) {
System.out.println("Automation script worked, the element was found");
} else {
System.out.println("Something went wrong with the test, the element was not found");
}
- Locate an element by using CSS. Use #firstName as the CSS attribute. Print the output when the first name with the CSS is displayed and when it's not.
WebElement firstNameWithCss = driver.findElement(By.cssSelector("#firstName"));
if (firstNameWithCss.isDisplayed()) {
System.out.println("Automation script worked, firstName with CSS element is visible");
} else {
System.out.println("Something went wrong with the test, firstName with CSS element is not visible");
}
- Verify that each element was found by interacting with it.
- Compile and run the script.