DEV Community

Big Show
Big Show

Posted on

Q3

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.time.Duration;
import org.apache.commons.io.FileUtils;
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.Assert;
import org.testng.annotations.*;

public class LibraryTest {

WebDriver driver;
Workbook wb;
Sheet sheet;

@BeforeClass
public void beforeClass() throws Exception {

    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(Duration.ofSeconds(5));

    FileInputStream fis = new FileInputStream(
            "/home/pg_user/Desktop/Additional Supplied Files/Data.xls");

    wb = new HSSFWorkbook(fis);
    sheet = wb.getSheet("Details");
}

@Test(priority = 1)
public void test1() throws Exception {

    driver.findElement(By.linkText("REGISTER NOW")).click();

    for (int i = 1; i <= sheet.getLastRowNum(); i++) {

        Row row = sheet.getRow(i);

        driver.findElement(By.name("name")).clear();
        driver.findElement(By.name("name"))
                .sendKeys(row.getCell(0).toString());

        driver.findElement(By.name("age")).clear();
        driver.findElement(By.name("age"))
                .sendKeys(row.getCell(1).toString());

        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email"))
                .sendKeys(row.getCell(2).toString());

        driver.findElement(By.name("phone")).clear();
        driver.findElement(By.name("phone"))
                .sendKeys(row.getCell(3).toString());

        driver.findElement(By.id("register")).click();

        String msg = driver.findElement(By.id("message")).getText();

        Cell cell = row.createCell(4);
        cell.setCellValue(msg);

        File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(src,
                new File("/home/pg_user/Desktop/Additional Supplied Files/Screenshots/"
                        + row.getCell(0).toString() + ".png"));

        driver.findElement(By.id("reset")).click();
    }

    FileOutputStream fos = new FileOutputStream(
            "/home/pg_user/Desktop/Additional Supplied Files/Data.xls");
    wb.write(fos);

    driver.findElement(By.linkText("SIGN IN")).click();
    driver.findElement(By.name("username")).sendKeys("librarian");
    driver.findElement(By.name("password")).sendKeys("infy123");
    driver.findElement(By.id("login")).click();
}

@DataProvider(name = "bookDetails")
public Object[][] bookDetails() {

    return new Object[][] {

            { "Lord of the Rings", "000747766" },

            { "The Alchemist", "1112234988" }

    };
}

@Test(priority = 2, dataProvider = "bookDetails")
public void test2(String book, String isbn) {

    driver.findElement(By.linkText("View Books")).click();

    Select s = new Select(driver.findElement(By.name("category")));
    s.selectByVisibleText("Fiction");

    java.util.List<WebElement> rows =
            driver.findElements(By.xpath("//table/tbody/tr"));

    boolean found = false;

    for (WebElement r : rows) {

        String b = r.findElement(By.xpath("td[1]")).getText();
        String i = r.findElement(By.xpath("td[2]")).getText();

        if (b.equals(book) && i.equals(isbn)) {

            System.out.println(
                    r.findElement(By.xpath("td[3]")).getText());

            found = true;
            break;
        }
    }

    if (!found) {
        System.out.println("Book is not available");
    }

    driver.navigate().refresh();
}

@Test(priority = 3)
public void test3() {

    driver.findElement(By.linkText("Add a Book")).click();

    Select s = new Select(driver.findElement(By.name("type")));
    s.selectByVisibleText("Non Fiction");

    driver.findElement(By.name("title"))
            .sendKeys("What Happened");

    driver.findElement(By.name("isbn"))
            .sendKeys("122228-99");

    driver.findElement(By.name("author"))
            .sendKeys("Hillary");

    driver.findElement(By.id("addBook")).click();

    Alert alert = driver.switchTo().alert();

    String text = alert.getText();

    try {
        Assert.assertEquals(text, "Book successfully added");
    } catch (AssertionError e) {
        System.out.println(e.getMessage());
    }

    alert.accept();
}

@AfterClass
public void afterClass() {

    driver.quit();
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)