Uploaded by kumarvishantmkcoe

Automation Testing

advertisement
Test case scenario
Here is an example of a test case using Selenium and Java to test the functional aspect of a website:
Test Case: Verify Login Functionality
Preconditions:

The web browser is open and the website is loaded

The user has a valid login username and password
Steps:
1. Click on the login button to navigate to the login page
2. Enter the login username and password in the appropriate fields
3. Click on the submit button to log in
4. Verify that the user is redirected to the home page
5. Verify that the user's name is displayed in the top right corner of the page
Expected Result:

The login process is successful and the user is able to access the home page

The user's name is displayed in the top right corner of the page
Test Case: Verify Search Functionality
Preconditions:

The web browser is open and the website is loaded

The user is logged in
Steps:
1. Enter a search term in the search field
2. Click on the search button
3. Verify that the search results page is displayed
4. Verify that the search results contain the search term
Expected Result:

The search results page is displayed and contains the search term
Test Case: Verify Product Details Page Functionality
Preconditions:

The web browser is open and the website is loaded

The user is logged in

A product has been selected from the search results
Steps:
1. Click on the product to navigate to the product details page
2. Verify that the product name, image, and price are displayed on the page
3. Click on the add to cart button
4. Verify that the product is added to the cart and the cart count is updated
Expected Result:

The product details page is displayed correctly and the product can be added to the cart
This is just a basic example of how you could use Selenium and Java to test the functional aspect of a
website. You can add additional test cases and steps as needed to cover all the features and
functionality of the website.
Test Case: Verify Login Functionality
Preconditions:

The web browser is open and the website is loaded

The user has a valid login username and password
Steps:
1. Click on the login button to navigate to the login page
2. Enter the login username and password in the appropriate fields
3. Click on the submit button to log in
4. Verify that the user is redirected to the home page
5. Verify that the user's name is displayed in the top right corner of the page
Expected Result:

The login process is successful and the user is able to access the home page

The user's name is displayed in the top right corner of the page
To implement this test case using Selenium and Java, you would need to write code to perform the
following actions:
1. Find the login button element on the page and click on it
2. Find the username and password fields on the login page and enter the login credentials
3. Find the submit button and click on it to log in
4. Verify that the user is redirected to the correct page by checking the URL or by finding specific
elements on the page
5. Find the element that displays the user's name and verify that it is displayed correctly
Here is an example of how this test case could be implemented using Selenium and Java:
Example code
// Import Selenium libraries and set up WebDriver
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
public static void main(String[] args) {
// Set up WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to website
driver.get("https://www.example.com/");
// Click on login button
driver.findElement(By.id("login-button")).click();
// Enter login credentials and submit form
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("testpass");
driver.findElement(By.id("submit-button")).click();
// Verify that user is redirected to home page
String expectedUrl = "https://www.example.com/home";
String actualUrl = driver.getCurrentUrl();
if (!expectedUrl.equals(actualUrl)) {
System.out.println("Error: expected URL " + expectedUrl + " but got " + actualUrl);
}
// Verify that user's name is displayed
String expectedName = "Test User";
String actualName = driver.findElement(By.id("user-name")).getText();
if (!expectedName.equals(actualName)) {
System.out.println("Error: expected name " + expectedName + " but got " + actualName);
}
// Close web browser
driver.quit();
}
}
This code will perform the steps of the test case using Selenium commands to interact with the website
and verify the expected results. You may need to adjust the element selectors and other details to
match your specific website.
Test Case: Verify Search Functionality
Preconditions:

The web browser is open and the website is loaded

The user is logged in
Steps:
1. Enter a search term in the search field
2. Click on the search button
3. Verify that the search results page is displayed
4. Verify that the search results contain the search term
Expected Result:

The search results page is displayed and contains the search term
To implement this test case using Selenium and Java, you would need to write code to perform the
following actions:
1. Find the search field element on the page and enter the search term
2. Find the search button element and click on it
3. Verify that the search results page is displayed by checking the URL or by finding specific
elements on the page
4. Find the elements that display the search results and verify that they contain the search term
Here is an example of how this test case could be implemented using Selenium and Java:
Example code
// Import Selenium libraries and set up WebDriver
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SearchTest {
public static void main(String[] args) {
// Set up WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to website and log in
driver.get("https://www.example.com/");
driver.findElement(By.id("login-button")).click();
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("testpass");
driver.findElement(By.id("submit-button")).click();
// Enter search term and submit form
driver.findElement(By.id("search-field")).sendKeys("test search");
driver.findElement(By.id("search-button")).click();
// Verify that search results page is displayed
String expectedUrl = "https://www.example.com/search";
String actualUrl = driver.getCurrentUrl();
if (!expectedUrl.equals(actualUrl)) {
System.out.println("Error: expected URL " + expectedUrl + " but got " + actualUrl);
}
// Verify that search results contain search term
String expectedResult = "test search";
String actualResult = driver.findElement(By.className("search-result")).getText();
if (!actualResult.contains(expectedResult)) {
System.out.println("Error: expected search result to contain " + expectedResult + " but got " +
actualResult);
}
// Close web browser
driver.quit();
}
}
This code will perform the steps of the test case using Selenium commands to interact with the website
and verify the expected results. You may need to adjust the element selectors and other details to
match your specific website.
Test Case: Verify Product Details Page Functionality
Preconditions:

The web browser is open and the website is loaded

The user is logged in

A product has been selected from the search results
Steps:
1. Click on the product to navigate to the product details page
2. Verify that the product name, image, and price are displayed on the page
3. Click on the add to cart button
4. Verify that the product is added to the cart and the cart count is updated
Expected Result:

The product details page is displayed correctly and the product can be added to the cart
To implement this test case using Selenium and Java, you would need to write code to perform the
following actions:
1. Find the element for the product and click on it to navigate to the product details page
2. Find the elements that display the product name, image, and price and verify that they are
displayed correctly
3. Find the add to cart button and click on it
4. Verify that the product is added to the cart by finding the element that displays the cart count
and verifying that it is updated
Here is an example of how this test case could be implemented using Selenium and Java:
Example code (Needs completion)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ProductDetailsTest {
public static void main(String[] args) {
// Set up WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to website and log in
driver.get("https://www.example.com/");
driver.findElement(By.id("login-button")).click();
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("testpass");
driver.findElement(By.id("submit-button")).click();
// Perform search and select product
driver.findElement(By.id("search-field")).sendKeys("test product");
driver.findElement(By.id("search-button")).click();
driver.findElement(By.className("search-result")).click();
// Verify that product details page is displayed
String expectedUrl = "https://www.example.com/product";
String actualUrl = driver.getCurrentUrl();
if (!expectedUrl.equals(actualUrl)) {
System.out.println("Error: expected URL " + expectedUrl + " but got " + actualUrl);
}
// Verify that product name, image, and price are displayed
String expectedName = "Test Product";
String actualName = driver.findElement(By.id("product-name")).getText();
if (!expectedName.equals(actualName)) {
System.out.println("Error: expected name " + expectedName + " but got " + actualName);
}
if (!driver.findElement(By.id("product-image")).isDisplayed()) {
System.out.println("Error: product image is not displayed");
}
String expectedPrice = "$10.00";
String actualPrice = driver.findElement(By.id("product-price")).getText();
if (!expectedPrice.equals(actualPrice)) {
System.out.println("Error: expected price " + expectedPrice + " but got " + actualPrice);
}
// Click on add to cart button and verify that cart count is updated
int initialCartCount = Integer.parseInt(driver.findElement(By.id("cart-count")).getText());
driver.findElement(By.id("add-to-cart-button")).click();
int finalCartCount = Integer.parseInt(driver.findElement(By.id("cart-count")).getText());
if (finalCartCount != initialCartCount + 1) {
System.out.println("Error: expected cart count to increase by 1 but got " + (finalCartCount initialCartCount));
}
Example code 2
// Import Selenium libraries and set up WebDriver
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ProductDetailsTest {
public static void main(String[] args) {
// Set up WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to website and log in
driver.get("https://www.example.com/");
driver.findElement(By.id("login-button")).click();
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("testpass");
driver.findElement(By.id("submit-button")).click();
// Perform search and select product
driver.findElement(By.id("search-field")).sendKeys("test product");
driver.findElement(By.id("search-button")).click();
driver.findElement(By.className("search-result")).click();
// Verify that product details page is displayed
String expectedUrl = "https://www.example.com/product";
String actualUrl = driver.getCurrentUrl();
if (!expectedUrl.equals(actualUrl)) {
System.out.println("Error: expected URL " + expectedUrl + " but got " + actualUrl);
}
// Verify that product name, image, and price are displayed
String expectedName = "Test Product";
String actualName = driver.findElement(By.id("product-name")).getText();
if (!expectedName.equals(actualName)) {
System.out.println("Error: expected name " + expectedName + " but got " + actualName);
}
if (!driver.findElement(By.id("product-image")).isDisplayed()) {
System.out.println("Error: product image is not displayed");
}
String expectedPrice = "$50.00";
String actualPrice = driver.findElement(By.id("product-price")).getText();
if (!expectedPrice.equals(actualPrice)) {
System.out.println("Error: expected price " + expectedPrice + " but got " + actualPrice);
}
// Click on add to cart button
int initialCount = Integer.parseInt(driver.findElement(By.id("cart-count")).getText());
driver.findElement(By.id("add-to-cart-button")).click();
// Verify that product is added to cart and cart count is updated
int finalCount = Integer.parseInt(driver.findElement(By.id("cart-count")).getText());
if (finalCount != initialCount + 1) {
System.out.println("Error: expected cart count to be " + (initialCount + 1) + " but got " + finalCount);
}
// Close web browser
driver.quit();
}
}
Download