java - 具有 Excel 输入和输出的自动化测试用例

标签 java excel apache selenium-webdriver automated-tests

我很快就会开始担任软件 QA 分析师的新角色,并且我正在尝试通过使用 Java 和 Selenium WebDriver 开发一些基本的自动化测试脚本来练习我的自动化测试。一般来说,我对自动化和 Java 非常陌生,因此我想以流行的旅游网站的表单为基础进行练习。该脚本需要允许在 Excel 电子表格中输入预先准备好的数据,根据输入数据捕获网站上的某些结果,然后将这些结果导出到同一 Excel 电子表格中的相应单元格。我实际上创建了一个似乎做得相当好的脚本,但是每次运行该脚本时,我都会收到一条错误消息,内容如下:

log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

脚本的其余部分似乎进行得相当顺利,但我真的不确定这个错误指的是什么或如何修复它。我访问了它列出的 URL,但这就像阅读完全外语一样。我已经在谷歌上搜索了很多,但还没有找到任何可以解决此错误消息的特定实例的内容,我希望有人能够帮助我。大部分脚本都是由我在网上找到的示例以一种或另一种形式组装而成的,并且我将其组装起来通常可以实现我想要的功能。我不确定这是否相关,但我的项目加载了所有最新版本的 Selenium WebDriver、Apache POI 和 Junit、外部 JAR 文件。我对 Java、Selenium WebDriver 和自动化都很陌生,所以如果有人有解决方案,特别是这个错误背后的“原因”,我将不胜感激。我的整个代码如下。

package TestCases;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OrbitzDotComBasicFormEntry1 {

    public static void main(String[] args) throws Exception {

        WebDriver wd = new FirefoxDriver();
        String baseURL = "http://www.orbitz.com/";
        wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        InputStream inp = new FileInputStream("/path/to/spreadsheet/Workbook1.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);

        String departLocation;
        String arriveLocation;
        String departureDate;
        String returnDate;

        int rowcount=sheet.getLastRowNum();

        for(int i=1;i<=rowcount;i++){

            Row row = sheet.getRow(i);
            Cell cell = row.getCell(4);
            departLocation=sheet.getRow(i).getCell(0).getStringCellValue();
            arriveLocation=sheet.getRow(i).getCell(1).getStringCellValue();
            departureDate=sheet.getRow(i).getCell(2).getStringCellValue();
            returnDate=sheet.getRow(i).getCell(3).getStringCellValue();

            wd.get(baseURL);

            //Selects the "Flights Only" radio button
            wd.findElement(By.id("search.type.air")).click();
            Thread.sleep(1000);

            //Enter variable text into the "From" field
            wd.findElement(By.name("ar.rt.leaveSlice.orig.key")).click();
            wd.findElement(By.name("ar.rt.leaveSlice.orig.key")).clear();
            wd.findElement(By.name("ar.rt.leaveSlice.orig.key")).sendKeys(departLocation);

            //Enter variable text into the "To" field
            wd.findElement(By.name("ar.rt.leaveSlice.dest.key")).click();
            wd.findElement(By.name("ar.rt.leaveSlice.dest.key")).clear();
            wd.findElement(By.name("ar.rt.leaveSlice.dest.key")).sendKeys(arriveLocation);

            //Enter variable text into the "Leave" field
            wd.findElement(By.name("ar.rt.leaveSlice.date")).click();
            wd.findElement(By.name("ar.rt.leaveSlice.date")).clear();
            wd.findElement(By.name("ar.rt.leaveSlice.date")).sendKeys(departureDate);
            Thread.sleep(1000);

            //Enter variable text into the "Return" field
            wd.findElement(By.name("ar.rt.returnSlice.date")).click();
            wd.findElement(By.name("ar.rt.returnSlice.date")).clear();
            wd.findElement(By.name("ar.rt.returnSlice.date")).sendKeys(returnDate);
            Thread.sleep(1000);

            //Clicks the "Search Flights" button
            wd.findElement(By.name("search")).click();

            Thread.sleep(30000);

            String bestPrice = wd.findElement(By.cssSelector(".money.small-cents.small-symbol")).getText();

            if (cell == null)
                cell = row.createCell(4);
                cell.setCellType(Cell.CELL_TYPE_STRING);
                cell.setCellValue(bestPrice);

                FileOutputStream fileOut = new FileOutputStream("/path/to/spreadsheet/Workbook1.xlsx");
                wb.write(fileOut);
                fileOut.close();

            }

        wd.close();

        System.out.println("The Class script has finished running");

        }

}

最佳答案

我认为 LINGS 在上面链接了一个足够的答案,这个答案对于您的情况和新角色来说更通用。如果您希望使用 Selenium,我会花时间阅读 Selenium 网页上的最佳和最差实践。

https://seleniumhq.github.io/docs/best.html#best_practices https://seleniumhq.github.io/docs/worst.html#worst_practices

关于java - 具有 Excel 输入和输出的自动化测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33618846/

相关文章:

java - 替换某些符号之间的文本

java - 对于 ARM 开发,您建议使用哪种 Java 嵌入式 VM?

python - 尝试发送带有希腊文件名的文件时出现 UnicodeError

apache - 主域名的 SSL 不起作用,但子域可以

java - RSA 加密 Java、 key 交换

java - 两组大数 - 它们的总和的分数

Excel 求解器和 VBA : Floating point/decimal numbers in constraints get incorrectly converted to integers?

java - 如何使用列名遍历 Excel 工作表中的当前行?

python - 计算没有。时间间隔的小时数以确定是工作日还是周末

php - 通过 PHP 启动 C 程序时出现段错误