DEV Community

Big Show
Big Show

Posted on

Music2

public class Submit {
WebDriver driver=null;
@Before
public void setup() {
System.setProperty("webdriver.gecko.driver", "/home/user/Desktop/geckodriver");
driver new FirefoxDriver();
driver.get("http://localhost:8080/application/*); driver.manage().window().maximize();
driver.manage().timeouts().implicitlywait(12, TimeUnit.SECONDS);

@test
public void test() throws IOException {
driver.findElement(By.id("bmhm")).click();
TakesScreenshot src (TakesScreenshot) driver: File srcfile src.getScreenshotAs (OutputType.FILE);
FileUtils.copyFile(srcfile, new File(/home/user/Desktop/user_repo/To_Participant/
driver.findElement(By.id("offersLink")).click();
Select dropdown = new Select(driver.findElement(By.id("offersDropdown"}});
List WebElement> op= dropdown.getOptions();
int size op.size();
for(int i=0; i System.out.println(options); String options op.get(i).getText();
System.out.pritnln(options)
}
driver.findElement(By.id("eventsLink")).click();
driver.findElement(By.id("ent")).click();
WebElement tableDetails = driver.findElement(By.id("eventsTable']); List for (WebElement r:rows) ( List WebElement> columns= driver.findElements By.tagName("td"));

System.out.println("Number of cloumns:columns.size());
}
}

driver.findElement(By.id("book2")).click();
String msg driver.switchTo().alert().getText();
Thread, sleep(1000);
System.out.println(msg);
driver.switchTo().alert().accept();
Set handle driver.getwindowHandles();
for (String s:handle) {
driver.switchTo().window(s);
String url= driver.getCurrentUrl(); System.out.println(driver.getCurrenturl());
}
String title driver.getTitle();
System.out.println("pageTitle:+title);
String path=/home/user/Desktop/user_repo/To_Participant/Additional Supplieds/Book;
FileInputStream fis= new FileInputStream(path);

XSSFWorkbook wb new XSSFWorkbook(); XSSFSheet stwb.getSheet("Sheet1");

int ir st.getLastRowNum()-st.getFirstRowNum() for(int i=1; i<lr; i++) { Thread.sleep(1000);

String contact st.getRow(i).getCell(0).getStringCellvalue(); driver.findElement(By.id("phone")).sendKeys(contact); Thread.sleep(1000);

String email st.getRow(1).getCell(1).getStringCellValue(); driver.findElement(By.id("email")).sendKeys(email); Thread.sleep(1000);

String ticket st.getRow(i).getCell(2).getStringellvalue(); driver.findElement(By.id("ticket")).sendKeys (ticket); Thread.sleep(1000);

driver.findElement(By.id("checkValid")).click(); driver.findElement(By.id("calculateFare")).click();
String msgl driver.switchTo().alert().getText();

Thread.sleep(1000); System.out.println(msgl);

driver.switchTo().alert().accept();

@after

public void tearDown() {

driver.close();
}
}

Top comments (1)

Collapse
 
big_shiw_d828acaaa5932383 profile image
Big Show •

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();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}