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

时间:2017-03-21 19:25:39

标签: 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)

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:-1)

通过修改&#34; Login.Java&#34;中的代码class ..问题与驱动程序为null有关,因为我没有扩展&#34; Base&#34;在&#34;登录&#34;类。