1

嗨,我是 javascript 和 Nightwatch 的新手,我是一名手动测试人员,大约 6 个月前开始进行自动化测试。我正在编写测试用例来检查产品的详细信息,并带有可折叠的菜单。按 + 按钮将打开并显示一个元素列表,当使用相同按钮关闭时,它会关闭列表,并显示一个计数器,其中包含列表中的项目数。

我有一个正确执行此过程的函数,但我已将其写在测试中。我想在拥有与该页面相关的所有元素和功能的页面中使用它。我想从测试中调用该函数。我已经能够做到这一点,但对于嵌套函数的情况却不行,因为我不知道如何编写它。

这些是我的页面:

   loginPage.js;
   productPage.js;
   productFuntionalityListPage.js;

这是我的测试:

module.exports = {
    'Buy a Product with Bank Account': function (browser) {
        const login = browser.page.loginPage();
        const productList = browser.page.productPage();
        const productFunctionalityList = browser.page.productFuntionalityListPage();
        


        login
            .navigate()
            .checkLoginPage();

        productList
            .getAProduct()


        //------------------------------------------Features--------------------------------------

        //function to click on each button for functionalities and wait for list to appear
        function displayFunctionsList(elems) {
            elems.value.forEach(function (element) {
                browser.elementIdClick(element.ELEMENT)
                    //wait for list to appear
                    .waitForElementVisible('.list_of_items')
                    .pause(2000)
            })
        }

        // click on each function and wait for list to appear
        browser.elements('css selector', '.expand_collapse_btn', displayFunctionsList, 5000)

        browser.useCss()

        // close each function
        function closeFunctionsList(elems) {
            elems.value.forEach(function (element) {
                browser.elementIdClick(element.ELEMENT)
                    //after click close wait for count to appear
                    .waitForElementVisible("input[data-id='counter']")
                    .pause(2000)
            })
        }

 browser.elements('css selector', '.expand_collapse_btn', closeFunctionsList, 2000)

browser.end()

}
}

这工作正常。

下面是我尝试过但不起作用的内容:

页:

productFunctionalityListPage.js

module.exports = {

    elements: {

        counterOfItemsInList: {
            locatorStrategy: 'css selector'
            selector: "input[data-id='counter']",
        },

        expandCollapseBtn: {
            locateStrategy: 'css selector',
            selector: '.expand_collapse_btn',
        },
       listOfItems: {
            locateStrategy: 'css selector',
            selector: '.list_of_items',
        }
  },

commands: [{
        displayFunctionsList: function () {
            function displayFunctionsList(elems) {
                elems.value.forEach(function (element) {
                    this.elementIdClick(element.ELEMENT)
                        //wait for list to appear
                        .waitForElementVisible('@listOfItems')
                        .pause(2000)
                })
            }

            this.elements('css selector', '@expandCollapseBtn', displayFunctionsList, 5000)

        },

 closeFunctionsList: function () {
            function closeFunctionsList(elems) {
                elems.value.forEach(function (element) {
                    this.elementIdClick(element.ELEMENT)
                        //wait for list to appear
                        .waitForElementVisible('@counterOfItemsInList')
                        .pause(2000)
                })
            }

            this.elements('css selector', '@expandCollapseBtn', closeFunctionsList, 5000)

        }


    }]
}

从页面测试调用函数:

module.exports = {
    'Buy a Product with Bank Account': function (browser) {
        const login = browser.page.loginPage();
        const productList = browser.page.productPage();
        const productFunctionalityList = browser.page.productFuntionalityListPage();
     


        login
            .navigate()
            .checkLoginPage();

        productList
            .getAProduct()


        //------------------------------------------Features--------------------------------------

         //calling displayFunctionsList from productFuntionalityListPage.js
         productFunctionalityList.displayFunctionsList()
         
         //calling closeFunctionsList from productFuntionalityListPage.js
         productFunctionalityList.closeFunctionsList()
         


browser.end()

}
}

运行上述测试后的结果:


Error:
TypeError: this.elements is not a function
   - writing an ES6 async test case? - keep in mind that commands return a Promise; 
   - writing unit tests? - make sure to specify "unit_tests_mode=true" in your config.

谁能帮我在 productFuntionalityListPage.js 中将这些函数添加为自定义命令并从测试本身调用这些函数?不知道出了什么问题,因为我缺乏 javascript 和守夜知识。

4

1 回答 1

0

Try passing browser as a variable when calling the function like this -

##Test page##
//Example call

gmail.selectEmail(browser, 'browser authentication')

And then the method in the pageObject -

##Page Object##
//Example Method
selectEmail(browser, searchValue){
     browser.blah(searchValue);
     browser.blah
     browser.blah
};

Its slightly messy way of getting it to work but this has saved my bacon a few times

于 2021-08-26T14:54:19.300 回答