Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BN 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

1) HOME NAVIGATION :

TEST 1.1 : Clicking on the Luma logo redirects to the homepage

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MagentoHomePageTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website


driver.get("https://magento.softwaretestingboard.com/");

try {
// Find the Luma logo element by its XPath or CSS selector
WebElement lumaLogo = driver.findElement(By.cssSelector(".logo"));

// Click on the Luma logo


lumaLogo.click();

// Sleep for a few seconds to see the redirection (this is just for
demonstration)
Thread.sleep(3000);

// Get the current URL to verify if it redirected to the homepage


String currentUrl = driver.getCurrentUrl();
System.out.println("Current URL after clicking on Luma logo: " +
currentUrl);

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 1.2 : Navigate to "What's New"

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MagentoWhatsNewTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website


driver.get("https://magento.softwaretestingboard.com/");

try {
// Click on the "What's New" link to navigate to the section
WebElement whatsNewLink = driver.findElement(By.linkText("What's
New"));
whatsNewLink.click();

// Sleep for a few seconds to see the navigation (optional)


Thread.sleep(3000);

// Get the current URL to verify if it redirected to the "What's


New" section
String currentUrl = driver.getCurrentUrl();
System.out.println("Current URL after navigating to 'What's New': "
+ currentUrl);

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 1.3 : Navigate to "Men"

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MagentoMenSectionTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website


driver.get("https://magento.softwaretestingboard.com/");

try {
// Click on the "Men" link to navigate to the section
WebElement menLink = driver.findElement(By.linkText("Men"));
menLink.click();

// Sleep for a few seconds to see the navigation (optional)


Thread.sleep(3000);

// Get the current URL to verify if it redirected to the "Men"


section
String currentUrl = driver.getCurrentUrl();
System.out.println("Current URL after navigating to 'Men': " +
currentUrl);

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 1.4 : Navigate to "Sale"

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MagentoSaleSectionTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website


driver.get("https://magento.softwaretestingboard.com/");

try {
// Click on the "Sale" link to navigate to the section
WebElement saleLink = driver.findElement(By.linkText("Sale"));
saleLink.click();

// Sleep for a few seconds to see the navigation (optional)


Thread.sleep(3000);

// Get the current URL to verify if it redirected to the "Sale"


section
String currentUrl = driver.getCurrentUrl();
System.out.println("Current URL after navigating to 'Sale': " +
currentUrl);

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 1.5 : Test breadcrumbs functionality

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MagentoBreadcrumbsTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website


driver.get("https://magento.softwaretestingboard.com/");

try {
// Navigate to the "Men" section
WebElement menLink = driver.findElement(By.linkText("Men"));
menLink.click();
Thread.sleep(3000);

// Verify breadcrumbs for the "Men" section


WebElement menBreadcrumb =
driver.findElement(By.xpath("//div[@class='breadcrumbs']//li[last()]"));
System.out.println("Breadcrumb for 'Men' section: " +
menBreadcrumb.getText());

// Navigate to the "Women" section


WebElement womenLink = driver.findElement(By.linkText("Women"));
womenLink.click();
Thread.sleep(3000);

// Verify breadcrumbs for the "Women" section


WebElement womenBreadcrumb =
driver.findElement(By.xpath("//div[@class='breadcrumbs']//li[last()]"));
System.out.println("Breadcrumb for 'Women' section: " +
womenBreadcrumb.getText());

// Navigate to the "Sale" section


WebElement saleLink = driver.findElement(By.linkText("Sale"));
saleLink.click();
Thread.sleep(3000);

// Verify breadcrumbs for the "Sale" section


WebElement saleBreadcrumb =
driver.findElement(By.xpath("//div[@class='breadcrumbs']//li[last()]"));
System.out.println("Breadcrumb for 'Sale' section: " +
saleBreadcrumb.getText());

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
2) PRODUCT BROWSING :
TEST 2.1 : Verify product categories on Shop page

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;

public class MagentoShopPageTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website Shop page


driver.get("https://magento.softwaretestingboard.com/shop/");

try {
// Find all elements representing product categories
List<WebElement> categoryElements =
driver.findElements(By.cssSelector(".category"));

// Verify if product categories are present


if (categoryElements.size() > 0) {
System.out.println("Product categories found on the Shop
page:");
for (WebElement categoryElement : categoryElements) {
System.out.println("- " + categoryElement.getText());
}
} else {
System.out.println("No product categories found on the Shop
page.");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 2.2 : Test filtering and sorting options

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.List;

public class MagentoFilterAndSortTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website Shop page


driver.get("https://magento.softwaretestingboard.com/shop/");

try {
// Apply filter by category (e.g., "Men")
Select categoryDropdown = new
Select(driver.findElement(By.id("category")));
categoryDropdown.selectByVisibleText("Men");
Thread.sleep(3000); // Wait for products to filter

// Apply sort by price (e.g., "Price: Low to High")


Select sortDropdown = new Select(driver.findElement(By.id("sort")));
sortDropdown.selectByVisibleText("Price: Low to High");
Thread.sleep(3000); // Wait for products to sort

// Get the list of displayed products


List<WebElement> products =
driver.findElements(By.cssSelector(".product-item"));

// Verify if products are displayed


if (products.size() > 0) {
System.out.println("Filtered and sorted products:");
for (WebElement product : products) {
System.out.println("- " +
product.findElement(By.cssSelector(".product-name")).getText());
}
} else {
System.out.println("No products found after filtering and
sorting.");
}

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 2.3 : Clicking on a product thumbnail redirects to product details page

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MagentoProductDetailsTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website Shop page


driver.get("https://magento.softwaretestingboard.com/shop/");

try {
// Find a product thumbnail (e.g., the first one)
WebElement productThumbnail =
driver.findElement(By.cssSelector(".product-item .product-image"));

// Get the product name from the thumbnail


String productName = productThumbnail.getAttribute("alt");

// Click on the product thumbnail to navigate to the product details


page
productThumbnail.click();
Thread.sleep(3000); // Wait for the product details page to load

// Verify if the correct product details page is loaded


WebElement productDetailsTitle =
driver.findElement(By.cssSelector(".product-name"));

if (productDetailsTitle.getText().equals(productName)) {
System.out.println("Clicked on product thumbnail successfully
redirects to product details page for: " + productName);
} else {
System.out.println("Failed to redirect to correct product
details page.");
}

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 3 : Verify product details display

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MagentoProductDetailsDisplayTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website Shop page


driver.get("https://magento.softwaretestingboard.com/shop/");

try {
// Find a product thumbnail (e.g., the first one)
WebElement productThumbnail =
driver.findElement(By.cssSelector(".product-item .product-image"));

// Click on the product thumbnail to navigate to the product details


page
productThumbnail.click();
Thread.sleep(3000); // Wait for the product details page to load

// Verify product details


WebElement productNameElement =
driver.findElement(By.cssSelector(".product-name"));
WebElement productPriceElement =
driver.findElement(By.cssSelector(".price"));
WebElement productDescriptionElement =
driver.findElement(By.cssSelector(".description"));

String productName = productNameElement.getText();


String productPrice = productPriceElement.getText();
String productDescription = productDescriptionElement.getText();

System.out.println("Product Name: " + productName);


System.out.println("Product Price: " + productPrice);
System.out.println("Product Description: " + productDescription);

// Verify if product details are displayed correctly


if (!productName.isEmpty() && !productPrice.isEmpty() && !
productDescription.isEmpty()) {
System.out.println("Product details display correctly on the
product details page.");
} else {
System.out.println("Product details display incorrectly or are
missing on the product details page.");
}

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 4 : Test adding multiple products to cart

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

import java.util.List;

public class MagentoAddMultipleProductsToCartTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website Shop page


driver.get("https://magento.softwaretestingboard.com/shop/");

WebDriverWait wait = new WebDriverWait(driver, 10);

try {
// Find and click on the first product thumbnail to add it to the
cart
WebElement firstProductThumbnail =
driver.findElement(By.cssSelector(".product-item .product-image"));
firstProductThumbnail.click();

// Wait for the product details page to load

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".produc
t-name")));

// Add the first product to the cart


WebElement addToCartButton =
driver.findElement(By.cssSelector(".add-to-cart-buttons button"));
addToCartButton.click();

// Wait for the "Continue shopping" link to appear

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".succes
s-msg a")));

// Navigate back to the Shop page


driver.navigate().back();
// Find and click on the second product thumbnail to add it to the
cart
List<WebElement> productThumbnails =
driver.findElements(By.cssSelector(".product-item .product-image"));
productThumbnails.get(1).click();

// Wait for the product details page to load

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".produc
t-name")));

// Add the second product to the cart


addToCartButton = driver.findElement(By.cssSelector(".add-to-cart-
buttons button"));
addToCartButton.click();

// Wait for the "Continue shopping" link to appear

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".succes
s-msg a")));

// Verify if both products are added to the cart


WebElement cartQuantity =
driver.findElement(By.cssSelector(".minicart-quantity"));

if (cartQuantity.getText().equals("2")) {
System.out.println("Both products are successfully added to the
cart.");
} else {
System.out.println("Failed to add both products to the cart.");
}

} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 5 : Test proceeding to checkout

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

public class MagentoProceedToCheckoutTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website Shop page


driver.get("https://magento.softwaretestingboard.com/shop/");
WebDriverWait wait = new WebDriverWait(driver, 10);

try {
// Find and click on the first product thumbnail to add it to the
cart
WebElement firstProductThumbnail =
driver.findElement(By.cssSelector(".product-item .product-image"));
firstProductThumbnail.click();

// Wait for the product details page to load

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".produc
t-name")));

// Add the first product to the cart


WebElement addToCartButton =
driver.findElement(By.cssSelector(".add-to-cart-buttons button"));
addToCartButton.click();

// Wait for the "Continue shopping" link to appear

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".succes
s-msg a")));

// Navigate to the cart page


WebElement continueShoppingLink =
driver.findElement(By.cssSelector(".success-msg a"));
continueShoppingLink.click();

// Wait for the cart page to load

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".cart-
container")));

// Proceed to checkout
WebElement proceedToCheckoutButton =
driver.findElement(By.cssSelector(".cart-totals button.checkout-button"));
proceedToCheckoutButton.click();

// Wait for the checkout page to load

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".checko
ut-title")));

// Verify if the checkout page is loaded


WebElement checkoutTitle =
driver.findElement(By.cssSelector(".checkout-title"));

if (checkoutTitle.getText().equals("Checkout")) {
System.out.println("Proceeded to checkout successfully.");
} else {
System.out.println("Failed to proceed to checkout.");
}

} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 6 : Test user registration


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

public class MagentoUserRegistrationTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to the Magento website registration page


driver.get("https://magento.softwaretestingboard.com/customer/account/
create/");

WebDriverWait wait = new WebDriverWait(driver, 10);

try {
// Fill out the registration form
driver.findElement(By.id("firstname")).sendKeys("John");
driver.findElement(By.id("lastname")).sendKeys("Doe");

driver.findElement(By.id("email_address")).sendKeys("johndoe@example.com");
driver.findElement(By.id("password")).sendKeys("Test@123");
driver.findElement(By.id("confirmation")).sendKeys("Test@123");

// Click on the Register button to submit the registration form


driver.findElement(By.cssSelector(".actions-toolbar
button")).click();

// Wait for the registration success message or error message to


appear
WebElement successMessage =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".succes
s-msg")));
WebElement errorMessage = driver.findElement(By.cssSelector(".error-
msg"));

// Verify if the registration is successful


if (successMessage.isDisplayed()) {
System.out.println("User registration successful.");
} else if (errorMessage.isDisplayed()) {
System.out.println("User registration failed. Error message: " +
errorMessage.getText());
}

} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 7 : Test website's responsiveness on various devices


import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.HashMap;
import java.util.Map;

public class MagentoResponsivenessTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Define device dimensions or user-agent strings for different devices


Map<String, Dimension> deviceDimensions = new HashMap<>();
deviceDimensions.put("Desktop", new Dimension(1920, 1080));
deviceDimensions.put("Tablet", new Dimension(768, 1024));
deviceDimensions.put("Mobile", new Dimension(375, 667));

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

try {
// Navigate to the Magento website
driver.get("https://magento.softwaretestingboard.com/");

// Test responsiveness by resizing the browser window


for (Map.Entry<String, Dimension> entry :
deviceDimensions.entrySet()) {
String device = entry.getKey();
Dimension dimension = entry.getValue();

// Resize the browser window to simulate different devices


driver.manage().window().setSize(dimension);

System.out.println("Testing responsiveness for " + device + "


(Width: " + dimension.getWidth() + ", Height: " + dimension.getHeight() + ")");

// Add assertions or further tests here


// For example, you can check if specific elements are visible
or hidden at different screen sizes
}

} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 8 : Access website on Firefox

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MagentoFirefoxTest {


public static void main(String[] args) {
// Set the path of the geckodriver (Firefox driver)
System.setProperty("webdriver.gecko.driver", "C:\\browserdriver\\
geckodriver.exe");

// Create a new instance of the Firefox driver


WebDriver driver = new FirefoxDriver();

try {
// Navigate to the Magento website
driver.get("https://magento.softwaretestingboard.com/");

// Print the title of the website


System.out.println("Title of the website is: " + driver.getTitle());

} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 9 : Test error handling scenarios

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

public class MagentoErrorHandlingTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

WebDriverWait wait = new WebDriverWait(driver, 10);

try {
// Navigate to the Magento website login page

driver.get("https://magento.softwaretestingboard.com/customer/account/login/");

// Click on the "Login" button without entering any credentials


WebElement loginButton = driver.findElement(By.cssSelector(".login
button"));
loginButton.click();

// Wait for the error message to appear


WebElement errorMessage =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".messag
e-error")));

// Verify if the correct error message is displayed


if (errorMessage.getText().equals("Invalid login or password.")) {
System.out.println("Error handling for empty credentials test
passed.");
} else {
System.out.println("Error handling for empty credentials test
failed. Actual error message: " + errorMessage.getText());
}

// Enter invalid email and password


driver.findElement(By.id("email")).sendKeys("invalid_email@example.com");
driver.findElement(By.id("pass")).sendKeys("invalidpassword");
loginButton.click();

// Wait for the error message to appear


errorMessage =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".messag
e-error")));

// Verify if the correct error message is displayed


if (errorMessage.getText().equals("Invalid login or password.")) {
System.out.println("Error handling for invalid credentials test
passed.");
} else {
System.out.println("Error handling for invalid credentials test
failed. Actual error message: " + errorMessage.getText());
}

} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

TEST 10 : Test website's accessibility using keyboard navigation

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;
import org.openqa.selenium.interactions.Actions;

import java.util.List;

public class MagentoAccessibilityTest {


public static void main(String[] args) {
// Set the path of the chromedriver
System.setProperty("webdriver.chrome.driver", "C:\\browserdriver\\
chromedriver.exe");

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

Actions actions = new Actions(driver);

try {
// Navigate to the Magento website
driver.get("https://magento.softwaretestingboard.com/");

// Get all interactive elements on the page


List<WebElement> interactiveElements =
driver.findElements(By.xpath("//*[@tabindex or @role]"));

for (int i = 0; i < interactiveElements.size(); i++) {


WebElement element = interactiveElements.get(i);

// Focus on the element using keyboard navigation


actions.moveToElement(element).click().perform();
// Check if the element has been focused
WebElement focusedElement = driver.switchTo().activeElement();

if (!element.equals(focusedElement)) {
System.out.println("Failed to focus on element: " +
element.getTagName());
}

// Navigate to the next element using keyboard tab


actions.sendKeys(Keys.TAB).perform();
}

System.out.println("Accessibility test completed.");

} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}

You might also like