这是什么错误:在com.google.common.base.Preconditions.checkNotNull [英] What is this Error: at com.google.common.base.Preconditions.checkNotNull

查看:5795
本文介绍了这是什么错误:在com.google.common.base.Preconditions.checkNotNull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新的自动化测试人员,处理样本测试脚本,需要一些帮助,我尝试使用POM和基本的TestNG。

I am a new automation tester, Working on sample test scripts, Need some help from you guys, I have tried to use POM and basic TestNG.

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

I have created 2 packages - pages and testcases.

当我尝试从我的页面包中访问ClickJoin,Enterusername方法时,我收到了一些错误。我曾尝试搞清楚这个问题,但无法理解为什么会出现这个错误。

I am getting some error when I am trying to access the "ClickJoin, Enterusername" methods from my pages packages. I had tried figuring out the issue, but was unable to get to why this error is occurring.

下面是错误:
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull

Below is the Error: java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull

我的代码来自pages包:
package com.gptoday.pages;

My Code from the "pages" package: package 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)


推荐答案

以下是您获得错误的原因:

Here's the reason behind the error you are getting:

在Login&在您定义了已经通过Webdriver&的实例的等待时,输入用户名方法。间隔。现在每当Selenium检测到这种特殊类型的参数时,Selenium会将其视为FluentWait。现在,当您实际实现FluentWait时,您必须导入这两个库com.google.common.base.Function,它将解决您所面临的错误。当然你也必须导入FluentWait。

In both Login & EnterUsername methods while you defined the wait you have passed the instance of Webdriver & interval. Now whenever Selenium detects this particular type of argument Selenium treats this as FluentWait. Now when you actually implement a FluentWait you have to import those two libraries "com.google.common.base.Function" which will address the error you are facing. Of course you have to import "FluentWait" as well.

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

Let me know if this helps you.

这篇关于这是什么错误:在com.google.common.base.Preconditions.checkNotNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆