@Test method is not working, but works when placed after flipkart url section.
我认为,如果我将click函数放置在url上,那将是错误的方法,代码应独立。
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.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Testdemo {
WebDriver driver;
@BeforeTest
public void test2() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","\\driver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
}
@Test
public void test3() throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.className("_29YdH8")).click();
}
}
java.lang.NullPointerException在Test.Flipkart.data(Flipkart.java:40)
问题是,即使您为驱动程序定义了一个类级别变量,如下所示
public class Testdemo {
WebDriver driver;
您从未启动过,因为您在@BeforeTest中再次将其定义为局部变量因此,它会为驱动程序抛出零点,要解决此问题,请删除webdriver,因此将使用类级别的变量
**WebDriver** driver=new ChromeDriver();
更正的代码
@BeforeTest
public void test2() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","\\driver\\chromedriver.exe");
driver=new ChromeDriver();