【问题标题】:TestNG script not able to open Excel when run from .bat file从 .bat 文件运行时,TestNG 脚本无法打开 Excel
【发布时间】:2016-08-25 05:46:14
【问题描述】:

我的 TestNg 脚本通过 DataProvider 使用来自 Excel 的输入数据。当我从 Eclipse 运行 testng.xml 文件时,它工作正常。 但是当我尝试从 .bat 文件运行相同的文件时它不起作用。它在尝试打开 Excel 文件时失败。 请帮忙。

这是我的 DataProvider 类:

@DataProvider(name="DataProvider")
public static Object[][] passExcelData(Method m) throws BiffException,   IOException
{
    String sheetName = m.getName();
    config = new ExcelDataConfig("inputData.xlsx", sheetName);
    int rows = config.getRowCount(sheetName);
    int cols = config.getColCount(sheetName);
    Object[][] data = new Object[rows][cols];

    for(int i=0; i<rows; i++)
    {
        for(int j=0; j<cols; j++)
        {
            data[i][j] = config.getData(sheetName, i, j);
        }
    }
    return data;
  }

下面是BeforeClass和测试调用DataProvider:

@BeforeClass
public void setup() throws InterruptedException
{
    System.setProperty("webdriver.chrome.driver", rootFolder +"Drivers\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    System.out.println("setup done");
}

@Test(priority = 7, dataProvider="DataProvider")
public void EmailAlerts(String Product, String URL, String username, String password) throws InterruptedException
{   
    functionName = Product + "_EmailAlerts_" +username;
    HomePage(URL);
    Login.loginProcess(username, password);
    EmailAlerts.EmailAlerts_Process();
    Logout();
}

它不会出现任何错误。 BeforeClass 运行没有问题。它打开浏览器并显示“设置完成”消息。它甚至调用 DataProvider 类,但无法打开 excel 文件。 仅当我从 .bat 文件运行时才会发生此问题。如果我从 excel 部分中删除读取并硬编码 DataProvider 类中的输入数据 - 相同的 bat 文件可以正常工作。


我已经简化了我的脚本并将 Verbose 设置为 10。下面是最新的:

public class Master {
@Test
public void sample() throws Exception 
{
    System.out.println("INSIDE SAMPLE");
    FileInputStream fis = new FileInputStream ("C:\\Automation\\Test\\inputData.xlsx");
    System.out.println("FILE");
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    System.out.println("WORKBOOK");
    wb.getSheet("MyDetails");
    System.out.println("SHEET");

    System.out.println(wb.getActiveSheetIndex());
}

}

以下是从 bat 文件运行时的日志:

C:\Automation\Test>cd C:\Automation\Test

C:\Automation\Test>set ProjectPath=C:\Automation\Test

C:\Automation\Test>echo C:\Automation\Test
C:\Automation\Test

C:\Automation\Test>set classpath=C:\Automation\Test\bin;C:\Automation\Test\lib\*


C:\Automation\Test>echo C:\Automation\Test\bin;C:\Automation\Test\lib\*
C:\Automation\Test\bin;C:\Automation\Test\lib\*

C:\Automation\Test>java org.testng.TestNG C:\Automation\Test\testng.xml
...
... TestNG 6.8beta 20120825_1010 by C?dric Beust (cedric@beust.com)
...

[TestRunner] Running the tests in 'Test' with parallel mode:false
[RunInfo] Adding method selector: org.testng.internal.XmlMethodSelector@14caa3f
priority: 10
[TestClass] Creating TestClass for [ClassImpl class=Master.Master]
[TestClass] Adding method Master.sample()[pri:0, instance:null] on TestClass cla
ss Master.Master
[XmlMethodSelector] Including method Master.sample()
[TestNG] Running:
  C:\Automation\Test\testng.xml

[SuiteRunner] Created 1 TestRunners
[TestRunner] Running test Test on 1  classes,  included groups:[] excluded group
s:[]
===== Test class
Master.Master
    @Test Master.sample()[pri:0, instance:Master.Master@1150435]
======
[Invoker 6738746] Invoking Master.Master.sample
INSIDE SAMPLE
FILE
===== Invoked methods
    Master.sample()[pri:0, instance:Master.Master@1150435] 18154549
=====
Creating C:\Automation\Test\test-output\Suite\Test.html
Creating C:\Automation\Test\test-output\Suite\Test.xml
FAILED: sample
java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
        at Master.Master.sample(Master.java:79)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocat
ionHelper.java:80)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWork
er.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
        at org.testng.TestNG.run(TestNG.java:1031)
        at org.testng.TestNG.privateMain(TestNG.java:1338)
        at org.testng.TestNG.main(TestNG.java:1307)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFW
orkbook
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 24 more


===============================================
    Test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

Creating C:\Automation\Test\test-output\old\Suite\toc.html
Creating C:\Automation\Test\test-output\old\Suite\Test.properties
Creating C:\Automation\Test\test-output\old\Suite\index.html
Creating C:\Automation\Test\test-output\old\Suite\main.html
Creating C:\Automation\Test\test-output\old\Suite\groups.html
Creating C:\Automation\Test\test-output\old\Suite\methods.html
Creating C:\Automation\Test\test-output\old\Suite\methods.html
Creating C:\Automation\Test\test-output\old\Suite\methods.html
Creating C:\Automation\Test\test-output\old\Suite\methods-alphabetical.html
Creating C:\Automation\Test\test-output\old\Suite\methods-alphabetical.html
Creating C:\Automation\Test\test-output\old\Suite\methods-alphabetical.html
Creating C:\Automation\Test\test-output\old\Suite\classes.html
Creating C:\Automation\Test\test-output\old\Suite\reporter-output.html
Creating C:\Automation\Test\test-output\old\Suite\methods-not-run.html
Creating C:\Automation\Test\test-output\old\Suite\testng.xml.html
Creating C:\Automation\Test\test-output\old\index.html
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@1637f22: 53 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter@2d56bf: 3 ms
Creating C:\Automation\Test\test-output\junitreports\TEST-Master.Master.xml
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@122cdd0: 4 ms
Creating C:\Automation\Test\test-output\testng-failed.xml
Creating C:\Automation\Test\test-output\Suite\testng-failed.xml
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 6 ms
Creating C:\Automation\Test\test-output\index.html
[TestNG] Time taken by org.testng.reporters.jq.Main@1a4555e: 40 ms
Creating C:\Automation\Test\test-output\testng-results.xml
[TestNG] Time taken by org.testng.reporters.XMLReporter@1b4f649: 6 ms

C:\Automation\Test>pause
Press any key to continue . . .

【问题讨论】:

  • 请提供有关您的信息的更多信息。请编辑您的问题以包含您使用的脚本以及显示的错误消息(如果有)。

标签: excel batch-file testng


【解决方案1】:

数据提供者方法不应该抛出异常。将它们管理到你的方法中,你就会明白你的代码出了什么问题。

我敢打赌,当您使用 bat 文件运行时,inputData.xlsx 在预期位置不可用。

【讨论】:

  • 感谢您的回复。
  • 下面是我的bat文件:
  • cd C:\DCE\Automation set ProjectPath=C:\DCE\Automation set classpath=%ProjectPath%\bin;%ProjectPath%\lib* java org.testng.TestNG %ProjectPath%\testng .xml 暂停
  • 我不明白我在哪里做错了。如果我从 Eclipse 运行 testng.xml,它就可以工作。如果我没有阅读 excel 步骤,即使是相同的 bat 文件也会运行。这只是从 bat 文件打开 excel 时的问题。
  • 管理异常并告诉我们堆栈跟踪是什么
【解决方案2】:

是的,现在已经解决了。我的 poi 文件位于项目文件夹\lib\poi 文件夹中。我已将它们移至项目文件夹\lib。它有效。 谢谢@Julien Herr。

【讨论】:

    最近更新 更多