-1

Fails to hit method2() for following code snippet

public class project 
{
    static WebDriver driver;

    @Test()
    public void method1()
    {
    System.setProperty("webdriver.chrome.driver","C:\\Users\\52013597\\Desktop\\Selenium\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("url");
    }

    @Test()
    public void method2()
        {
            driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
            driver.findElement(By.xpath("//*[@id=\"login-panel\"]/div[2]/form/div[3]/input")).clear();
            driver.findElement(By.xpath("//*[@id=\"login-panel\"]/div[2]/form/div[3]/input")).sendKeys("TOYVA30U5VC");
        }
}

[RemoteTestNG] detected TestNG version 6.14.2 Starting ChromeDriver 2.38.551601 (edb21f07fc70e9027c746edd3201443e011a61ed) on port 10902 Only local connections are allowed. Aug 08, 2018 8:13:37 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS PASSED: method1 FAILED: method2 java.lang.NullPointerException at Framework1.project.method2(project.java:24) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

=============================================== Default test

Tests run: 2, Failures: 1, Skips: 0

=============================================== Default suite

Total tests run: 2, Failures: 1, Skips: 0

wazz
  • 4,953
  • 5
  • 20
  • 34
chaithra
  • 11
  • 1
  • driver object is not available in test method 2 as it is created locally in method 1, you should not create new driver object, instead of that use static object. – Sumit2500 Aug 09 '18 at 10:11

2 Answers2

0

You are missing driver object, either add to method2:

  WebDriver driver = new ChromeDriver();

Or move this statement from method1 to class level.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

You have define Webdriver at multiple place, Instead you have to initialize object value to driver in @Test method.

public class project 
{
static WebDriver driver;

@Test()
public void method1()
{
  System.setProperty("webdriver.chrome.driver","C:\\Users\\52013597\\Desktop\\Selenium\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("url");
}

@Test()
public void method2()
    {
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        driver.findElement(By.xpath("//*[@id=\"login-panel\"]/div[2]/form/div[3]/input")).clear();
        driver.findElement(By.xpath("//*[@id=\"login-panel\"]/div[2]/form/div[3]/input")).sendKeys("TOYVA30U5VC");
    }
}

Or You can define it in Class level or Separate method without @Test annotation if you want to reuse it in another class.

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51