selenium 这个错误是什么:在com.google.common.base.Preconditions.checkNotNull

k2fxgqgv  于 2022-11-10  发布在  Go
关注(0)|答案(2)|浏览(156)

我是一个新的自动化测试员,在样本测试脚本上工作,需要你们的一些帮助,我已经尝试使用POM和基本测试。
我已经创建了2个包-页面和测试用例。
当我试图从我的页面包中访问“ClickJoin,EnterUsername”方法时,我遇到了一些错误。我试图找出这个问题,但无法理解为什么会发生这个错误。
以下是错误:com.google.common.base.Preconditions.checkNotNull中的java.lang.NullPointerException

**我的Pages包中的代码:**包com.gupday.Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Login {

public WebDriver driver;
public WebDriverWait wait;
By join = By.xpath("//*[@id='members']/a[1]");
By username = By.id("username");
By password = By.id("password");
By loginButton = By.xpath("//*[@id='normallogin']/div[4]/p/input[2]");

public Login (WebDriver driver){
    this.driver=driver;
}
    public void ClickJoin(){
        wait = new WebDriverWait(driver,10);
        //driver.findElement(join).click();

wait.until(ExpectedConditions.visibilityOfElementLocated(join)).click();
    }

    public void EnterUsername(){
        wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.visibilityOfElementLocated(username)).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(username)).sendKeys("Test username");
        System.out.println("Username Entered");
    }

    public void EnterPassword(){
        driver.findElement(password).clear();
        driver.findElement(password).click();
        System.out.println("Password Entered");
    }

        public void ClickButton(){
            wait = new WebDriverWait(driver, 10);
            //driver.findElement(loginButton).click();
 wait.until(ExpectedConditions.visibilityOfElementLocated(loginButton)).click();
            System.out.println("Login Button Clicked");
        }
    }

我的“TestCase”包中的代码:此代码属于我的基类。

package com.gptoday.com.gptoday.testcases;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.testng.annotations.AfterMethod;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Base {
    public WebDriver driver;

    @BeforeMethod
    public void BaseSetup(){
        ProfilesIni prof = new ProfilesIni();
        FirefoxProfile ffProfile= prof.getProfile ("vishvesh");
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);

        String BaseUrl = "https://www.gptoday.com";
        System.setProperty("webdriver.gecko.driver", "G:/Workplace/AutomationSetupFiles/Geckdriver/geckodriver.exe"); 
        driver = new FirefoxDriver (ffProfile);
        driver.get(BaseUrl);
        driver.manage().window().maximize();
    }

    @AfterMethod
    public void afterTest() {
        System.out.println("Success");
      }
}

**Below the the code for the test case.**

package com.gptoday.com.gptoday.testcases;

import org.testng.annotations.Test;
import com.gptoday.pages.Login;
import org.openqa.selenium.WebDriver;

public class LoginVerification extends Base {

    public WebDriver driver;
    public Login obj = new Login(driver);

    @Test
    public void Verify()
    {
        obj.ClickJoin();
        obj.EnterUsername();
    }
}

**Error:**

Success
FAILED: Verify
java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
    at com.gptoday.pages.Login.ClickJoin(Login.java:22)
    at com.gptoday.com.gptoday.testcases.LoginVerification.Verify(LoginVerification.java:16)
    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:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
cwtwac6a

cwtwac6a1#

以下是您收到的错误背后的原因:
在Login和EnterUsername方法中,当您定义等待时,您已经传递了WebDriver&Interval的示例。现在,只要Selence检测到这种特殊类型的参数,Selence就会将其视为FluentWait。现在,当您实际实现一个FluentWait时,您必须导入这两个库“com.google.Common.base.Function”,这将解决您所面临的错误。当然,您还必须导入“FluentWait”。
如果这对你有帮助,请告诉我。

7xllpg7q

7xllpg7q2#

通过修改“Login.Java”类中的代码。这个问题与驱动程序为空有关,因为我没有在“Login”类中扩展“Base”。

相关问题