gpt4 book ai didi

java - 特征文件的并行执行导致控制台上打印出空指针异常

转载 作者:行者123 更新时间:2023-12-04 07:23:53 25 4
gpt4 key购买 nike

我试图对功能文件的 cucumber 并行执行进行编码,但是在控制台上打印出一个错误,如下所示:

[TestNG-PoolService-0] ERROR com.nicholas.StepsDef.ExportProduct - java.lang.NullPointerException

Element info: {Using=xpath, value=//a[@class='log-wrapper']/child::img}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
at com.nicholas.StepsDef.AddProduct.navigateToManageProduct(AddProduct.java:65)
at ✽.navigate to manage product(file:///C:/Users/nicholaswkc/IdeaProjects/cucumber-java-skeleton/src/test/resources/Features/AddProduct.feature:7)

05:38:22.730 [TestNG-PoolService-0] ERROR com.nicholas.StepsDef.ExportProduct - java.lang.NullPointerException
05:38:22.780 [TestNG-PoolService-0] ERROR com.nicholas.StepsDef.AddProduct - org.openqa.selenium.NoSuchSessionException: invalid session id
添加ProductPageObject.java
public By lazadaLogo = By.xpath("//a[@class='log-wrapper']/child::img");
添加产品.java
public class AddProduct {

private AddProductPageObject page;
private ChromeDriver driver;
private Logger log = LogManager.getLogger(AddProduct.class);
// ======================================================================
public AddProduct() {
}

@Given("Launch the homepage and login")
public void launchTheHomepageAndLogin() {
log.info("Start Login");
try {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
WebDriverWait timeWait = new WebDriverWait(driver, 30);
page = PageFactory.initElements(driver, AddProductPageObject.class);
driver.navigate().to("https://sellercenter.lazada.com.my/apps/seller/login");
timeWait.until(ExpectedConditions.visibilityOfElementLocated(page.getLazadaSellerLogo()));

// Input username
driver.findElement(page.getUsername()).click();
driver.findElement(page.getUsername()).clear();
driver.findElement(page.getUsername()).sendKeys("nicholaswkc34@gmail.com");

// Input password
driver.findElement(page.getPassword()).click();
driver.findElement(page.getPassword()).clear();
driver.findElement(page.getPassword()).sendKeys("wlx_+279295");

// Click submit btn
driver.findElement(page.getSignInButton()).click();

//assertThat(page.getPageTitle()).isEqualto("");

Wait wait = new Wait();
wait.implicitWait(driver, 5);

} catch (Exception e) {
log.error(e);
}
}

@Given("navigate to manage product")
public void navigateToManageProduct() {
WebDriverWait waitProductLink = new WebDriverWait(driver, 30);
waitProductLink.until(ExpectedConditions.visibilityOfElementLocated(page.getLazadaLogo()));
driver.findElement(page.getProductLink()).click();

WebDriverWait waitManagedProductLink = new WebDriverWait(driver, 30);
waitManagedProductLink.until(ExpectedConditions.visibilityOfElementLocated(page.getManageProductLink()));
driver.findElement(page.getManageProductLink()).click();
}
此外,当我使用 mvn 命令运行时,它会弹出 org.testng.xml.XmlSuite.setParallel(Ljava/lang/String;)在此突出显示 group
问题:
  • 导致错误的原因是什么?
  • 当 maven surefire 插件错误应该修复?
  • 最佳答案

    我在“at com.nicholas.StepsDef.AddProduct.navigateToManageProduct(AddProduct.java:65)”中看到了一个错误,方法page.getLazadaLogo()。
    我假设在创建 WebDriverWait waitProductLink = new WebDriverWait(driver, 30); 时驱动程序对象为 null目的。通常,WebDriverWait 在其构造函数参数中接受 null,但是当我们尝试使用使用 null 创建的等待对象时,它会在运行时抛出错误。
    解决方案:

  • 检查驱动程序在步骤“导航到管理产品”实现中包含的内容。特定于该特定块的“启动主页和登录”驱动程序分配的范围。为了克服这个问题,在步骤“启动主页和登录”实现中将驱动程序变量设为静态并为其分配驱动程序对象引用。
  • 如果这两个步骤并行运行,那么您还需要在步骤“导航到管理产品”实现中创建驱动程序对象。
  • 关于java - 特征文件的并行执行导致控制台上打印出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68322890/

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