如何在 Cucumber 中重复使用表达式?

问题描述 投票:0回答:1

我有这样的场景,我想检查每个页面上是否存在横幅元素。

我创建了一个单独的

banners.js
文件,其中包含执行检查的 (Selenium Webdriver) Javascript:

./components/banner.js

const { By } = require('selenium-webdriver');

async function checkBannerExists(webdriver, bannerClass) {
    var banner = await webdriver.driver.findElement(By.className(bannerClass)).getRect();
    return banner.height;
  };

module.exports = { checkBannerExists };

当我需要它时,我可以简单地在步骤的文件表达式中调用它:

const banners = require("../../components/banners");

Given('I am on the home page',  async function () {
    this.driver = new Builder()
        .forBrowser('firefox')
        .build();
    
    this.driver.wait(until.elementLocated(By.tagName('h1')));
    await this.driver.get('https://www.awebsite.com');
});

Then('there should be a banner', async function() {
    var homeBanner = await banners.checkBannerExists(this, 'banner');
    assert.ok(homeBanner!==null);
});

那里一切都好。

但是如果我将相同的表达式添加到另一个页面,例如:

const banners = require("../../components/banners");

Given('I am on the about us page',  async function () {
    this.driver = new Builder()
        .forBrowser('firefox')
        .build();
    
    this.driver.wait(until.elementLocated(By.tagName('h1')));
    await this.driver.get('https://www.awebsite.com/about');
});

Then('there should be a banner', async function() {
    var homeBanner = await banners.checkBannerExists(this, 'banner');
    assert.ok(homeBanner!==null);
});

我收到错误

Multiple step definitions match:

有没有办法可以在多个步骤和特征文件中重复使用相同的表达式?

javascript selenium-webdriver automated-tests cucumber cucumberjs
1个回答
0
投票

我最终使用

Scenario Outline
采取了不同的方法,其中使用相同的
.feature
文件并为其提供多个页面:

genericBannerTest.feature

Feature: Generic Banner Tests
 
    Scenario Outline: Run a check for generic banner
        Given I am on the '<page>' page
        Then there should be a banner on the '<page>' page

    Examples: 
        | page     |
        | /        |
        | /contact |
        | /about   |
        | /products|

genericBannerTestSteps.js

const banners = require("../components/banners");
// other required stuff

Given('I am on the {string} page', async function (string) {
    this.driver = new Builder()
        .forBrowser('firefox')
        .build();
    
    this.driver.wait(until.elementLocated(By.tagName('h1')));
    await this.driver.get('https://www.awebsite.com' + string);
});

Then('there should be a banner on the {string} page', async function(string) {
  let bannerClass = 'banner-module'
  if (string == '/') {
    bannerClass = 'home-banner';
  }
  else if (string == '/product-banner') {
    bannerClass = 'prod-short';
  }

  var banner = await banners.checkBannerExists(this, bannerClass);
  assert.ok(banner!==null);
});

banner.js

const { By } = require('selenium-webdriver');

async function checkBannerExists(webdriver, bannerClass) {
  var banner = await webdriver.driver.findElement(By.className(bannerClass)).getRect();
  return banner.height;
};

module.exports = { checkBannerExists };
© www.soinside.com 2019 - 2024. All rights reserved.