java.lang.NullPointerException , 在 Annotaion_testng.Login_case.login(Login_case.java:34
java.lang.NullPointerException ,at Annotaion_testng.Login_case.login(Login_case.java:34
package Annotaion_testng;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Login_case {
public String baseUrl = "https://www.eventerprise.com";
String driverpath = "C://Users//fizan//Documents//testing//webdriver_soft//fire_fox_driver//geckodriver.exe";
public WebDriver driver;
@BeforeMethod
public void f() throws InterruptedException {
System.out.println("opening firefox");
System.setProperty("webdriver.gecko.driver", driverpath);
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
Thread.sleep(10000);
}
@Test
public void login(){
System.out.println("click on login link from header");
//code is failing from here
driver.findElement(By.xpath(".//*[@id='app']/div/div/div/header/div[1]/div[1]/div[2]/ul/li[4]/a")).click();
driver.findElement(By.id("email")).sendKeys("fijan@atyantik.com");
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//enter password
driver.findElement(By.id("password")).sendKeys("atyantik@1234");
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//click on login button
driver.findElement(By.xpath(".//*[@id='eve-modal']/div/div[1]/div/div/div[2]/div/div/div/div[1]/div[2]/form/div[4]/button")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//click on log out from header
System.out.println("logging out");
driver.findElement(By.xpath(".//*[@id='app']/div/div/header/div[1]/div[1]/div[2]/div[1]/ul/li[7]/a")).click();
}
}
您在 f
WebDriver driver = new FirefoxDriver();
中隐藏 driver
因为您已经正确地将其声明为字段,所以删除了新的重新声明
即
driver = new FirefoxDriver();
package Annotaion_testng;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Login_case {
public String baseUrl = "https://www.eventerprise.com";
String driverpath = "C://Users//fizan//Documents//testing//webdriver_soft//fire_fox_driver//geckodriver.exe";
public WebDriver driver;
@BeforeMethod
public void f() throws InterruptedException {
System.out.println("opening firefox");
System.setProperty("webdriver.gecko.driver", driverpath);
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
Thread.sleep(10000);
}
@Test
public void login(){
System.out.println("click on login link from header");
//code is failing from here
driver.findElement(By.xpath(".//*[@id='app']/div/div/div/header/div[1]/div[1]/div[2]/ul/li[4]/a")).click();
driver.findElement(By.id("email")).sendKeys("fijan@atyantik.com");
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//enter password
driver.findElement(By.id("password")).sendKeys("atyantik@1234");
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//click on login button
driver.findElement(By.xpath(".//*[@id='eve-modal']/div/div[1]/div/div/div[2]/div/div/div/div[1]/div[2]/form/div[4]/button")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//click on log out from header
System.out.println("logging out");
driver.findElement(By.xpath(".//*[@id='app']/div/div/header/div[1]/div[1]/div[2]/div[1]/ul/li[7]/a")).click();
}
}
您在 f
WebDriver driver = new FirefoxDriver();
中隐藏 driver
因为您已经正确地将其声明为字段,所以删除了新的重新声明
即
driver = new FirefoxDriver();