java - 这是什么错误 : at com. google.common.base.Preconditions.checkNotNull

标签 java selenium automation pom.xml

我是一名新的自动化测试人员,正在研究示例测试脚本,需要你们的帮助,我尝试使用 POM 和基本的 TestNG。

我创建了 2 个包 - 页面和测试用例。

当我尝试从我的页面包中访问“ClickJoin,Enterusername”方法时出现一些错误。我曾尝试找出问题所在,但无法弄清楚为什么会出现此错误。

错误如下: java.lang.NullPointerException异常 在 com.google.common.base.Preconditions.checkNotNull

“页面”包中的我的代码: 包 com.gptoday.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");
        }
    }

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

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)

最佳答案

这是您收到错误的原因:

在 Login 和 EnterUsername 方法中,当您定义等待时,您已经传递了 Webdriver 和 interval 的实例。现在,每当 Selenium 检测到这种特殊类型的参数时,Selenium 都会将其视为 FluentWait。现在,当您实际实现 FluentWait 时,您必须导入这两个库“com.google.common.base.Function”,这将解决您面临的错误。当然,您还必须导入“FluentWait”。

如果这对您有帮助,请告诉我。

关于java - 这是什么错误 : at com. google.common.base.Preconditions.checkNotNull,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42936414/

相关文章:

java - 单击 TextView 类中的链接

visual-c++ - VC++环境的Fitnesse工具

python - 以特定格式提取 Excel 数据作为 python 文件的输入

java - Guava - InetAddress.coerceToInteger 返回 int 而不是 long

java - 如何在执行 while 循环时测试 if 语句

java - Android:在 RecyclerView 中添加 View 时出现奇怪的 react

java - Selenium 测试 Java Maven 依赖关系

python - 如何使用 Selenium 测量页面下载时间

java - 获取 Eclipse 编辑器的当前源代码?

python - 如何使用 Sikuli 键入希腊字母