gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:42:46 24 4
gpt4 key购买 nike

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

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

有人说是因为driver为null,需要在LoginPage中导入基类。
我也试过了,遇到了同样的问题。

基础类

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)

最佳答案

我在appium中发现了如下相同的错误

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;}

解决方案-:

So now you remove the driver initialisation from child class as below Child class

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

/*
* Sign up Page Elements
*/

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

Hope it will help all to solve such type of errors in both Selenium and Appium using Java or any language. Just don't have to initialise driver if you are extending parent class and if yo have initialise driver in base or parent class.

关于java.lang.NullPointerException-- -"com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44223175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com