gpt4 book ai didi

java - 执行 TestNG 套件后显示错误 'java.lang.NullPointerException DefaultElementLocator.findElement(DefaultElementLocator.java:69)'

转载 作者:行者123 更新时间:2023-12-01 17:19:59 24 4
gpt4 key购买 nike

我正在 Selenium Webdriver 中使用页面对象模型 (POM) 创建一个混合框架,并创建“TestBase.class”作为父类。

页面对象模型包含以下页面:

页面对象和方法:1. LoginPage.class 和 2. LeadsPage.class

页面对象模型包含以下测试用例文件:

测试用例:1. LoginPageTest 2. LeadsPageTest

TestBase.class

package com.core.qa.base;

import java.io.FileNotFoundException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.core.qa.config.PropertyFile;
import com.core.qa.utility.ExcelReading;
import com.core.qa.utility.Helper;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;

public class TestBase {

public static WebDriver driver;
public static String browserName;
public static String URL;

public PropertyFile PropFile;
public ExcelReading excelRead;

public ExtentReports extent;
public ExtentTest logger;

// TakeScreenshotEx ts = new TakeScreenshotEx();

@BeforeSuite
public void setup() throws FileNotFoundException {

PropertyFile.readPropertiesFile();
System.out.println("In before suite");

if (browserName.equalsIgnoreCase("Chrome")) {
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
driver = new ChromeDriver();
driver.get(URL);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

}

else if (browserName.equalsIgnoreCase("Firefox")) {
System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
driver = new FirefoxDriver();
driver.get(URL);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
}

@AfterMethod
public void teardownMethod(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
Helper.CaptureScreenshot(driver);
}
}

@AfterSuite
public void teardown() {
driver.close();
driver.quit();
}
}

LeadsPage.class

package com.sams.qa.pages;

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

import com.core.qa.base.TestBase;

public class LeadsPage extends TestBase {

//WebDriver driver;

// Page factory OR Object repository

@FindBy(id = "q_search")
WebElement QuickSearchTxtBox;

@FindBy(xpath = "//*[@id=\"total_records\"]")
WebElement totalNoOfLeads;

@FindBy(linkText = "Leads")
WebElement LeadsLink;

@FindBy(id = "ui-id-1")
WebElement LeadsListTab;

@FindBy(id = "ui-id-2")
WebElement LeadAssessmentTab;

@FindBy(name = "addLead")
WebElement AddLeadsBtn;

@FindBy(name = "export")
WebElement ExportAllLeadsbtn;

@FindBy(name = "Search")
WebElement FilterSearchBtn;

@FindBy(name = "filter_search")
WebElement SearchIDNametxtbox;

@FindBy(xpath = "//a[contains(text()),'Assessment']")
WebElement LeadsAssessmentTab;

@FindBy(xpath = "//a[contains(text()),'Clear All']")
WebElement ClearAllSrchFilter;

@FindBy(xpath = "//*[@id=\"tabs-1\"]/table[1]/tbody/tr[2]/td[3]")
WebElement FirstLeadLink;

@FindBy(name = "first_name")
WebElement Fname;

@FindBy(name = "last_name")
WebElement Lname;

@FindBy(name = "email")
WebElement Email;

@FindBy(xpath = "//input[contains(text()),'Yes']")
WebElement IsCustEmailYes;

@FindBy(xpath = "//input[contains(text()),'No']")
WebElement IsCustEmailNo;

@FindBy(name = "address1")
WebElement Add1;

@FindBy(name = "city")
WebElement City;

@FindBy(name = "zip_code")
WebElement Zip;

@FindBy(name = "azimuth")
WebElement Azimuth;

@FindBy(name = "phone")
WebElement Phone;

@FindBy(name = "lead_source_id")
WebElement LeadSource;

@FindBy(xpath = "//select[contains(text()),'PQRS']")
WebElement LeadSrcvalue;

@FindBy(name = "installer_id")
WebElement Installerdrpdn;

@FindBy(xpath = "//*[@id='salesman_id']/option[2]")
WebElement Salesman;

@FindBy(xpath = "//select[contains(text()),'ABCD']")
WebElement InstallerValue;

@FindBy(id = "saveLead")
WebElement SaveBtn;

@FindBy(id = "startProposal")
WebElement StartProposalBtn;

@FindBy(name = "cancel")
WebElement CancelBtn;

// initializing page objects
public LeadsPage(WebDriver ldriver) {
TestBase.driver = ldriver;
PageFactory.initElements(ldriver, this);
}

// Methods or Actions

public LeadsPage openLeadspg() {
LeadsLink.click();
return new LeadsPage(driver);
}

public void openAssessmentTab()
{
LeadAssessmentTab.click();
}

public void leadsListtab()
{
LeadsListTab.click();
}

public String ValidateLeadsCurURL(WebDriver driver) {

TestBase.driver=driver;
return driver.getCurrentUrl();
}

public String GetLeadsCount() {
return totalNoOfLeads.getText();
}

public String GetFirstLeadFrmList() {
return FirstLeadLink.getText();

}

public void searchEditLead() {
String FirstID = FirstLeadLink.getText();
SearchIDNametxtbox.sendKeys(FirstID);
FilterSearchBtn.click();
ClearAllSrchFilter.click();

}

public void ValidateAddLeads(String fname, String lname, String email, String add1, String city, String zip,
String phone) {
Fname.sendKeys(fname);
Lname.sendKeys(lname);
Email.sendKeys(email);
IsCustEmailYes.click();
Add1.sendKeys(add1);
City.sendKeys(city);
Zip.sendKeys(zip);
Phone.sendKeys(phone);
SaveBtn.click();
}

}

LeadsPageTest.class

package com.sams.qa.testcases;

import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.core.qa.base.TestBase;
import com.core.qa.config.PropertyFile;
import com.sams.qa.pages.DashboardPage;
import com.sams.qa.pages.LeadsPage;
import com.sams.qa.pages.LoginPage;

public class LeadsPageTest extends TestBase {

LoginPage loginpg;
DashboardPage dashboardpg;
LeadsPage leadspage = new LeadsPage(driver);

@Test(priority = 4, groups = "smoke")
public void OpenLeadsPgTest() {

System.out.println("In Open leads pg method");

leadspage.openLeadspg();
leadspage.openAssessmentTab();
leadspage.leadsListtab();
System.out.println("Leads count is :" + leadspage.GetLeadsCount());
}

}

执行 testng.xml(套件)后,LeadsPageTest 会出现以下错误:-

java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy13.click(Unknown Source)
at com.sams.qa.pages.LeadsPage.openLeadspg(LeadsPage.java:121)
at com.sams.qa.testcases.LeadsPageTest.OpenLeadsPgTest(LeadsPageTest.java:31)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

最佳答案

我建议进行一些更改

  1. 初始化 pageObject 类中的元素,如下所示

    公共(public)LeadsPage(WebDriver驱动程序){ this.driver = 驱动程序;pageFactory.initElements(驱动程序,此); }

并从测试中删除 pageFactory 行。

  • 尝试在基类中将网络驱动程序声明为静态

    公共(public)静态WebDriver驱动程序;

  • 在测试中创建 pageobject 类的对象,例如 LeadsPage Leadspage = new Leadspage();

  • 关于java - 执行 TestNG 套件后显示错误 'java.lang.NullPointerException DefaultElementLocator.findElement(DefaultElementLocator.java:69)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61315598/

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