import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Set;
import java.util.List;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.;
import org.apache.commons.io.FileUtils;
public class ApplicationTest {
WebDriver driver;
@BeforeClass
public void beforeClass() {
System.setProperty(
"webdriver.gecko.driver",
"/home/pg_user/Desktop/geckodriver"
);
driver = new FirefoxDriver();
driver.get(
"http://localhost:8080/application/index.html"
);
driver.manage().window().maximize();
driver.manage().timeouts()
.implicitlyWait(
java.time.Duration.ofSeconds(12)
);
}
@Test(priority = 1)
public void test10() {
driver.findElement(
By.linkText("Offers")
).click();
WebElement dropdown = driver.findElement(
By.id("category")
);
Select select = new Select(dropdown);
// Print all dropdown options
List<WebElement> options = select.getOptions();
for (WebElement option : options) {
System.out.println(option.getText());
}
// Select Credit Card Offers
select.selectByVisibleText("Credit Card Offers");
// Click ICICI View Offer button
driver.findElement(
By.xpath(
"//tr[.//td[contains(normalize-space(), 'ICICI')]]" +
"//button[contains(., 'View Offer')]"
)
).click();
// Verify alert message
Alert alert = driver.switchTo().alert();
String actualMessage = alert.getText();
Assert.assertEquals(
actualMessage,
"Now get Rs 100 off on your ICICI Bank Credit Card"
);
alert.accept();
}
@Test(priority = 2)
public void test20() throws IOException {
driver.findElement(
By.linkText("Home")
).click();
// Select Garuda Mall
driver.findElement(
By.xpath(
"//input[@type='radio' and " +
"following-sibling::text()[contains(.,'Garuda Mall')]]"
)
).click();
// Read Movies Now web table
WebElement table = driver.findElement(
By.xpath(
"//table[.//th[contains(.,'Top Movies')]]"
)
);
List<WebElement> rows = table.findElements(
By.xpath(".//tr")
);
for (int i = 1; i < rows.size(); i++) {
List<WebElement> cells = rows.get(i)
.findElements(By.xpath("./th|./td"));
if (cells.size() >= 2) {
System.out.println(
"Top Movie: " + cells.get(0).getText()
+ " | Rating: " + cells.get(1).getText()
);
}
}
// Select BM Habitat Mall
driver.findElement(
By.xpath(
"//input[@type='radio' and " +
"following-sibling::text()[contains(.,'BM Habitat Mall')]]"
)
).click();
// Take screenshot
File source = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
File destination = new File(
"/home/pg_user/Desktop/" +
"AdditionalSuppliedfiles/Screenshots/Image.png"
);
FileUtils.copyFile(source, destination);
}
@Test(priority = 3)
public void test30() throws Exception {
driver.findElement(
By.linkText("Events")
).click();
// Select Music checkbox
driver.findElement(
By.xpath(
"//input[@type='checkbox' and " +
"following-sibling::text()[contains(.,'Music')]]"
)
).click();
// Click radio button adjacent to Sunburn On Air
driver.findElement(
By.xpath(
"//input[@type='radio' and " +
"following-sibling::text()[contains(.,'Sunburn On Air')]]"
)
).click();
// Accept alert if present
try {
driver.switchTo().alert().accept();
} catch (NoAlertPresentException e) {
System.out.println("No alert displayed.");
}
// Identify newly opened booking window
String mainWindow = driver.getWindowHandle();
String bookingWindow = null;
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
System.out.println(
"Window URL: " + driver.getCurrentUrl()
);
if (driver.getTitle().equals(
"Book ThyEvent: Booking")) {
bookingWindow = window;
}
}
if (bookingWindow != null) {
System.out.println("Desired window found");
driver.switchTo().window(bookingWindow);
} else {
System.out.println("Desired window not found");
driver.switchTo().window(mainWindow);
return;
}
// Read Excel worksheet: Details
String excelPath =
"/home/pg_user/Desktop/" +
"Additional Supplied Files/Data.xls";
try (FileInputStream fis =
new FileInputStream(excelPath);
Workbook workbook = WorkbookFactory.create(fis)) {
Sheet sheet = workbook.getSheet("Details");
DataFormatter formatter = new DataFormatter();
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue;
}
// Example: map Excel columns to page fields.
// Replace these locators and column indexes
// with the actual worksheet and form structure.
String name = formatter.formatCellValue(
row.getCell(0)
);
String email = formatter.formatCellValue(
row.getCell(1)
);
String tickets = formatter.formatCellValue(
row.getCell(2)
);
driver.findElement(By.id("name"))
.clear();
driver.findElement(By.id("name"))
.sendKeys(name);
driver.findElement(By.id("email"))
.clear();
driver.findElement(By.id("email"))
.sendKeys(email);
driver.findElement(By.id("tickets"))
.clear();
driver.findElement(By.id("tickets"))
.sendKeys(tickets);
driver.findElement(
By.id("checkValidData")
).click();
driver.findElement(
By.id("calculateCost")
).click();
try {
driver.switchTo().alert().accept();
} catch (NoAlertPresentException e) {
System.out.println(
"No calculation alert displayed."
);
}
}
}
}
@AfterClass
public void afterClass() {
if (driver != null) {
try {
driver.findElement(
By.linkText("Home")
).click();
} catch (Exception e) {
System.out.println(
"Could not navigate to Home."
);
} finally {
driver.quit();
}
}
}
}
Top comments (0)