Selenium For Beginners

Selenium

Using Github Copilot to create a Selenium automation project

Alex Siminiuc's avatar
Alex Siminiuc
Aug 31, 2025
∙ Paid
Share

Everyone hears every day about using Copilot for Selenium test automation.

I thought that I should give it a try as well, pretending that I am a beginner with both Selenium and Java.

The IDE for this exercise is IntelliJ.


Let’s start with the question:

How can i create a Selenium test automation project with Java and TestNG for the saucedemo.com site?

Please add as many tests as possible for this site.

I am a beginner with both java and selenium.


The Copilot answer is below.

To create a Selenium test automation project with Java and TestNG for saucedemo.com, follow these steps:

1. Set up Maven Project in IntelliJ IDEA

Create a new Maven project.

2. Add Dependencies in pom.xml

Add Selenium and TestNG dependencies.

<dependencies>
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.19.1</version>
  </dependency>

  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.9.0</version>
    <scope>test</scope>
  </dependency>
</dependencies>

3. Download ChromeDriver

Download ChromeDriver and add its path to your system environment variables.

4. Create Test Classes in src/test/java

Below is a sample TestNG test class with multiple tests for saucedemo.com:

  • Login test

  • Invalid login test

  • Add product to cart

  • Remove product from cart

  • Checkout process

Explanation:

Each test method uses Selenium to interact with the site and TestNG to organize and run the tests.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class SauceDemoTests {

    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://www.saucedemo.com/");
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testValidLogin() {
        driver.findElement(By.id("user-name")).sendKeys("standard_user");
	driver.findElement(By.id("password")).sendKeys("secret_sauce");
	driver.findElement(By.id("login-button")).click();

	Assert.assertTrue(driver.getCurrentUrl().contains("inventory.html"));
    }

    @Test
    public void testInvalidLogin() {
	driver.findElement(By.id("user-name")).sendKeys("invalid_user");
	driver.findElement(By.id("password")).sendKeys("wrong_password");
	driver.findElement(By.id("login-button")).click();
	
        Assert.assertTrue(
            driver.findElement(
                By.cssSelector("h3[data-test='error']")).isDisplayed());
    }

    @Test
    public void testAddProductToCart() {
        login();

	driver.findElement(By.id("add-to-cart-sauce-labs-backpack")).click();
	
        String cartCount = driver.findElement(
                               By.className("shopping_cart_badge"))
                             .getText();

	Assert.assertEquals(cartCount, "1");
    }

    @Test
    public void testRemoveProductFromCart() {
        login();

	driver.findElement(By.id("add-to-cart-sauce-labs-backpack")).click();
	driver.findElement(By.id("remove-sauce-labs-backpack")).click();
	
        Assert.assertTrue(
            driver.findElements(By.className("shopping_cart_badge"))
          .isEmpty());
    }

    @Test
    public void testCheckoutProcess() {
        login();

	driver.findElement(By.id("add-to-cart-sauce-labs-backpack")).click();
	driver.findElement(By.className("shopping_cart_link")).click();

	driver.findElement(By.id("checkout")).click();

	driver.findElement(By.id("first-name")).sendKeys("John");

	driver.findElement(By.id("last-name")).sendKeys("Doe");

	driver.findElement(By.id("postal-code")).sendKeys("12345");

	driver.findElement(By.id("continue")).click();

	driver.findElement(By.id("finish")).click();

	Assert.assertTrue(
           driver.findElement(By.className("complete-header"))
         .isDisplayed());
    }

    private void login() {
	driver.findElement(By.id("user-name")).sendKeys("standard_user");
	driver.findElement(By.id("password")).sendKeys("secret_sauce");
	driver.findElement(By.id("login-button")).click();
    }

}

5. How to run:

Right-click the test class and select Run.

TestNG will execute all test methods.


How useful is this answer for a beginner?

Let’s see.

Keep reading with a 7-day free trial

Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Alex Siminiuc
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture