MUSIC
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.testng.Assert; // Using TestNG's Assert
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class AutomationScenarioTest {
private WebDriver driver;
private String originalWindowHandle;
/**
- Helper method to read data from the .xls file
-
Assumes data is in the first sheet, on the second row (index 1)
*/
private static Map readExcelData(String filePath) throws IOException {
Map data = new HashMap<>();
FileInputStream fis = new FileInputStream(filePath);// Use HSSFWorkbook for .xls files
Workbook workbook = new HSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0); // Assuming first sheet// DataFormatter handles different cell types
DataFormatter formatter = new DataFormatter();// Assuming headers are in Row 0 and data is in Row 1
Row dataRow = sheet.getRow(1);// Assuming Column 0 = Contact, Column 1 = Email, Column 2 = Tickets
String contact = formatter.formatCellValue(dataRow.getCell(0));
String email = formatter.formatCellValue(dataRow.getCell(1));
String tickets = formatter.formatCellValue(dataRow.getCell(2));data.put("contact", contact);
data.put("email", email);
data.put("tickets", tickets);workbook.close();
fis.close();return data;
}
@BeforeTest
public void setup() {
// --- Step 1: Use appropriate Setup method to: ---
// Set the driver property System.setProperty("webdriver.gecko.driver", "/home/user/Desktop/geckodriver"); // Initialize Firefox driver driver = new FirefoxDriver(); // Set an implicit wait of 12 seconds driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(12)); // Open the application using url driver.get("http://localhost:8080/application/"); // Maximize the browser driver.manage().window().maximize(); // Store the original window handle for later use originalWindowHandle = driver.getWindowHandle(); }
@test
public void executeAutomationScenario() throws Exception {
// --- Step 2: Click on Events link. --- // *ASSUMED LOCATOR* driver.findElement(By.linkText("Events")).click(); // --- Step 3: Select Music check box. --- // *ASSUMED LOCATOR* WebElement musicCheckbox = driver.findElement(By.id("musicCheckbox")); // e.g., //input[@value='Music'] musicCheckbox.click(); // --- Step 4: Using webtable iteration... print all the contents of Events Table --- System.out.println("\n--- Events Table Contents ---"); // *ASSUMED LOCATOR* WebElement eventsTable = driver.findElement(By.id("eventsTable")); // e.g., //table[@name='events'] // Print Headers (th) List<WebElement> headers = eventsTable.findElements(By.tagName("th")); for (WebElement header : headers) { System.out.print(header.getText() + "\t\t"); } System.out.println("\n---------------------------------"); // Print Rows (tr) List<WebElement> rows = eventsTable.findElements(By.tagName("tr")); for (WebElement row : rows) { List<WebElement> cells = row.findElements(By.tagName("td")); for (WebElement cell : cells) { System.out.print(cell.getText() + "\t\t"); } if (!cells.isEmpty()) { System.out.println(); } } System.out.println("--- End of Table ---"); // --- Step 5: Deselect the Music check box. --- musicCheckbox.click(); // --- Step 6: Select the Adventure checkbox and select radio button against to Skydiving Celebration. --- // *ASSUMED LOCATOR* driver.findElement(By.id("adventureCheckbox")).click(); // e.g., //input[@value='Adventure'] // *ASSUMED LOCATOR (XPath)* WebElement skydivingRadio = driver.findElement(By.xpath("//tr[contains(., 'Skydiving Celebration')]//input[@type='radio']")); skydivingRadio.click(); // --- Step 7: Accept the alert box. --- System.out.println("Accepting alert from step 6..."); Alert alert1 = driver.switchTo().alert(); alert1.accept(); // --- Step 8: ...print the URLs of each window and switch to the new tab... --- Thread.sleep(2000); // Wait for new window Set<String> allWindowHandles = driver.getWindowHandles(); String newWindowHandle = null; System.out.println("Printing all window URLs:"); for (String handle : allWindowHandles) { driver.switchTo().window(handle); System.out.println("Window URL: " + driver.getCurrentUrl()); if (!handle.equals(originalWindowHandle)) { newWindowHandle = handle; // Found the new window } } // Switch to the new window to work on it Assert.assertNotNull(newWindowHandle, "New window was not found!"); driver.switchTo().window(newWindowHandle); System.out.println("Switched to new window."); // --- Step 9: Fetch the title of the newly opened window... --- String newTitle = driver.getTitle(); System.out.println("New window title is: " + newTitle); if (newTitle.equals("BookThyEvent: Booking")) { System.out.println("Desired window found"); } else { System.out.println("Desired window not found"); } // TestNG assertion Assert.assertEquals(newTitle, "BookThyEvent: Booking", "Window title mismatch!"); // --- Step 10: Parameterize the fields... --- String excelFilePath = "/home/user/Desktop/user_repo/To_Participant/AdditionalSuppliedFiles/Data.xls"; Map<String, String> testData = readExcelData(excelFilePath); // Fill the form // *ASSUMED LOCATORS* driver.findElement(By.id("contactNumber")).sendKeys(testData.get("contact")); driver.findElement(By.id("emailId")).sendKeys(testData.get("email")); driver.findElement(By.id("numTickets")).sendKeys(testData.get("tickets")); // Click on "Check Valid data" and "Calculate cost" buttons // *ASSUMED LOCATORS* driver.findElement(By.id("checkValidDataButton")).click(); driver.findElement(By.id("calculateCostButton")).click(); // Accept the alert System.out.println("Accepting alert from step 10..."); Alert alert2 = driver.switchTo().alert(); alert2.accept(); // --- Step 11: Switch back to the default window... --- driver.close(); // Close the current (new) window driver.switchTo().window(originalWindowHandle); // Switch back to the original System.out.println("Switched back to original window."); // --- Step 12: Click on Offers link. --- // *ASSUMED LOCATOR* driver.findElement(By.linkText("Offers")).click(); // --- Step 13: Select Debit Card offers option from 'Select Category' drop down. --- // *ASSUMED LOCATOR* WebElement categoryDropdown = driver.findElement(By.id("selectCategory")); Select select = new Select(categoryDropdown); select.selectByVisibleText("Debit Card offers"); // --- Step 14: Click on View Offer button corresponding to "AXIS" Bank. --- // *ASSUMED LOCATOR (XPath)* WebElement viewOfferButton = driver.findElement(By.xpath("//td[text()='AXIS Bank']/following-sibling::td/button[text()='View Offer']")); viewOfferButton.click(); // --- Step 15: Print the alert message in the console and accept the alert. --- System.out.println("Handling final alert..."); Alert alert3 = driver.switchTo().alert(); String alertMsg = alert3.getText(); System.out.println("Final Alert Message: " + alertMsg); alert3.accept(); System.out.println("\n--- Scenario Completed Successfully ---"); }
@AfterTest
public void tearDown() {
// Ensure the browser is closed even if the script fails
if (driver != null) {
driver.quit();
}
}
}