Dataprovider anotation - Testng Selenium Java

时间:2017-07-04 06:53:33

标签: selenium-webdriver testng selenium-firefoxdriver testng-dataprovider testng-eclipse

查询 - 最后一组值保持失败,我得到的错误是它无法对xpath进行细化,但是对于其他两种情况它的工作正常。所以有点困惑。我也粘贴了错误代码。非常感谢我能得到的任何帮助。提前谢谢。

测试代码 -

@Test(priority = 4, dataProvider = "getData")
            public void loginScenario(String Username, String Password) throws InterruptedException {

WebElement user =  driver.findElement(By.xpath(".//*[@id='LoginForm_username']"));
                 if(null != user) {
                     user.clear();
                 }

                 WebElement pass =  driver.findElement(By.xpath(".//*[@id='LoginForm_password']"));
                 if(null != pass) {
                     pass.clear();
                 } 

                // driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).click();
                // driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).click();
                driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).sendKeys(Username);
                driver.findElement(By.xpath(".//*[@id='LoginForm_password']")).sendKeys(Password);
                driver.findElement(By.xpath(".//*[@id='login-form']/button")).click();
                driver.findElement(By.xpath(".//*[@id='LoginForm_username']")).clear();
                driver.findElement(By.xpath(".//*[@id='LoginForm_password']")).clear();

            }

            @DataProvider
            public Object[][] getData() {

                Object[][] data = new Object[3][2];
                data[0][0] = "abcd";
                data[0][1] = "xyz";
                data[1][0] = "...";
                data[1][1] = ".";
                data[2][0] = "blah@blah.com";
                data[2][1] = "kingkongkijai";

                return data;

            }
        }

Error -

FAILED: loginScenario("blah@blah.com", "kingkongkijai")
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='LoginForm_username']"}
Command duration or timeout: 51 milliseconds
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
    at pages.LoginPage.loginScenario(LoginPage.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='LoginForm_username']"}
For documentation on this error, please visit: 

http://seleniumhq.org/exceptions/no_such_element.html

1 个答案:

答案 0 :(得分:0)

@Madis Kangro

我刚刚输入后添加了以下代码,你就开始了!非常感谢

WebElement user = driver.findElement(By.xpath(".//*[@id='LoginForm_username']"‌​)); if(null != user) { user.clear(); } WebElement pass = driver.findElement(By.xpath(".//*[@id='LoginForm_password']"‌​)); if(null != pass) { pass.clear(); }