import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.;
import org.openqa.selenium.
;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.ITestResult;
import org.testng.annotations.*;
import org.testng.asserts.SoftAssert;

public class Ragged {

WebDriver driver; final String dataPath = "/home/pg_user/Desktop/Additional SuppliedFiles/Data.xls"; final String screenshotPath = "/home/pg_user/Desktop/Additional SuppliedFiles/Screenshots/Image.PNG"; @BeforeClass public void beforeClass() { System.setProperty( "webdriver.gecko.driver", "/home/pg_user/Desktop/geckodriver" ); driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts() .implicitlyWait(Duration.ofSeconds(6)); driver.get( "http://localhost:8080/application/index.html" ); } @Test(priority = 1) public void test10() { driver.findElement( By.xpath("//*[normalize-space()='Offices']") ).click(); List<WebElement> officeRows = driver.findElements( By.xpath("//table//tbody/tr") ); System.out.println( "Number of offices: " + officeRows.size() ); } @Test(priority = 2) public void test20() throws IOException { driver.findElement( By.xpath("//*[normalize-space()='HomeDelivery']") ).click(); SoftAssert softAssert = new SoftAssert(); WebElement buildOrderButton = driver.findElement(By.id("BuildOrder")); WebElement whereDropdown = driver.findElement(By.id("Where")); softAssert.assertFalse( buildOrderButton.isEnabled(), "BuildOrder should be disabled initially" ); softAssert.assertFalse( whereDropdown.isEnabled(), "Where dropdown should be disabled initially" ); // Read Excel data. try (FileInputStream fis = new FileInputStream(dataPath); Workbook workbook = new HSSFWorkbook(fis)) { Sheet sheet = workbook.getSheetAt(0); DataFormatter formatter = new DataFormatter(); Row header = sheet.getRow(0); int buildCol = findColumn( header, formatter, "BuildOrder" ); int whereCol = findColumn( header, formatter, "Where" ); int statusCol = findColumn( header, formatter, "Button status" ); if (statusCol == -1) { statusCol = header.getLastCellNum(); header.createCell(statusCol) .setCellValue("Button status"); } for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) { continue; } String buildOption = formatter.formatCellValue( row.getCell(buildCol) ).trim(); String whereOption = formatter.formatCellValue( row.getCell(whereCol) ).trim(); if (buildOption.isEmpty() || whereOption.isEmpty()) { continue; } // Replace these IDs if the actual HTML differs. Select buildDropdown = new Select( driver.findElement(By.id("BuildOrder")) ); Select whereSelect = new Select( driver.findElement(By.id("Where")) ); buildDropdown.selectByVisibleText(buildOption); whereSelect.selectByVisibleText(whereOption); // Re-find the button after changing selections. WebElement button = driver.findElement( By.id("BuildOrder") ); String status = button.isEnabled() ? "Button is enabled" : "Button is disabled"; System.out.println(status); Cell statusCell = row.getCell(statusCol); if (statusCell == null) { statusCell = row.createCell(statusCol); } statusCell.setCellValue(status); } // Save Button status results into Data.xls. try (FileOutputStream fos = new FileOutputStream(dataPath)) { workbook.write(fos); } } softAssert.assertAll(); } @Test(priority = 3) public void test30() throws IOException { driver.findElement( By.xpath("//*[normalize-space()='PickUp']") ).click(); new Select(driver.findElement(By.id("City"))) .selectByVisibleText("Bengaluru"); new Select(driver.findElement(By.id("Where"))) .selectByVisibleText("Koramangala"); // Update this locator if the contact field has a known ID. driver.findElement( By.xpath("//input[@type='tel' or " + "contains(@id,'contact') or " + "contains(@name,'contact')]") ).sendKeys("9876543210"); // Standard XPath 1.0: click the Coffee label. driver.findElement( By.xpath("//label[contains(normalize-space(.),'Coffee')]") ).click(); // Identify the Name column in the table. List<WebElement> headers = driver.findElements( By.xpath("//table//th") ); int nameColumn = -1; for (int i = 0; i < headers.size(); i++) { if (headers.get(i).getText().trim() .equalsIgnoreCase("Name")) { nameColumn = i + 1; break; } } if (nameColumn == -1) { throw new NoSuchElementException( "Name column was not found" ); } String[] coffeeNames = { "Black Coffee", "Cappuccino", "Espresso" }; for (String coffee : coffeeNames) { List<WebElement> matches = driver.findElements( By.xpath("//table//tbody/tr/td[" + nameColumn + "][normalize-space()='" + coffee + "']") ); if (!matches.isEmpty()) { System.out.println( "Mentioned coffee is in stock" ); } } // Locate the Black Coffee row. WebElement blackCoffeeRow = driver.findElement( By.xpath("//table//tr[td[normalize-space()='Black Coffee']]") ); // Select Small using the ID locator. new Select( blackCoffeeRow.findElement(By.id("cupSize")) ).selectByVisibleText("Small"); // Enter quantity using XPath. blackCoffeeRow.findElement( By.xpath(".//input[@id='quantity' or " + "@name='quantity' or " + "contains(@placeholder,'Quantity')]") ).sendKeys("10"); driver.findElement( By.xpath("//*[self::button or self::input]" + "[normalize-space()='Add to Cart' or " + "@value='Add to Cart']") ).click(); // Choose No for Have a promo code? driver.findElement( By.xpath("//label[contains(normalize-space(.),'No')]") ).click(); driver.findElement( By.xpath("//*[self::button or self::input]" + "[normalize-space()='Place Order' or " + "@value='Place Order']") ).click(); // Take screenshot and save as Image.PNG. File folder = new File( "/home/pg_user/Desktop/Additional SuppliedFiles/Screenshots" ); if (!folder.exists() && !folder.mkdirs()) { throw new IOException( "Could not create screenshot folder" ); } File screenshot = ((TakesScreenshot) driver) .getScreenshotAs(OutputType.FILE); Files.copy( screenshot.toPath(), new File(screenshotPath).toPath(), StandardCopyOption.REPLACE_EXISTING ); System.out.println("Screenshot saved: " + screenshotPath); } private int findColumn( Row header, DataFormatter formatter, String name) { if (header == null) { throw new IllegalStateException( "Excel header row is missing" ); } for (Cell cell : header) { if (formatter.formatCellValue(cell) .trim().equalsIgnoreCase(name)) { return cell.getColumnIndex(); } } if (name.equals("Button status")) { return -1; } throw new IllegalStateException( "Excel column not found: " + name ); } @AfterMethod public void afterMethod(ITestResult result) { // Keep the browser alive for the next test. // Close it after the final test, or after a failure. if (driver != null && (result.getMethod().getMethodName().equals("test30") || !result.isSuccess())) { driver.quit(); driver = null; } } 

}