java NullPointerException无法调用“org”,openqa.selenium.SearchContext.findElement(org.openqa.selenium.由)“因为”这个,searchContext”为null

0sgqnhkj  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(187)

我遇到了一个Java。尝试使用TestNG运行Selenium测试时出现lang.NullPointerException错误。错误消息指出:无法调用**“组织。openqa.selenium.SearchContext.findElement(org.openqa.selenium.由)“因为”这个。searchContext”为null。**
我在下面提供了相关的代码片段:
LandingPage(页面对象类):

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LandingPage {

    @FindBy(linkText = "Enter the Store")
    private WebElement linkToStore;

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

    public void enterTheStore() {
        linkToStore.click();
    }
}

TestBase(测试基类):

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;

import java.time.Duration;

public class TestBase {
    public WebDriver driver;

    @BeforeTest
    void setUpAll() {
        WebDriverManager.chromedriver().setup();
    }

    @BeforeMethod
    void setupEach() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.get("http://przyklady.javastart.pl/jpetstore/");
    }

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

FailedLoginTest(测试类):

import com.javastart.buildingFramework.page.objects.LandingPage;
import com.javastart.tests.base.TestBase;
import org.testng.annotations.Test;

public class FailedLoginTest extends TestBase {

    @Test
    void failedLoginTest() throws InterruptedException {
        LandingPage landingPage = new LandingPage(driver);
        landingPage.enterTheStore();
    }
}

当我尝试运行FailedLoginTest时,我得到了这个错误:java.lang.NullPointerException:无法调用“org”。openqa.selenium.SearchContext.findElement(org.openqa.selenium.由)“因为”这个。searchContext”是null,我不知道为什么它被抛出。任何帮助将不胜感激:)

yyhrrdl8

yyhrrdl81#

修正了这个问题,我忘了在TestBase类中为方法添加公共访问修饰符,解决了这个问题

相关问题