Whether you are a beginner or an intermediate user, hands-on experience is key to mastering Selenium testing techniques. In this article, Novatesting will provide you with a series of training exercises and possible code solutions in Java related to utility testing, acceptance testing, regression testing and exploratory testing to help you gain a deeper understanding of Selenium testing.

  1. Utility testing exercises:

a. Write a test case to check the functionality of a calculator using Selenium and Java.

typescript
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CalculatorTest {
    private WebDriver driver;

    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.calculator.net/");
    }

    @Test
    public void testAddition() {
        driver.findElement(By.xpath("//input[@name='cInput']")).sendKeys("2+3");
        driver.findElement(By.xpath("//input[@value='Calculate']")).click();
        String result = driver.findElement(By.xpath("//span[@id='sciOutPut']")).getText();
        assert result.equals("5") : "Expected 5 but got " + result;
    }

    @Test
    public void testSubtraction() {
        driver.findElement(By.xpath("//input[@name='cInput']")).sendKeys("5-2");
        driver.findElement(By.xpath("//input[@value='Calculate']")).click();
        String result = driver.findElement(By.xpath("//span[@id='sciOutPut']")).getText();
        assert result.equals("3") : "Expected 3 but got " + result;
    }

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

b. Write a test case to verify that the login functionality of a web application using Selenium and Java.

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginTest {
    private WebDriver driver;

    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.example.com/login");
    }

    @Test
    public void testLogin() {
        driver.findElement(By.xpath("//input[@name='username']")).sendKeys("user");
        driver.findElement(By.xpath("//input[@name='password']")).sendKeys("password");
        driver.findElement(By.xpath("//input[@value='Login']")).click();
        String greeting = driver.findElement(By.xpath("//div[@id='greeting']")).getText();
        assert greeting.equals("Hello user!") : "Expected 'Hello user!' but got " + greeting;
    }

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

2. Acceptance testing exercises:
a. Write a test case to verify that a user can purchase a product on an e-commerce website using Selenium and Java.

typescript
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class PurchaseTest {
    private WebDriver driver;

    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.example.com/products");
    }

    @Test
    public void testPurchase() {
        driver.findElement(By.xpath("//a[contains(text(),'product name')]")).click();
        driver.findElement(By.xpath("//button[contains(text(),'Add to Cart')]")).click();
        driver.findElement(By.xpath("//a[contains(text(),'View Cart')]")).click();
        driver.findElement(By.xpath("//a[contains(text(),'Checkout')]")).click();
        driver.findElement(By.xpath("//input[@name='username']")).sendKeys("user");
        driver.findElement(By.xpath("//input[@name='password']")).sendKeys("password");
        driver.findElement(By.xpath("//input[@value='Login']")).click();
        driver.findElement(By.xpath("//button[contains(text(),'Place Order')]")).click();
        String orderConfirmation = driver.findElement(By.xpath("//div[@id='orderConfirmation']")).getText();
        assert orderConfirmation.equals("Order placed successfully!") : "Expected 'Order placed successfully!' but got " + orderConfirmation;
    }

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

b. Write a test case to verify that a user can subscribe to a newsletter on a website using Selenium and Java.

typescript
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SubscribeTest {
    private WebDriver driver;

    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.example.com/");
    }

    @Test
    public void testSubscribe() {css

        driver.findElement(By.xpath("//input[@name='email']")).sendKeys("[email protected]");
        driver.findElement(By.xpath("//button[contains(text(),'Subscribe')]")).click();
        String confirmationMessage = driver.findElement(By.xpath("//div[@id='subscriptionConfirmation']")).getText();
        assert confirmationMessage.equals("Subscribed successfully!") : "Expected 'Subscribed successfully!' but got " + confirmationMessage;
    }

    @After
    public void tearDown() {
        driver.quit();
    }
}
  1. Regression testing exercises:

    a. Write a test case to verify that a user can add a product to their cart and the total amount updates correctly on an e-commerce website using Selenium and Java.

    typescript
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class AddToCartTest {
        private WebDriver driver;
    
        @Before
        public void setUp() {
            System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
            driver = new ChromeDriver();
            driver.get("https://www.example.com/products");
        }
    
        @Test
        public void testAddToCart() {
            driver.findElement(By.xpath("//a[contains(text(),'product name')]")).click();
            driver.findElement(By.xpath("//button[contains(text(),'Add to Cart')]")).click();
            String totalAmount = driver.findElement(By.xpath("//span[@id='totalAmount']")).getText();
            assert totalAmount.equals("$100.00") : "Expected '$100.00' but got " + totalAmount;
        }
    
        @After
        public void tearDown() {
            driver.quit();
        }
    }
    typescript
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class ChangePasswordTest {
        private WebDriver driver;
    
        @Before
        public void setUp() {
            System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
            driver = new ChromeDriver();
            driver.get("https://www.example.com/");
        }
    
        @Test
        public void testChangePassword() {
            driver.findElement(By.xpath("//a[contains(text(),'Change Password')]")).click();
            driver.findElement(By.xpath("//input[@name='oldPassword']")).sendKeys("oldpassword");
            driver.findElement(By.x
    path("//input[@name='confirmPassword']")).sendKeys("newpassword");
    driver.findElement(By.xpath("//button[contains(text(),'Save Changes')]")).click();
    String confirmationMessage = driver.findElement(By.xpath("//div[@id='passwordChangedConfirmation']")).getText();
    assert confirmationMessage.equals("Password changed successfully!") : "Expected 'Password changed successfully!' but got " + confirmationMessage;
    }
    @After
    public void tearDown() {
        driver.quit();
    }
    1. Exploratory testing exercises:
      a. Write a test case to verify the search functionality on a website using Selenium and Java.
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class SearchFunctionalityTest {
    private WebDriver driver;
    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.example.com/");
    }
    
    @Test
    public void testSearchFunctionality() {
        driver.findElement(By.xpath("//input[@name='searchTerm']")).sendKeys("product name");
        driver.findElement(By.xpath("//button[contains(text(),'Search')]")).click();
        String searchResult = driver.findElement(By.xpath("//div[@id='searchResult']")).getText();
        assert searchResult.contains("product name") : "Expected 'product name' in the search result but got " + searchResult;
    }
    
    @After
    public void tearDown() {
        driver.quit();
    }
    }

        b. Write a test case to verify the navigation on a website using Selenium and Java.

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class NavigationTest {
    private WebDriver driver;
    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.example.com/");
    }
    
    @Test
    public void testNavigation() {
        driver.findElement(By.xpath("//a[contains(text(),'About Us')]")).click();
        String aboutUsPageTitle = driver.getTitle();
        assert aboutUsPageTitle.equals("About Us") : "Expected 'About Us' page title but got " + aboutUsPageTitle;
    }
    
    @After
    public void tearDown()
    {
    driver.quit();
    }
    }

    In conclusion, this article has covered four key testing techniques: utility, acceptance, regression and exploratory testing, through a series of practical exercises and code solutions in Selenium and Java. These exercises will help you to build your skills and knowledge, as well as gain hands-on experience in applying Selenium testing techniques in real-world situations. With the knowledge and experience you have gained from this article, you can continue to grow and develop your automation testing skills with Selenium.

    By admin1

    Leave a Reply

    Your email address will not be published. Required fields are marked *