如何使用Javascript在另一个函数中使用参数

时间:2017-08-18 13:45:09

标签: javascript selenium-webdriver gruntjs protractor cucumber

我正在使用Protractor和Cucumber进行自动化。我有一个场景,我需要在函数B中使用函数A的输出。在不使用黄瓜的正常方式中,我能够使用then扩展函数。但是使用黄瓜我无法做到这一点。

有人可以建议如何使用黄瓜。

功能A

Then('abc', function (done) {
  search.cityCountyDropDown.click();
  search.cityCounty.count().then(function(count) {
    search.cityCountyCount = count;
    search.randomCityCountyIndex = (
      1 + Math.floor(Math.random() * (search.cityCountyCount - 1))
    );
  })
  .then(function() {
    search
      .cityCounty
      .get(search.randomCityCountyIndex)
      .getText()
      .then(function(cityCountyLabel) {
        search.selectCityCountyLabel(cityCountyLabel);
        console.log(cityCountyLabel);
        done();
      });
  });
});

功能B

Then('defij', function (done) {
  console.log(cityCountyLabel);
  done();
});

我想在功能B中使用cityCountyLabel

这是我的pageObject.js文件。

var SearchPage = function () {
    this.Category = element(by.css('[href="/abc/"]'));
    this.Header = element(by.id('abc'));

    this.cityCountyDropDown = element(by.id('abc'));
    this.cityCounty = element.all(by.xpath("//*[@id='abc']/dd/ul/li"));

    this.cityCountyCount;
    this.randomCityCountyIndex;

    this.selectCityCountyLabel = function (cityCountyLabel) {
        element(by.xpath(".//*[@id='abc']/dd/ul/li[text()='" + cityCountyLabel + "']")).click();
    };


    this.submitButton = element(by.css('input.btn-search'));
    this.searchResults = element.all(by.xpath("//*[@id='abc']/tbody/tr/td[1]/div[@class='abc']"));

    this.searchResultsCount;
    this.randomSearchResultsIndex;

    this.randomSearchResult = function (randomSearchResultsIndex, searchResult) {
        return element(by.xpath(".//*[@id='abc']/tbody/tr/td[1]/div[@class='abc'][" + randomSearchResultsIndex + "]/div[1]/h2/a"));
    }

    this.addressRegex;
}

module.exports = new SearchPage();

1 个答案:

答案 0 :(得分:0)

在黄瓜中,您可以使用World在步骤之间传递数据,这些步骤在this中可用。示例代码:

Then('abc', function (done) {
     this["cityCountyLabel"] = "data";
     done();
});

Then('defij', function (done) {
     // this will print out: "data"
     console.log(this["cityCountyLabel"]);
     done();
});