You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Cucumber测试时抛出io.cucumber.java.PendingException问题求助

解决Cucumber测试中的PendingException及登录点击问题

1. 核心问题:手动抛出的PendingException

你代码里的throw new io.cucumber.java.PendingException();是报错的直接原因——这是Cucumber专门用来标记未实现步骤的异常,只要这行代码存在,执行到对应步骤就会触发报错。

直接移除两个步骤方法里的这行抛出语句即可解决该异常。

2. 修复登录点击的等待逻辑

原代码中的driver.wait()是错误用法(这是Object类的线程等待方法,不是WebDriver的元素等待),会导致无法正确等待登录按钮加载,大概率引发NoSuchElementException。改用显式等待确保元素可点击后再操作。

修改后的完整代码

// 需导入的必要包
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;

// 确保driver是类级别的全局变量,让两个步骤方法都能访问
WebDriver driver;

@Given("Access the website")
public void access_the_website() {
    System.setProperty("webdriver.chrome.driver","C://Users//Jayalekshmi//Desktop//Automation//chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("你的实际网站URL"); // 替换为真实的网站地址
    System.out.println("heloo");
    // 移除抛出PendingException的语句
}

@Given("Click Login option")
public void click_login_option() {
    // 显式等待登录按钮加载完成并可点击,超时时间10秒
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    WebElement loginButton = wait.until(
        ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[1]/header/div/div/div[2]/div/div/a[1]"))
    );
    loginButton.click();
    System.out.println("Hiiii");
    // 移除抛出PendingException的语句
}

额外优化建议

  • 避免使用绝对XPath(/html/body/...),这种定位方式极易因页面结构变化失效,建议改用相对定位,比如By.linkText("登录")或By.cssSelector("header div > a:first-child")。
  • 可以把ChromeDriver的路径配置到系统环境变量中,避免代码里硬写路径。

内容的提问来源于stack exchange,提问作者Jayalekshmi Arumugham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 10:45:46