显示java.lang.NullPointerException ---" com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)"

时间:2017-05-28 02:05:27

标签: java selenium

我是Selenium的新手,我正在尝试创建一个小框架,我在其中登录一个应用程序,然后尝试验证帐户中的一些文本。

但是一旦我的脚本登录,浏览器就会关闭,我无法验证文本字符串。

有些人说这是因为驱动程序是null并且我需要在LoginPage中导入基类 我也尝试了这个问题并得到了同样的问题。

BASE CLASS

package testcases;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;


import factory.BrowserFactory;
import factory.DataproviderFactory;
import pages.LoginPage;

public class baseClass {

 WebDriver driver;


    public void setUP()
    {
        driver=BrowserFactory.getBrowser("chrome");
        driver.get(DataproviderFactory.getConfig().getApplicationurl());
    }

    public void loginIntoApplication()
    {

        LoginPage login=PageFactory.initElements(driver, LoginPage.class);
        login.Login(DataproviderFactory.getExcel().getData(0, 0, 0), DataproviderFactory.getExcel().getData(0, 0, 1));

    }


    public void TearDown()
    {
        driver.quit();
    }


}

登录页面

package pages;

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

import testcases.baseClass;


public class LoginPage extends baseClass {

    WebDriver driver;

    public LoginPage(WebDriver ldriver) 
    {
        this.driver=ldriver;
    }


    @FindBy(xpath="//input[@id='log']") WebElement username;
    @FindBy(xpath="//input[@id='pwd']") WebElement password;
    @FindBy(xpath="//input[@id='login']") WebElement LoginButton;

    @FindBy(xpath="//li[@id='wp-admin-bar-my-account']/a") WebElement loginVerification;
    public  String loginText;
    public  String Exp="Howdy, AISHWARY";



    public void Login(String uname,String pwd)
    {
        username.sendKeys(uname);
        password.sendKeys(pwd);
        LoginButton.click();
    }

    public String LoginVerification()
    {
        WebDriverWait wait=new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@id='wp-admin-bar-my-account']/a")));
        loginText=loginVerification.getText();
        return loginText;
    }
}

testCaseLoginPage

package testcases;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import pages.LoginPage;


public class testCaseLoginPage extends baseClass{

    WebDriver driver;

    baseClass base=new baseClass();

    @BeforeTest
    public void initial()
    {
        base.setUP();
    }

    @Test(priority=1)
    public void login()
    {

        base.loginIntoApplication();
        LoginPage login=PageFactory.initElements(driver, LoginPage.class);

        login.LoginVerification();
        System.out.println(login.loginText);
        Assert.assertEquals(login.loginText, login.Exp,"Unable to verify login verification");

    }

    @AfterTest
    public void close()
    {
        base.TearDown();
    }
}

错误记录

[TestNG] Running:
  C:\Users\Freak\AppData\Local\Temp\testng-eclipse--1783674419\testng-customsuite.xml

Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 10357
Only local connections are allowed.
May 28, 2017 7:08:42 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: login
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 pages.LoginPage.LoginVerification(LoginPage.java:42)
    at testcases.testCaseLoginPage.login(testCaseLoginPage.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
    at org.testng.TestNG.run(TestNG.java:1031)

1 个答案:

答案 0 :(得分:0)

我在鸦片中发现了与下面相同的错误

java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:882)
    at io.appium.java_client.TouchAction.<init>(TouchAction.java:52)

原因-:因为我在子类和父类中都初始化了驱动程序,如下所示。

儿童班

public class AirAsia extends Base {
@SuppressWarnings("rawtypes")
  private AndroidDriver driver;

/*
 * Sign up Page Elements
 */

@AndroidFindBy(xpath = "//*[@text='Flights']")
private MobileElement flights;}

基类

    public class Base {

@SuppressWarnings("rawtypes")
public AppiumDriver driver;

/*
 * General App Elements
 */

@AndroidFindBy(xpath = "//androidx.appcompat.app.ActionBar.Tab[@content-desc=\"Home\"]/android.widget.TextView")
public MobileElement home;}

解决方案-:

所以现在您从子类中删除驱动程序初始化,如下所示 儿童班

    public class AirAsia extends Base {
@SuppressWarnings("rawtypes")
  private AndroidDriver driver;

/*
 * Sign up Page Elements
 */

@AndroidFindBy(xpath = "//*[@text='Flights']")
private MobileElement flights;}

希望它将帮助所有人使用Java或任何语言解决Selenium和Appium中的此类错误。如果您要扩展父类,并且您在基类或父类中具有初始化驱动程序,则不必初始化驱动程序。