Automate Your First Task in Selenium Java

So far, you’ve installed the IDE, created a new project, added Selenium WebDriver, and set up the browser driver. You’ve also learned HTML basics, how to identify elements using locators, perform basic interactions, and wait for elements to become interactable.

With all this knowledge, it's time to automate your first real task!

We’ll write a Selenium script to interact with a live webpage. The script will:

     Step 1: Open https://qafeast.com
     Step 2: Accept the cookie policy
     Step 3: Click on Tools > Demo site
     Step 4: Type Hello World into the editable text box

Before checking the sample code, try writing the script yourself using what you've learned.

Here’s what you need to do:

     1. Make sure Selenium is added to your pom.xml
     2. Create a new Java class inside src > main > java in your project
     3. Set up the browser driver (e.g., ChromeDriver)
     4. Visit https://qafeast.com and use browser dev tools to inspect and pick locators for:

               The Accept button in the cookie message
               The Tools menu item
               The Demo site link
               The Editable text box

     5. Write the script to perform click and type actions

Once you've tried on your own, check out the example script below.

Here is the example code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class FirstTask {

    public static void main(String[] args) {
        // Set the system property to tell Selenium where the ChromeDriver executable is located
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

        // Launch a new Chrome browser
        WebDriver driver = new ChromeDriver();

        //Set up an explicit wait with a 10-second timeout
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // Navigate to the specified URL in the browser
        driver.get("https://www.qafeast.com/");

        // Click the "Accept" button in the cookie consent banner
        driver.findElement(By.id("gdpr-cookie-accept")).click();

        // Click the "Tools" menu item in the header
        driver.findElement(By.cssSelector("li[class='tut_ fr_tls']")).click();

        // Click the "Demo Site" link
        driver.findElement(By.linkText("Demo Site")).click();

        // Wait until the "Textbox" section header becomes visible
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.xpath("//h2[text()='Textbox']")));

        //Type "Hello World" into the editable text box
        driver.findElement(By.id("editabletext")).sendKeys("Hello World");

        // Close the browser and end the session
        driver.quit();

    }
}

 

Related Tutorials

Related Questions






Read more