Java 第二个类正在为TestNG引发空指针异常
我使用TestNG为每个网页创建了不同的类。我的第一个类工作正常,但第二个类抛出空指针异常。同样的脚本,如果我只在一个类中组合,它就可以正常工作,没有任何问题 下面是我的基础课Java 第二个类正在为TestNG引发空指针异常,java,selenium-webdriver,testng,Java,Selenium Webdriver,Testng,我使用TestNG为每个网页创建了不同的类。我的第一个类工作正常,但第二个类抛出空指针异常。同样的脚本,如果我只在一个类中组合,它就可以正常工作,没有任何问题 下面是我的基础课 package Test; import org.testng.annotations.Test; import Pages.FM_login; import org.testng.annotations.BeforeTest; import java.util.concurrent.TimeUnit; import o
package Test;
import org.testng.annotations.Test;
import Pages.FM_login;
import org.testng.annotations.BeforeTest;
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.AfterClass;
import org.testng.annotations.AfterTest;
public class Baseclass {
WebDriver driver;
FM_login objLogin;
@BeforeTest
public void setup() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\****\\Downloads\\chromedriver.exe");
String url = "http://vatlookup.cloudfront.net";
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
String Title = driver.getTitle();
System.out.println(Title);
System.out.println("Application Launch successful");
// Accept the cookie policy
driver.findElement(By.xpath("//a[@class='cc-btn cc-allow']")).click();
}
@Test
public void Login() {
objLogin = new FM_login(driver);
objLogin.GoToLoginScreen();
objLogin.loginToFleetMatch("abc", "123");
System.out.println("Login Successful");
}
@AfterTest(enabled=false)
public void teardown() {
driver.close();
}
这是另一门课
package Test;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import Pages.FM_Profile;
public class Profile {
WebDriver driver;
FM_Profile objProfile;
@Test
public void Skip_Tutorial() {
objProfile = new FM_Profile(driver);
driver.getCurrentUrl();
objProfile.SkipTutorial();
}
@Test(enabled=false)
public void edit_profile() {
objProfile.edit_profile();
}
}
下面是第二个类的页面对象
package Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class FM_Profile {
WebDriver driver;
By company_name = By.xpath("//p[@id='company-name']");
By plan_details = By.xpath("//p[@id='current-plan']");
By End_tour = By.xpath("//button[@class='driver-close-btn']");
By edit_profile = By.xpath("//span[contains(text(),'Edit')]");
By Description = By.xpath("//textarea[@id='mat-input-10']");
By Upload_image = By.xpath("//div[@class='upload-btn continental-book ng-star-inserted']");
By Save_profile = By.xpath("//button[@class='save-btn continental-book mat-stroked-button mat-button-base mat-primary editMode cdk-focused cdk-mouse-focused']");
public FM_Profile(WebDriver driver) {
this.driver = driver;
}
public void SkipTutorial() {
driver.findElement(End_tour).click();
}
执行后,它将显示第二个类的java.lang.NullPointerException
package Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class FM_Profile {
WebDriver driver;
By company_name = By.xpath("//p[@id='company-name']");
By plan_details = By.xpath("//p[@id='current-plan']");
By End_tour = By.xpath("//button[@class='driver-close-btn']");
By edit_profile = By.xpath("//span[contains(text(),'Edit')]");
By Description = By.xpath("//textarea[@id='mat-input-10']");
By Upload_image = By.xpath("//div[@class='upload-btn continental-book ng-star-inserted']");
By Save_profile = By.xpath("//button[@class='save-btn continental-book mat-stroked-button mat-button-base mat-primary editMode cdk-focused cdk-mouse-focused']");
public FM_Profile(WebDriver driver) {
this.driver = driver;
}
public void SkipTutorial() {
driver.findElement(End_tour).click();
}
错误
在概要文件类中,驱动程序对象未初始化。您正在初始化的驱动程序对象位于该类的基类和实例中。您还需要初始化Profile类中的驱动程序对象WebDriver从未初始化过
WebDriver=新的WebDriver() 不相关但很重要:您应该遵循Java命名约定:变量名和方法名应该用camelCase编写,类名应该用PascalCase编写;org.testng.TestNGException:无法实例化类Test.Profile