Sta - Final Lab Manual
Sta - Final Lab Manual
AIM
To Develop the test plan for testing an e-commerce web/mobile application (www.amazon.com)
PROCEDURE
1. Test Objectives:
• Verify that users can browse and search for products.
• Confirm that products are displayed accurately with correct details.
• Test the user registration and login process.
• Validate the shopping cart and checkout process.
• Test payment processing and order placement.
• Check for responsiveness and usability on mobile devices.
• Verify security aspects such as data encryption and session management.
2. Test Environment:
• Web Browsers: Chrome, Firefox, Safari, Edge.
• Mobile Devices: iOS and Android devices.
• Testing Framework: Selenium WebDriver with Java.
• Load Testing: Apache JMeter for performance testing.
• Security Testing: OWASP ZAP or similar tool.
3. Test Cases:
• Test cases should cover registration, login, product browsing, cart management, payment
processing, etc.
• Include positive and negative scenarios, edge cases, and stress tests.
4. Test Execution:
• Automate test cases using Selenium WebDriver.
• Execute tests across different browsers and mobile devices.
• Use load testing tools to simulate high user traffic and monitor performance.
• Perform security testing to identify vulnerabilities.
5. Test Data:
• Use real and dummy data for testing.
• Test accounts, products, payment methods, addresses, etc.
6. Risks and Assumptions:
• Risk: Changes in the website's structure might break automation scripts.
• Risk: Mobile app updates might affect test scripts and compatibility.
• Assumption: Test data in the environment matches real-world scenarios.
1
7. Deliverables:
• Test reports summarizing test results, defects found, and performance analysis.
• Documentation of test cases, procedures, and configurations.
• Recommendations for improvements and fixes.
PROGRAM
Test Plan: User Registration
Test Cases:
• TC-UR-01: Verify successful registration with valid information.
• TC-UR-02: Verify registration failure with invalid email format.
• TC-UR-03: Verify registration failure with existing email.
• TC-UR-04: Verify appropriate error messages for failed registration attempts.
Selenium WebDriver Test Case Code: User Registration
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
OUTPUT
User registration successful
3
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AmazonUserLoginTest {
4
}
}
OUTPUT
User Login Successful
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
5
WebElement firstProductPrice = driver.findElement(By.cssSelector(".product-item:first-child
.product-price"));
OUTPUT
Product listing page accessed successfully
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
6
driver.get("https://www.amazon.com/products");
// Step 2: Select a product from the list
WebElement product = driver.findElement(By.cssSelector(".product-item:first-child"));
product.click();
// Step 3: Verify product details
WebElement productName = driver.findElement(By.cssSelector(".product-name"));
WebElement productPrice = driver.findElement(By.cssSelector(".product-price"));
WebElement productDescription = driver.findElement(By.cssSelector(".product-description"));
// Step 4: Verify product images and thumbnails
WebElement productImage = driver.findElement(By.cssSelector(".product-image"));
productImage.click(); // Clicking on the image to view full size
// Step 5: Verify navigation back to product listing
driver.navigate().back();
OUTPUT
Navigated to Amazon product listing page
Product selected from the list
Product details verified
Product images and thumbnails verified
Navigated back to product listing
TEST PLAN: Shopping Cart
TEST CASE:
• TC-SC-01: Verify addition of products to the shopping cart.
• TC-SC-02: Verify accurate display of product details and quantities in the cart.
• TC-SC-03: Verify user can update quantities of products in the cart.
• TC-SC-04: Verify user can remove products from the cart.
Selenium WebDriver Test Case Code: Shopping Cart
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
7
public static void main(String[] args) {
// Set up the WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Step 1: Navigate to Amazon product listing page
driver.get("https://www.amazon.com/products");
// Step 4: Verify accurate display of product details and quantities in the cart
WebElement productNameInCart = driver.findElement(By.cssSelector(".cart-item:first-child
.product-name"));
WebElement productQuantityInCart = driver.findElement(By.cssSelector(".cart-item:first-child
.product-quantity"));
// Verification based on success or failure is not included due to the complexity of the shopping cart
page.
// Close the browser
driver.quit();
8
}
}
Output
Product added to cart successfully
Product details and quantities in cart verified
Product quantity updated successfully
Product removed from cart successfully
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
9
// Step 3: Click on "Submit Payment"
WebElement submitButton = driver.findElement(By.id("submit-payment"));
submitButton.click();
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
10
// Step 2: Navigate to order history page
driver.get("https://www.amazon.com/order-history");
RESULT:
Thus, the test plan for testing an e-commerce web/mobile application was developed and
executed successfully.
11
EX.NO: 2 DESIGN THE TEST CASE FOR TESTING THE
DATE: E-COMMERCE APPLICATION
AIM
To Develop the test case for testing an e-commerce application.
PROCEDURE
1. Test Objectives:
• Verify that users can browse and search for products.
• Confirm that products are displayed accurately with correct details.
• Test the user registration and login process.
• Validate the shopping cart and checkout process.
• Test payment processing and order placement.
• Check for responsiveness and usability on mobile devices.
• Verify security aspects such as data encryption and session management.
2. Test Environment:
• Web Browsers: Chrome, Firefox, Safari, Edge.
• Mobile Devices: iOS and Android devices.
• Testing Framework: Selenium WebDriver with Java.
• Load Testing: Apache JMeter for performance testing.
• Security Testing: OWASP ZAP or similar tool.
3. Test Cases:
• Test cases should cover registration, login, product browsing, cart management, payment
processing, etc.
• Include positive and negative scenarios, edge cases, and stress tests.
4. Test Execution:
• Automate test cases using Selenium WebDriver.
• Execute tests across different browsers and mobile devices.
• Use load testing tools to simulate high user traffic and monitor performance.
• Perform security testing to identify vulnerabilities.
5. Test Data:
• Use real and dummy data for testing.
• Test accounts, products, payment methods, addresses, etc.
6. Risks and Assumptions:
• Risk: Changes in the website's structure might break automation scripts.
• Risk: Mobile app updates might affect test scripts and compatibility.
• Assumption: Test data in the environment matches real-world scenarios.
12
7. Deliverables:
• Test reports summarizing test results, defects found, and performance analysis.
• Documentation of test cases, procedures, and configurations.
• Recommendations for improvements and fixes.
PROGRAM
Test Plan: User Registration
Test Cases:
• TC-UR-01: Verify successful registration with valid information.
• TC-UR-02: Verify registration failure with invalid email format.
• TC-UR-03: Verify registration failure with existing email.
• TC-UR-04: Verify appropriate error messages for failed registration attempts.
Selenium WebDriver Test Case Code: User Registration
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
OUTPUT
User registration successful
14
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
15
driver.quit();
}
}
OUTPUT
User Login Successful
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AmazonProductDetailsTest {
public static void main(String[] args) {
// Set up the WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Step 1: Navigate to Amazon product listing page
driver.get("https://www.amazon.com/products");
// Step 2: Select a product from the list
WebElement product = driver.findElement(By.cssSelector(".product-item:first-child"));
product.click();
// Step 3: Verify product details
WebElement productName = driver.findElement(By.cssSelector(".product-name"));
WebElement productPrice = driver.findElement(By.cssSelector(".product-price"));
WebElement productDescription = driver.findElement(By.cssSelector(".product-description"));
// Step 4: Verify product images and thumbnails
WebElement productImage = driver.findElement(By.cssSelector(".product-image"));
productImage.click(); // Clicking on the image to view full size
// Step 5: Verify navigation back to product listing
16
driver.navigate().back();
RESULT:
Thus, the test cases for testing an e-commerce application were developed and executed
successfully.
17
EX.NO: 3 TEST THE E-COMMERCE APPLICATION AND REPORT
DATE: THE DEFECTS IN IT
AIM:
To test the e-commerce application and report the defects in it.
PROCEDURE:
Defect 1: Incorrect Product Search
Defect Aim: Verify that the product search functionality returns correct results.
Correct Test Procedure:
• Navigate to the e-commerce website's homepage.
• Enter "Laptop" in the search bar.
• Click the search button.
• Verify that the search results list includes relevant products.
DEFECT OUTPUT
Defect: Search results not displayed.
18
public class ProductSearchTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.example-ecommerce.com");
// Perform the correct search
WebElement searchInput = driver.findElement(By.id("search-input"));
searchInput.sendKeys("Laptop");
WebElement searchButton = driver.findElement(By.id("search-button"));
searchButton.click();
// Verify search results
WebElement searchResults = driver.findElement(By.id("search-results"));
if (searchResults.isDisplayed()) {
System.out.println("Search results displayed.");
} else {
System.out.println("Defect: Search results not displayed.");
}
driver.quit();
}
}
EXPECTED OUTPUT
Search results displayed.
RESULT:
Thus, In the above Program The defected in the e-commerce website is tested and rectified
successfully
19
EX.NO :4 DEVELOP THE TEST PLAN AND DESIGN THE TEST CASES
DATE: FOR AN INVENTORY CONTROL SYSTEM
AIM:
To develop the test plan and design the test cases for an inventory control system
PROCEDURE
Test Plan Scenarios:
Scenario 1: Add New Item
• Launch the Chrome browser.
• Navigate to the inventory control system's homepage.
• Log in as an administrator.
• Click on the "Add New Item" button.
• Fill in the item details and submit the form.
• Verify that the new item is listed in the inventory.
Expected Result: The new item should be added to the inventory and listed correctly.
Scenario 2: Update Item Details
• Launch the Chrome browser.
• Navigate to the inventory control system's homepage.
• Log in as an administrator.
• Search for an existing item by name or ID.
• Click on the item to edit its details.
• Update the item's details and save the changes.
• Verify that the updated details are correctly reflected in the inventory.
Expected Result: The item's details should be updated as per the changes made and displayed accurately
in the inventory.
Scenario 3: Delete Item
• Launch the Chrome browser.
• Navigate to the inventory control system's homepage.
• Log in as an administrator.
• Search for an existing item by name or ID.
• Select the item and click the "Delete" button.
• Confirm the deletion.
• Verify that the item is removed from the inventory.
Expected Result: The item should be successfully deleted from the inventory and no longer listed.
20
Scenario 4: Defect - Incorrect Item Quantity Calculation
• Launch the Chrome browser.
• Navigate to the inventory control system's homepage.
• Log in as an administrator.
• Search for an existing item by name or ID.
• Observe the current quantity of the item.
• Perform transactions (additions/subtractions) to change the quantity.
• Verify that the calculated quantity matches the actual quantity.
Expected Result: A defect should be identified if the calculated quantity does not match the actual
quantity due to incorrect quantity calculation logic.
Defect Report: Defect Aim: Verify the accuracy of item quantity calculation. Procedure:
• Navigate to the inventory control system's homepage.
• Log in as an administrator.
• Search for an existing item by name or ID.
• Observe the current quantity of the item.
• Perform transactions (additions/subtractions) to change the quantity.
• Verify that the calculated quantity does not match the actual quantity. Output: Calculated
quantity is incorrect. Severity: Major Priority: High
Test Case Design: Add New Item
• Test Case Name: Add New Item
• Description: Verify that a new item can be successfully added to the inventory.
• Preconditions: Logged in as an administrator.
• Test Steps:
• Click on the "Add New Item" button.
• Fill in the item details: name, ID, category, quantity, etc.
• Click the "Submit" button.
• Expected Result: The new item should be added to the inventory and listed correctly.
PROGRAM :
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
21
driver.get("https://www.inventory-control-system.com");
driver.quit();
}
}
OUTPUT
No errors encountered. New item "New Product" added to the inventory.
RESULT:
Thus, the test plan and the test case for an inventory control system was developed and executed
successfully.
22
EX.NO: 5 EXECUTE THE TEST CASES AGAINST A CLIENT SERVER OR
DATE: DESKTOP APPLICATION AND IDENTIFY THE DEFECTS
AIM
To Execute the test cases against a client server or desktop application and identify the defects
PROCEDURE:
Test Environment:
• Appium
• Android Emulator or iOS Simulator
• Mobile Application: YourMobileApp.apk (Android) or YourMobileApp.ipa (iOS)
Test Scenarios:
23
Scenario 4: Defect - Slow Response
• Launch the Appium server.
• Set desired capabilities to connect to the mobile device/emulator.
• Automate the login process with valid credentials.
• Introduce a delay in the application's response.
• Verify that the user is not logged in within a reasonable time.
Expected Result: A defect should be identified if the login process takes an unreasonably long time to
complete.
Defect Report: Defect Aim: Verify the responsiveness of the login process. Procedure:
• Launch the Appium server.
• Set desired capabilities to connect to the mobile device/emulator.
• Automate the login process with valid credentials.
• Introduce a delay in the application's response.
• Verify that the user is not logged in within a reasonable time. Output: Login process takes longer
than expected. Severity: Major Priority: High
Test Case Design: Successful Login
• Test Case Name: Successful Login
• Description: Verify that a user can successfully log in with valid credentials.
• Preconditions: Appium server is running; Mobile app installed on emulator/simulator.
• Test Steps:
• Set desired capabilities for the mobile device/emulator.
• Automate the login process with valid credentials.
• Expected Result: The user should be logged in, and the welcome message with the username
should be displayed.
try {
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
// Enter credentials
usernameField.sendKeys("testuser");
passwordField.sendKeys("testpass");
// Click login button
loginButton.click();
// Verify successful login
MobileElement welcomeMessage = driver.findElement(MobileBy.id("welcomeMessage"));
if (welcomeMessage.getText().equals("Welcome, testuser!")) {
System.out.println("Login successful.");
} else {
System.out.println("Defect: Login failed.");
}
driver.quit();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
OUTPUT(EXPECTED)
Login successful.
OUTPUT(DEFECT)
Defect: Login failed.
RESULT:
Thus, the test cases against a client server or desktop application and identification of defects
were done successfully.
25
EX NO: 6 TEST THE PERFORMANCE OF THE E-COMMERCE APPLICATION
DATE:
AIM:
To test the performance of the e-commerce application
PROCEDURE
Test Environment:
• Web browser: Chrome
• Selenium WebDriver with Java
• E-commerce website URL: https://www.example-ecommerce.com
• Page to Test: ProductCatalogPage
Test Scenarios:
Scenario 1: Measure Page Load Time
• Launch the Chrome browser.
• Navigate to the ProductCatalogPage.
• Measure the time it takes for the page to fully load.
Expected Result: The page load time should be measured accurately and within an acceptable range.
Test Case Design: Measure Page Load Time
• Test Case Name: Measure Page Load Time
• Test Case ID: TC-PERF-001
• Description: Measure the page load time of a specific page on the e-commerce application.
• Preconditions: None
• Test Steps:
• Launch the Chrome browser.
• Navigate to the ProductCatalogPage.
• Measure the time it takes for the page to fully load.
• Expected Result: The page load time should be measured accurately and within an acceptable
range.
CODE:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// Wait for an element on the page to indicate page load completion (e.g., a product list)
driver.findElement(By.className("product-list"));
endTime = System.currentTimeMillis();
pageLoadTime = endTime - startTime;
driver.quit();
}
}
OUTPUT:
Page Load Time: <Some Value> milliseconds
RESULT:
Thus, the performance of the e-commerce application was executed successfully.
27
EX NO: 7 AUTOMATE THE TESTING OF E-COMMERCE
DATE: APPLICATION USING SELENIUM
AIM
To automate the testing of an e-commerce application using selenium.
PROCEDURE
• Login Module:
o Verify that a user can log in with valid credentials.
o Verify that an error is displayed when logging in with invalid credentials.
o Verify the "Forgot Password" functionality.
• Product Browsing:
o Verify that products are displayed on the homepage.
o Verify that a user can search for products.
o Verify that a user can filter products based on different criteria.
• Shopping Cart:
o Verify that a user can add products to the shopping cart.
o Verify that the shopping cart displays the correct items and quantities.
o Verify that a user can update the quantity of items in the cart.
• Checkout Process:
o Verify that a user can proceed to checkout.
o Verify that the correct shipping information is displayed.
o Verify that the order total is calculated correctly.
CODE
CODE FOR LOGIN MODULE
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
// Initialize WebDriver
WebDriver driver = new ChromeDriver();
29
System.out.println("Test Case 3 Failed: Login Successful");
}
usernameInput.sendKeys("valid_username");
passwordInput.sendKeys("valid_password");
loginButton.click();
30
WebElement searchInput = driver.findElement(By.id("searchInput"));
searchInput.sendKeys("product_name");
searchInput.sendKeys(Keys.RETURN);
// Wait for search results to load (you might want to use WebDriverWait in a real-world scenario)
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement searchResults = driver.findElement(By.id("searchResults"));
if (searchResults.isDisplayed() && searchResults.getText().contains("product_name")) {
System.out.println("Test Case 5 Passed: Search results are displayed for the product.");
} else {
System.out.println("Test Case 5 Failed: Search results are not displayed for the product.");
}
31
// Add more products to the cart if needed...
// Test Case 8: Verify the correctness of the items and quantities in the shopping cart.
WebElement cartIcon = driver.findElement(By.id("cartIcon"));
cartIcon.click();
WebElement cartItems = driver.findElement(By.id("cartItems"));
if (cartItems.isDisplayed() && cartItems.getText().contains("Product 1")) {
System.out.println("Test Case 8 Passed: Product 1 is in the shopping cart.");
} else {
System.out.println("Test Case 8 Failed: Product 1 is not in the shopping cart.");
}
// Test Case 9: Verify the ability to update the quantity of items in the cart.
WebElement quantityInput = driver.findElement(By.id("quantityInput"));
quantityInput.clear();
quantityInput.sendKeys("2");
WebElement updateButton = driver.findElement(By.id("updateButton"));
updateButton.click();
// Wait for the cart to update (you might want to use WebDriverWait in a real-world scenario)
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Check if the quantity is updated
WebElement updatedQuantity = driver.findElement(By.id("updatedQuantity"));
if (updatedQuantity.getText().equals("2")) {
System.out.println("Test Case 9 Passed: Quantity of Product 1 is updated to 2.");
} else {
System.out.println("Test Case 9 Failed: Quantity of Product 1 is not updated to 2.");
}
// Add more test cases and actions here...
// Close the browser
driver.quit();
}
}
CODE FOR CHECKOUT PROCESS
import org.openqa.selenium.By;
32
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
33
OUTPUT:
Test Case 1 Passed: Valid Username
Test Case 2 Passed: Valid Password
Test Case 3 Passed: User Login successful
Test Case 4 Passed: Products are displayed on the homepage.
Test Case 5 Passed: Search results are displayed for the product
Test Case 9 Passed: Quantity of Product 1 is updated to 2.
Test Case 11 Passed: Shipping information is displayed correctly.
Test Case 12 Passed: Order total is correct.
RESULT
Thus, the automation of an e-commerce application using selenium has been executed
successfully.
34
EX NO: 8 INTEGRATE TESTNG WITH ABOVE TEST AUTOMATION
DATE:
AIM:
To integrate TestNG for testing an e-commerce application with test automation
PROCEDURE:
• Login Module:
o Test Case 1: Verify successful login with valid credentials.
o Test Case 2: Verify error message for login with invalid credentials.
o Test Case 3: Verify the functionality of the "Forgot Password" link.
• Product Browsing:
o Test Case 4: Verify that products are displayed on the homepage.
o Test Case 5: Verify the search functionality.
o Test Case 6: Verify product filtering options.
• Shopping Cart:
o Test Case 7: Verify the addition of products to the shopping cart.
o Test Case 8: Verify the correctness of the items and quantities in the shopping cart.
o Test Case 9: Verify the ability to update the quantity of items in the cart.
• Checkout Process:
o Test Case 10: Verify the ability to proceed to checkout.
o Test Case 11: Verify the display of correct shipping information.
o Test Case 12: Verify the correctness of the order total.
CODE
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
35
runProductBrowsingTests(driver);
runShoppingCartTests(driver);
runCheckoutProcessTests(driver);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
36
WebElement searchResults = driver.findElement(By.id("searchResults"));
if (searchResults.isDisplayed() && searchResults.getText().contains("product_name")) {
System.out.println("Test Case 5 Passed: Search results are displayed for the product.");
} else {
System.out.println("Test Case 5 Failed: Search results are not displayed for the product.");
}
}
// Test Case 9: Verify the ability to update the quantity of items in the cart.
WebElement quantityInput = driver.findElement(By.id("quantityInput"));
quantityInput.clear();
quantityInput.sendKeys("2");
WebElement updateButton = driver.findElement(By.id("updateButton"));
updateButton.click();
// Wait for the cart to update (you might want to use WebDriverWait in a real-world scenario)
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Check if the quantity is updated
WebElement updatedQuantity = driver.findElement(By.id("updatedQuantity"));
37
if (updatedQuantity.getText().equals("2")) {
System.out.println("Test Case 9 Passed: Quantity of Product 1 is updated to 2.");
} else {
System.out.println("Test Case 9 Failed: Quantity of Product 1 is not updated to 2.");
}
}
38
Test Case 5 Passed: Search results are displayed for the product
Test Case 9 Passed: Quantity of Product 1 is updated to 2.
Test Case 11 Passed: Shipping information is displayed correctly.
Test Case 12 Passed: Order total is correct.
RESULT:
Thus, the automation of TestNG for testing an e-commerce application using selenium was
executed successfully.
39
EX NO:9. a BUILD A DATA-DRIVEN FRAMEWORK USING
DATE: SELENIUM AND TESTNG
AIM
To Build a data-driven framework using selenium and TestNG.
PROCEDURE
• Create a CSV file (testdata.csv):
• Create a CSV file with columns "Username" and "Password" containing test data.
• Create a TestNG Test Class (LoginTest.java):
• Use TestNG annotations to define the test methods.
• Use @DataProvider to read test data from the CSV file.
• Configure TestNG XML File (testng.xml):
• Create a TestNG XML file to configure the execution of your test class.
• Run the Test:
• Execute the TestNG XML file to run your data-driven test.
Test Cases:
• Login with valid credentials:
• Login with invalid credentials:
CODE
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
41
Assert.assertTrue(errorMessage.isDisplayed(), "Error message not displayed for user: " +
username);
// Close the browser
driver.quit();
}
@DataProvider(name = "loginData")
public Object[][] readData() {
List<Object[]> testData = new ArrayList<>();
String csvFile = "path/to/testdata.csv";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
testData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
}
Object[][] result = new Object[testData.size()][2];
for (int i = 0; i < testData.size(); i++) {
result[i] = testData.get(i);
}
return result;
}
}
Configure TestNG XML File (testng.xml):
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestSuite">
<test name="DataDrivenTest">
<classes>
<class name="LoginTest"/>
</classes>
</test>
</suite>
42
OUTPUT
Username : John
Password : 123
Login Successful
RESULT:
Thus the data driven framework using selenium and TestNG was executed successfully.
43
EX NO:9. b BUILD PAGE OBJECT MODEL USING SELENIUM AND TESTNG
DATE:
AIM
To build a page object model using Selenium and TestNG.
PROCEDURE
• Create Page Object Classes:
• Create a class for each web page. Include web elements and methods representing the actions on
that page.
• Create a Test Class using TestNG:
• Use TestNG annotations to define the test methods.
• Instantiate the Page Object classes and use them in your test methods.
• Run the Test:
• Execute the TestNG XML file or run the test class directly to run your tests.
CODE
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
loginPage = new LoginPage(driver);
}
@Test
public void testLoginWithValidCredentials() {
loginPage.openLoginPage("url_of_your_application_login_page");
loginPage.enterUsername("valid_username");
loginPage.enterPassword("valid_password");
45
loginPage.clickLoginButton();
@Test
public void testLoginWithInvalidCredentials() {
loginPage.openLoginPage("url_of_your_application_login_page");
loginPage.enterUsername("invalid_username");
loginPage.enterPassword("invalid_password");
loginPage.clickLoginButton();
@AfterMethod
public void tearDown() {
driver.quit();
}
}
OUTPUT :
User Login Successful
RESULT
Thus, the page object model using selenium and TestNG has been built successfully.
46
EX NO:9.c BUILD A BDD FRAMEWORK USING SELENIUM,
DATE: TESTNG AND CUCUMBER
AIM
To build a BDD framework using selenium,testng and cucumber.
PROCEDURE:
1. Create a Maven Project:
• Use your IDE (Eclipse, IntelliJ, etc.) to create a new Maven project.
2. Add Dependencies:
• Add dependencies for Selenium, TestNG, and Cucumber in the `pom.xml` file.
3. Create Page Object Classes:
• Create a `pages` package and implement the `LoginPage` class for the login page.
4. Create Feature Files:
• Create a `resources` directory and a `features` sub-directory.
• Write Gherkin-style scenarios in a `login.feature` file.
5. Create Step Definition Classes:
• Create a `step_definitions` directory.
• Implement the `StepDefinitions` class with step definition methods.
6. Set Up TestNG XML File:
• Create a TestNG XML file (e.g., `testng.xml`) and configure the Cucumber runner class.
7. Write Selenium Code in Step Definitions:
• Use Selenium code in the `StepDefinitions` class to interact with web elements.
• Utilize methods from the `LoginPage` class.
8. Run the Tests:
• Run the TestNG XML file to execute the Cucumber tests.
9. Adjust Paths and Locators:
• Adapt paths, element locators, and assertions based on your application's structure.
10. Enhance the Framework:
• Add features like reporting, parallel execution, and additional steps.
• Install Cucumber plugins for your IDE to aid in feature file writing.
47
CODE
Dependencies (Maven):
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
48
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void openLoginPage(String url) {
driver.get(url);
}
public void enterUsername(String username) {
WebElement usernameField = driver.findElement(By.id("username"));
usernameField.sendKeys(username);
}
public void enterPassword(String password) {
WebElement passwordField = driver.findElement(By.id("password"));
passwordField.sendKeys(password);
}
49
Then User should see an error message
StepDefinitions.java (Cucumber Step Definitions):
package step_definitions;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import pages.LoginPage;
50
@Then("User should be logged in successfully")
public void user_should_be_logged_in_successfully() {
// Add assertions based on your application behavior
// For example, assert that a welcome message is displayed
}
@After
public void tearDown() {
driver.quit();
}
}
RUNNING THE TESTS
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestSuite">
<test name="CucumberTests">
<classes>
<class name="io.cucumber.testng.CucumberRunner"/>
</classes>
</test>
</suite>
RESULT
Thus, the BDD framework with Selenium,TestNG and Cucumber was executed successfully
51