定位器错误无效

时间:2017-02-27 06:57:36

标签: javascript node.js selenium-webdriver cucumberjs es6-class

我试图用Page Object样式重写我的测试,但出了点问题。

我使用Class Tab,这是我的代码的一部分:

var World = require('../support/world.js');
const isAllAjaxRequests = require('../scripts/util').isAllAjaxRequests;
const isElementLocatedAndVisible = require('../scripts/util').isElementLocatedAndVisible;

module.exports.Tab = class Tab {

    constructor(data) {
        this.name = "Base";
        this.locators = {
            'nextStepIsLocked': {xpath: '//md-tab-item[@aria-selected="true"]//div[@class="cc-status red"]'},
            'isActiveTab': {xpath: '//md-tab-item[@aria-selected="true"]//span[text()="'+ data + '"]'}
        }
    }

    waitForElement(bySelector) {
        var driver = World.getDriver();
        var self = this;
        //var bySelector = self.locators[bySelector];

        return driver.wait(isAllAjaxRequests(driver), waitTimeOut).then(() => {
            //console.log(bySelector)
            return driver.wait(isElementLocatedAndVisible(bySelector), waitTimeOut);
        });
    }

    tabIsOpen(tabName) {
        var driver = World.getDriver();
        var self = this;
        var bySelector = By.xpath('//md-tab-item[@aria-selected="true"]//span[text()="'+ tabName + '"]');

        return self.waitForElement(bySelector);
    }
}

util中的代码:

exports.isElementLocatedAndVisible = function isElementLocatedAndVisible(driver, bySelector) {
    return new Condition('element is located and visible', function(driver) {
        console.log(bySelector)
        return driver.findElements(bySelector).then((arr) => {
            if (arr.length > 0) {
                return arr[0].isDisplayed();
            }
            else {
                return false;
            }
        });
    });
};

我试图在我的测试中使用:

    this.Then(/^Tab "([^"]*)" is open$/, function (tabName) {
        this.createTab(tabName);
        //var bySelector = tab.getLocator(isActiveTab);

        return tab.tabIsOpen(tabName);
});

但我收到了Invalid Locator error。 通过调试打印我看到当代码转到bySelector函数时,我想念exports.isElementLocatedAndVisible值。这是undefiened。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

我怀疑它只是缺少引起问题的参数。

在以下行中:

return driver.wait(isElementLocatedAndVisible(bySelector), waitTimeOut);

driver对象添加为第一个参数,然后添加bySelector,如下所示:

return driver.wait(isElementLocatedAndVisible(driver, bySelector), waitTimeOut);

函数定义如下:

function isElementLocatedAndVisible(driver, bySelector)

所以,期待driver对象以及bySelector