即使不可能,CucumberJS也会测试通过

时间:2017-08-18 11:11:38

标签: selenium phantomjs cucumberjs

我正在尝试将一些旧的ruby测试(使用黄瓜,phantomjs和capybara)转换为JavaScript(使用黄瓜,幻影和硒),因为我的项目是基于100%节点的,我想删除Ruby依赖项。 / p>

当我运行测试时,它们都通过了。问题是,我没有创造测试通过的条件,所以传递是不可能的。我不确定我哪里出错了。

这是我的world.js文件:

var {defineSupportCode} = require('cucumber');
var seleniumWebdriver = require('selenium-webdriver'),
    By = seleniumWebdriver.By,
    until = seleniumWebdriver.until;

function CustomWorld() {
  this.driver = new seleniumWebdriver.Builder()
  .withCapabilities(seleniumWebdriver.Capabilities.phantomjs())
  .build()

  // Returns a promise that resolves to the element
  this.waitForElement = function(locator) {
    var condition = seleniumWebdriver.until.elementLocated(locator);
    return this.driver.wait(condition)
  }
}

defineSupportCode(function({setWorldConstructor}) {
  setWorldConstructor(CustomWorld)
});

这是我的步骤定义文件:

require('dotenv').config();
var chalk = require('chalk');
var {defineSupportCode} = require('cucumber');
var seleniumWebdriver = require('selenium-webdriver'),
    By = seleniumWebdriver.By,
    until = seleniumWebdriver.until;

defineSupportCode(function({ Given, When, Then }) {

  Given(/^I show my environment$/, function (next) {
    console.log(chalk.green("Running against:" + process.env.TARGET_URI))
    next()
  })

  When(/^I visit "(.*?)"$/, function (url) {
    return this.driver.get(url);
  })

  Then(/^I should be on "([^"]*)"$/, function(page_name) {
    this.driver.get(process.env.TARGET_URI+'/'+page_name)
    .then(function() {
      return this.driver.getCurrentUrl();
    })
  })

  Then(/^I should see "([^"]*)"$/, function (text) {
    var xpath = "//*[contains(text(),'" + text + "')]";
    var condition = seleniumWebdriver.until.elementLocated({xpath: xpath});
    return this.driver.wait(condition, 5000);
  });
})

唯一可能通过的测试包括:When(/^I visit "(.*?)"$/...Given(/^I show my environment$/...

作为参考,这里也是我的.feature文件:

Feature: Test the global header works as expected

Scenario: Header components should exist
    Given I visit "/hello"
    Then I expect to see a ".c-logo-bar" element
    And I expect to see a ".c-search-bar" element
    And I expect to see a ".c-main-nav-bar" element

我出错的任何想法?

0 个答案:

没有答案