测试类在类之前运行@beforeSuite这会导致 Webdriver 出现 NullreferenceException

Test Class runs before @beforeSuite Class which causes NullreferenceException on Webdriver

提问人:Reventh Kumar 提问时间:8/11/2023 更新时间:8/11/2023 访问量:27

问:

我是 selenium 的新手,我正在尝试设置一个自动化框架,我有两个类,一个用于 Webdriver(基类)初始化,第二个是运行 testScript,我已经用 @BeforeSuite 标记了 Webdriver 初始化

public class Base {
    
    Logger logs = StartLogging("Base");
    public static String browser;
    public static WebDriver driver;

    //Browser installization
    @BeforeSuite
    public void browserSetup() throws IOException{
        browser = readConfigData("browser");
        if (browser.equalsIgnoreCase("Chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
        } else if (browser.equalsIgnoreCase("Firefox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
        } else if (browser.equalsIgnoreCase("edge")) {
            WebDriverManager.edgedriver().setup();
            driver = new EdgeDriver();
        }
        driver.get(readConfigData("TestURL"));
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        logs.fatal(driver);
    }
}

其次,我有一个 Page 对象模型类,我在其中初始化 Web 元素

public class loginPage extends Base {

    Logger logs = StartLogging("loginPage");

    @FindBy(xpath = "//input[@id='user-name']")
    WebElement Username;
    @FindBy(xpath = "//input[@id='password']")
    WebElement PassTab;
    @FindBy(xpath = "//input[@id='login-button']")
    WebElement Loginbtn;
    @FindBy(xpath = "//img[@class='bot_column']")
    WebElement Logo;

    public loginPage() {
        logs.fatal(driver);
        PageFactory.initElements(driver, this);
    }
             //--------------rest of the methods below -------------------------
}

我有第三个测试类,从中执行测试

public class LoginTest extends Base {

    loginPage lp = new loginPage();
    Logger logs = StartLogging("LoginTest");
    
    @Test(priority = 1)
    public void VerifyPageTitle() throws IOException, InterruptedException {
        
        String actualTitle = driver.getTitle();
        String ExpectedTitle = readConfigData("ExpectedTitle");
        assertEquals(actualTitle, ExpectedTitle);
    }

    @Test(priority = 2)
    public void VerifyPageLogo() {

        logs.info(lp.IslogoDisplayed());
        assertEquals(lp.IslogoDisplayed(), true);
    }

    @Test(priority = 3)
    public void VerifyPageURL() throws IOException {
        String ActualURL = driver.getCurrentUrl();
        String ExpectedURL = readConfigData("ExpectedURL");
        assertEquals(ActualURL, ExpectedURL);
    }

         //--------------rest of the methods below -------------------------
}

因此,当我尝试从测试套件运行测试时,测试类首先在@BeforeSuite类之前运行,这会导致 searchContext 上出现 Nullreferenceexception(初始化页面对象 Web 元素时)

TestNG 跟踪如下所示

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:68)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at jdk.proxy2/jdk.proxy2.$Proxy46.isDisplayed(Unknown Source)
    at com.saucelab.pages.loginPage.IslogoDisplayed(loginPage.java:31)
    at com.saucelab.Testcases.LoginTest.VerifyPageLogo(LoginTest.java:28)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
    at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:664)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:227)
    at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
    at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:957)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:200)
    at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
    at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:848)
    at org.testng.TestRunner.run(TestRunner.java:621)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:443)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397)
    at org.testng.SuiteRunner.run(SuiteRunner.java:336)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
    at org.testng.TestNG.runSuites(TestNG.java:1114)
    at org.testng.TestNG.run(TestNG.java:1082)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

我怀疑TestClass是在导致此错误的Web驱动程序类之前调用的

调试时,将显示以下控制台输出

19:41:31:241 [main] FATAL loginPage - null
19:41:33:101 [main] FATAL Base - ChromeDriver: chrome on windows (34e5b87c9e9c80670ebcc46b04a7f9e6)
19:41:33:136 [main] INFO ProductPage - Product page loaded
19:41:33:167 [main] INFO YourInformationPage - Entering infomations

从输出中可以看出,Loginpage(Test case) 日志首先打印,Base(Webdriver 类)打印第二

注意:此问题仅发生在第一个 POM 和其余页面正确初始化并且测试成功时

基类(Web 驱动程序)应首先运行,但 Login 类(Test 类)被执行

java selenium-webdriver nullreferenceexception

评论


答: 暂无答案