如何解决Selenium中页面对象模型中的java.lang.NullPointerException?

问题描述 投票:0回答:2

测试基类:-

public class TestBase {
    public static WebDriver driver;
    public static Properties prop;

    // TestBase class constructor is used to initialize the properties object to
    // fetch config variables from config.properties file.
    public TestBase() {

        try {

            // File src = new File("./src/main/java/com/config/config.properties");
            File src = new File(".\\src\\main\\java\\com\\config\\config.properties");
            FileInputStream fs = new FileInputStream(src);
            prop = new Properties();
            prop.load(fs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void initialization() throws InterruptedException {
        String browserName = prop.getProperty("Browser");
        if (browserName.equals("chrome")) {
            System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
        } else {
            System.out.println("Oops! Exception has Caught");
        }

        driver.manage().window().maximize();
        driver.get(prop.getProperty("URL"));
    }
}

主页类:-

public class HomePage extends TestBase {

    TestUtil testUtil;

    @FindBy(xpath = "//a[@title='Update your profile, personal settings, and more']")
    WebElement updateProfile;


    @FindBy(xpath = "//a[@title='Create a New Post']")
    WebElement createNewPost;

    @FindBy(xpath="(//span[@class='count'])[2]")
    WebElement drafts;

    @FindBy(xpath="//a[@class='button masterbar__recent-drafts-see-all is-compact is-borderless']")
    WebElement seeAll;

    public HomePage() {
        PageFactory.initElements(driver, this);
    }

    public ProfilePage updateYourProfile() throws AWTException, InterruptedException {
        updateProfile.click();
        Thread.sleep(3000);
        return new ProfilePage();
    }

    public NewPostPage createNewPost() throws InterruptedException {
        createNewPost.click();
        Thread.sleep(3000);
        return new NewPostPage();
    }

    public DraftsPage draftsPage() throws InterruptedException {
        createNewPost.click();
        Thread.sleep(3000);
        drafts.click();
        Thread.sleep(3000);
        seeAll.click();
        Thread.sleep(3000);
        return new DraftsPage();
    }
}

DraftsPage 类:-

public class DraftsPage extends TestBase {

    @FindBy(xpath="(//button[@title='Toggle menu'])[1]")
    WebElement toggleMenu;

    @FindBy(xpath="(//button[@role='menuitem'])[2]")
    WebElement trash;

    public void toggleMenu() throws InterruptedException {
        toggleMenu.click();
        Thread.sleep(3000);
        trash.click();
        Thread.sleep(3000);
    }
}

DraftsPageTest 类:-

public class DraftsPageTest extends TestBase {

    ProfilePage myProfile;
    LoginPage loginpage;
    HomePage homepage;
    NewPostPage newPostPage;
    DraftsPage draftspage;

    public DraftsPageTest() {
        super();
    }

    @BeforeMethod
    public void setUp() throws InterruptedException {
        initialization();
        loginpage = new LoginPage();
        loginpage.login();

        Thread.sleep(3000);
        loginpage.userName(prop.getProperty("username"));
        Thread.sleep(3000);
        homepage = loginpage.password(prop.getProperty("password"));
        Thread.sleep(3000);
        myProfile = new ProfilePage();
        Thread.sleep(3000);
        newPostPage = new NewPostPage();
        Thread.sleep(3000);
        draftspage = new DraftsPage();
        Thread.sleep(3000);
    }

    @Test
    public void DraftsPageTest() throws InterruptedException {
        homepage.draftsPage();
        Thread.sleep(3000);
        draftspage.toggleMenu();
        Thread.sleep(3000);
    }

    @AfterMethod
    public void close() {
        driver.close();
    }
}

错误消息:-

FAILED: DraftsPageTest
java.lang.NullPointerException
    at com.pages.DraftsPage.toggleMenu(DraftsPage.java:18)
    at com.testCases.DraftsPageTest.DraftsPageTest(DraftsPageTest.java:51)
    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:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    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)

描述:-

我正在尝试在 Selenium 的帮助下开发 TestNG 框架。 我开发了HomePage、LoginPage、ProfilePage、NewPostPage、DraftsPage。当我运行

DraftsPageTest.java
时,@BeforeMethod 工作正常 力矩控制到@Test
homepage.draftsPage();
也工作正常。但当控制权到达
draftspage.toggleMenu();
时,它正在抛出

java.lang.NullPointerException。

任何帮助将不胜感激。

提前致谢:)

selenium testng
2个回答
2
投票

您的代码中有一个错误。

请将调用

PageFactory.initElements(driver, this);
的构造函数添加到您的
com.pages.DraftsPage
类中 [类似于共享代码中的 HomePage 构造函数]。这应该可以解决问题。

NullPointerException
背后的原因是因为没有调用
PageFactory.initElements()
WebElement
toggleMenu
未初始化。


0
投票

我也面临着同样的问题。我创建了另一个名为 PageFactory.initElements() 的类 PageBase,但仍然面临错误。 PageBase.class

public class PageBase {
AppiumDriver driver;
public static final long WAIT= 10;
public PageBase(AppiumDriver appiumDriver){
    PageFactory.initElements(new AppiumFieldDecorator(appiumDriver),this);
    driver = appiumDriver;
}
public void waitForVisibility(WebElement element){
    WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(WAIT));
    wait.until(ExpectedConditions.visibilityOf(element));
}
public void clear(WebElement element){
    waitForVisibility(element);
    element.clear();
}
public void click(WebElement element){
    waitForVisibility(element);
    element.click();
}
public void sendText(WebElement element, String text){
    waitForVisibility(element);
    element.sendKeys(text);
}
public String getText(WebElement element ){
    waitForVisibility(element);
    return element.getText();
}

}

登录页面.class

public class LoginPage extends PageBase {
public LoginPage(AppiumDriver appiumDriver) {
    super(appiumDriver);
}
@AndroidFindBy(accessibility = "test:id/Login-text_field")
WebElement mobileNumberField;
public void ProvideMobileNumber(String mobileNumber){
   sendText(mobileNumberField,mobileNumber);
}
@AndroidFindBy(accessibility = "test:id/Login-Button")
WebElement loginCTA;
public void ClickLoginCTA(){
    click(loginCTA);
}

}

© www.soinside.com 2019 - 2024. All rights reserved.