我试图以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
我做错了什么?

最佳答案

我怀疑只是缺少导致问题的参数。

在以下行中:

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


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

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


函数定义如下:

function isElementLocatedAndVisible(driver, bySelector)


因此,期望driver对象与bySelector

关于javascript - 无效的定位器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42479551/

10-09 21:10