WebdriverIO+Cucumber带参数测试用例无故跳过问题求助
WebdriverIO + Cucumber 带参数的测试用例无故跳过
我在用WebdriverIO结合Cucumber开展自动化测试时,遇到测试用例被无故跳过的问题。虽存在部分废弃内容提示,但我认为这并非问题根源。我已调整逗号格式、更新chromedriver版本并配置了指定的capabilities,其中打开网站的首个测试可正常运行,但带参数的测试用例始终被跳过。相关配置与代码如下:
package.json
{ "name": "webdriveriotest", "version": "1.0.0", "description": "", "main": "index.js", "directories": { "test": "tests" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "wdio": "wdio run ./wdio.conf.js" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@wdio/cli": "^8.15.10", "@wdio/cucumber-framework": "^8.15.10", "@wdio/local-runner": "^8.15.10", "@wdio/spec-reporter": "^8.15.10" }, "dependencies": { "chromedriver": "^115.0.1" } }
wdio.conf.js 相关配置
capabilities: [ { browserName: 'chrome', 'goog:chromeOptions': { args: ['--disable-extensions'], // Added --headless for headless mode }, acceptInsecureCerts: true, browserVersion: "stable", }, // { // browserName: 'firefox', // }, ], baseUrl: 'https://www.demoblaze.com', cucumberOpts: { // <string[]> (file/dir) require files before executing features require: [//'./features/step-definitions/steps.js' './features/step-definitions/*.steps.js' ], // <boolean> show full backtrace for errors backtrace: false, // <string[]> ("extension:module") require files with the given EXTENSION after requiring MODULE (repeatable) requireModule: [], // <boolean> invoke formatters without executing steps dryRun: false, // <boolean> abort the run on first failure failFast: false, // <boolean> hide step definition snippets for pending steps snippets: true, // <boolean> hide source uris source: true, // <boolean> fail if there are any undefined or pending steps strict: false, // <string> (expression) only execute the features or scenarios with tags matching the expression tagExpression: '', // <number> timeout for step definitions timeout: 60000, // <boolean> Enable this config to treat undefined definitions as warnings. ignoreUndefinedDefinitions: true },
dashboard.feature
Feature: Dashboard Scenario: Dashboard page should have the 'STORE' title Given I open 'Dashboard' page Then Page title should be equal to 'STORE'
validation-steps.js
const {Then} = require("@wdio/cucumber-framework"); const compareText = require("./utils/compre-text"); const dashboard = require('../../po/pages/dashboard.page'); Then('Page title should be equal to {string}', async function(titleText){ await browser.pause(200); const pageTitle = await dashboard.getMainPageTitle(); return expect(pageTitle).toEqual(titleText); });
actions.step.js
const { Given, When, Then } = require('@wdio/cucumber-framework'); const page = require('../../po/pages/dashboard.page'); Given('I open {string} page', async function(pageName) { //return page(pageName).openMainPage(); return await page.openMainPage(); });
排查与解决步骤
- 修正步骤定义与Feature的参数匹配规则
Cucumber默认的{string}匹配器仅识别双引号包裹的字符串,你Feature文件中使用单引号'Dashboard'会导致步骤匹配失败,进而被标记为跳过。可二选其一修正:
- 将Feature中的单引号改为双引号:
Given I open "Dashboard" page - 修改步骤定义的正则以兼容单引号:
Given('I open \'([^\']*)\' page', async function(pageName) { return await page.openMainPage(); });
开启Cucumber严格模式定位问题
当前wdio.conf.js中strict: false会将未匹配的步骤标记为pending而非直接报错,建议临时设置strict: true,这样运行时会直接抛出未定义步骤的错误,快速定位匹配问题。验证步骤定义文件的加载路径
确认./features/step-definitions/*.steps.js路径是否正确,若存在路径歧义可改为明确指定文件:
require: [ './features/step-definitions/actions.step.js', './features/step-definitions/validation-steps.js' ]
- 检查Page Object方法的异常情况
确保dashboard.page.js中的openMainPage()和getMainPageTitle()方法逻辑正常,无未捕获的异常——方法内部报错也可能导致步骤被跳过。
内容的提问来源于stack exchange,提问作者Jhon Hernandez
相关产品推荐
相关产品推荐

