无法执行selenium testng代码(FileNotFoundException)

时间:2015-03-23 16:59:33

标签: java eclipse excel selenium

我已经编写了dataprovider,testobjects和testcase类,当我尝试在eclipse中运行时遇到以下错误

我在路径src / test / resources中拖放了excel表,即chrome驱动程序。请告诉我这是在eclipse中将文件添加到资源的正确方法。

提前致谢。

错误:

java.lang.RuntimeException: java.io.FileNotFoundException: G:\Selenium%20Docs\Selenium%20Setup\SeleniumLearning\target\test-classes\rediff_book_login.xls (The system cannot find the path specified)

    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161)

    at org.testng.internal.Parameters.handleParameters(Parameters.java:429)

    at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)

    at org.testng.internal.Invoker.createParameters(Invoker.java:1075)

    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)

    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.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:1224)

    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)

    at org.testng.TestNG.run(TestNG.java:1057)

    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)

    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)

    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

2 个答案:

答案 0 :(得分:0)

如果您的Excel文件位于类路径中,只需通过

加载它
getClass().getResourceAsStream("/rediff_book_login.xls");

...在测试用例中,上面的斜杠标记了目录src / test / resources。

如果这没有帮助,请提供一个代码段,向我们展示您的文件加载实现。

答案 1 :(得分:0)

按照上面提供的解决方案或将文件“rediff_book_login.xls”保留在“G:\Selenium%20Docs\Selenium%20Setup\SeleniumLearning\target\test-classes\

位置

尝试类似以下代码:

String filepath= "G:\\Selenium Docs\\Selenium Setup\\SeleniumLearning\\target\\test-classes\\rediff_book_login.xls"; FileInputStream file = new FileInputStream(new File(filepath));

或者您也可以尝试:

String filepath= "G:\\Selenium Docs\\Selenium Setup\\SeleniumLearning\\target\\test-classes\\rediff_book_login.xls"; FileInputStream file = new FileInputStream(filepath);