TestNg is not running @test annotations with 'driver.find' however works with just 'syso-printing

fari :

I am having trouble with starting a new package using TestNg. Note I have simplified the code to try to figure out where I was going wrong. At @Test(priority=3) I am having the issue. It is not allowing me to click on a button.

I have checked the compiler and running 1.8 which is fine.

I checked my previous project which is running just fine but could not see any differences. Also I have my dependencies which are maven, selenium, testng which looks good. I imported the library which is good.

The kicker is that TestNg worked Great in my past Project maybe one month ago on the same computer and everything.

package com.Prod.dtx_project;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class DIF1v2 {

protected WebDriver driver;

@Test(priority=1)
public void initialization()
{
    // To set the path of the Chrome driver.
    System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Eclipes\\ChromeDriver\\chromedriver.exe");
}

@Test(priority=2)
public void OpenBrowserChrome ()
{
    // Create a new instance of the Chrome driver
    WebDriver driver = new ChromeDriver();
    //WebDriver driver = new InternetExplorerDriver();
    driver.get("https://testng.org/doc/index.html");
        // To maximize the browser
    driver.manage().window().maximize();
        // implicit wait
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        // Print a Log In message to the screen
    System.out.println("Successfully opened the website ");

    driver.findElement(By.xpath("//*[@id='topmenu']/table/tbody/tr[2]/td[1]/a")).click();

}

@Test(priority=3)    //IT FAILED HERE WHEN USING A NEW ANNOTATION// 
public void Issue ()
{
driver.findElement(By.xpath("/html/body/a[13]")).click();

}

@Test(priority=4)
public void LandingPage ()
{
    System.out.println("LandingPage-4");

}

@Test(priority=5)
private void publ() 
{
    System.out.println("publ-5");
}



}

Here is my testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

  <test name="Regression Testing">
    <classes>
      <class name="com.Prod.dtx_project.DIF1v2"/>
    </classes>
  </test>   

</suite> 

Also, here is the error message that I am receiving in the Console -

FAILED: Issue
java.lang.NullPointerException
at com.Prod.dtx_project.DIF1v2.Issue(DIF1v2.java:49)
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 
(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: 5, Failures: 1, Skips: 0
===============================================

I expected that it would open to url and click on the eclipse button on this page however it does not.

PLEASE NOTE: When I move the driver.get and driver.findElement inside the @Test(priority=2), Then IT WORKS. Please see below. HOWEVER How can I Run my TestNG with a this layout. Why does this work but when using more then one @Test annotation it Fails.

@Test(priority=2)
 public void OpenBrowserChrome ()
 {
        // Create a new instance of the Chrome driver
    WebDriver driver = new ChromeDriver();
        //WebDriver driver = new InternetExplorerDriver();
    driver.get("https://testng.org/doc/index.html");
        // To maximize the browser
    driver.manage().window().maximize();
        // implicit wait
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        // Print a Log In message to the screen
    System.out.println("Successfully opened the website ");


    driver.get("https://testng.org/doc/index.html");
    driver.findElement(By.xpath("//*[@id='topmenu']/table/tbody/tr[2]/td[1]/a")).click();



}
fari :

adding a @BeforeClass for systems.setProperty ended up working.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109323&siteId=1