在发现元素可见后,无法使用递归定位元素

时间:2016-05-09 09:49:06

标签: javascript recursion automated-tests nightwatch.js

我的问题:

我正在尝试使用Nightwatch的下拉列表中的选项,使用页面对象中的部分。我不确定这部分声明是否有问题,或者我错过了与范围相关的内容。问题是它发现元素是可见的,但是当它试图单击它时会抛出错误,它无法使用递归来定位它。

我可以尝试使用部分来解决此问题吗?

在测试中:

var myPage = browser.page.searchPageObject();
var mySection = searchPage.section.setResults;

// [finding and clicking the dropdown so it opens and displays the options]
browser.pause (3000);

browser.expect.section('@setResults').to.be.visible.before(1000);
myPage.myFunction(mySection, '18');

在页面对象中:

var searchKeywordCommands = {
    myFunction: function (section, x) {
        section.expect.element('@set18').to.be.visible.before(2000);
        if (x == '18') section.click('@set18');
        //[...]
};

module.exports = {
    //[.. other elements and commands..]
    sections: {
        setResults: {
            selector: '.select-theme-result', //have also tried with '.select-content' and '.select-options' but with the same result
            elements: {
                set18: '.select-option[data-value="18"]',
                set36: '.select-option[data-value="36"]' //etc

            }}}}

这是我的源代码: enter image description here

当我运行这块核心时,它似乎找到了该部分,发现元素可见(我也可以清楚地看到它打开下拉列表并显示选项)但是当试图点击任何选项时,我得到了错误: ERROR: Unable to locate element: Section[name=setResults], Element[name=@set18]" using: recursion

这是完整的错误: enter image description here

我的尝试:

我试图将set18选择器声明为单个元素而不是部分内部,并且所有内容都可以正常工作,但不会在该部分内部工作。我也尝试了所有可用的选择器来定义部分的选择器,但它不适用于任何选择器。

1 个答案:

答案 0 :(得分:0)

这就是我正在做的事情(LOL) 我假设步骤是(找到dropbox - 点击dropbox - 选择值)。

var getValueElement = {
        getValueSelector: function (x) {
            return 'li[data-value="'+ x + '"]';
        }
}; 

module.exports = {
    //[.. other elements and commands..]
    sections: {
        setResults: {
            commands:[getValueElement],
            selector: 'div[class*="select-theme-result"', //* mean contains,sometime class is too long and unique,also because i am lazy.
            elements: {
                setHighlight:'li[class*="select-option-highlight"]',
                setSelected:'li[class*="select-option-selected"]', 
                //set18: 'li[data-value="18"]',
                //set36: 'li[data-value="36"]' 
                // i think getValueFunction is better,what if you have 100+ of set.

            }}}}

在你的考试中

var myPage = browser.page.searchPageObject();
var mySection = searchPage.section.setResults;

// [finding and clicking the dropdown so it opens and displays the options]
mySection
     .click('@dropboxSelector')
     .waitForElementVisible('@setHighlight',5000,false,
          function(){
            var set18 = mySection.getValueElement(18);
            mySection.click(set18);
            });

Ps:在我的情况下(我认为你的情况也是如此),dropbox或在你的web应用程序中多次使用的任何小型第三方js框架,所以最好为它创建一个不同的PageObject,使pageObject / section很简单尽可能。