The aim here is to create an automation script that interacts with textboxes and textareas. We will assume that the textbox/textarea element has been found by means of its ID (we will go deeper into the findElement method in an upcoming section):
WebElement textArea = driver.findElement(By.
id("aboutYourself"));
Open the https://trainingbypackt.github.io/Beginning-Selenium/lesson_3/exercise_3_1.html file and use IntelliJ IDEA for the creation of a Selenium script.
The steps for completion of this process are as follows:
- Review and analyze the structure and behavior of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_3/exercise_3_1.html file.
- Create a new Java file for the automation script. Make sure that you include the required libraries:
package com.beginningselenium.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
- Locate the textarea of the By.id "aboutYourself":
WebElement textArea = driver.findElement(By.
id("aboutYourself"));
- Verify that the textarea is enabled and visible:
if (textArea.isEnabled() && textArea.isDisplayed())
- Using the System.out.println method in the if loop, display messages indicating whether the verification was successful or not:
System.out.println("The text area is visible and
displayed");
else {
System.out.println("Something went wrong, the text
area is not visible and displayed");
}
- If the textarea is enabled and visible, verify that it is empty:
// Checking for an empty text area
if ("".equals(textArea.getAttribute("value")))
Have the code of steps 6-11 inside the if (textArea.isEnabled() && textArea.isDisplayed()) loop.
- Using the System.out.println method, display messages indicating whether the verification was successful or not:
{
System.out.println("The text area is empty");
} else {
System.out.println("Something is wrong, the text
area NOT empty");
}
- Input some text and then verify that the text was actually typed:
textArea.sendKeys("This is a sample text.");
if ("This is a sample text.".equals(textArea.
getAttribute("value")))
- Using the System.out.println method, display messages indicating whether the verification was successful or not and that the name was sent.
{
System.out.println("Text was correctly typed into
the text area.");
} else {
System.out.println("Something went wrong, text
was not typed into the text area.");
}
- Clear the textarea contents and verify that it is empty:
textArea.clear();
if ("".equals(textArea.getAttribute("value")))
- Using the System.out.println method, display messages indicating whether the verification was successful or not and that the name was sent:
{
System.out.println("The text area is empty after
cleaning it though a Selenium command");
} else {
System.out.println("Something went wrong, the
text area was not cleaned");
}
- Compile and run the script.