Search code examples
selenium-webdrivertestngselenium-firefoxdrivertestng-dataprovidertestng-eclipse

Dataprovider anotation - Testng Selenium Java


Query - The last set of values keep failing, the error that i am getting is that its unable to fine the xpath, but its working fine for other two cases. So a bit confused. I have pasted the error code as well. Would appreciate any help I can get. Thanks in advance.

Test Code -

@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] = "[email protected]";
                data[2][1] = "kingkongkijai";

                return data;

            }
        }

Error -

FAILED: loginScenario("[email protected]", "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


Solution

  • @Madis Kangro

    I just added the below code after you inputs and you were bang on ! Thanks so much

    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(); }