如何在全局命令文件中访问API?

问题描述 投票:0回答:1

当我在不具有全局命令功能的页面object_file中执行此操作时,我想在nightwatch.js框架中创建一个全局命令

navigateWithNav() {
            return navigateWithNavToolbar.call(this, "#home-nav")
        },

一切正常。但是,当我尝试更改对象_file中的函数时,在全局命令上,我将无法获得this.api的定义,如何解决它?

// page_oject file
        navigateWithNav() {
            return this.navigateWithNavToolbar("#home-nav") 
        },


// global command file

const { client } = require("nightwatch-cucumber")

const { MID_TIMEOUT } = global.config.timeouts

exports.command = async function navigateWithNavToolbar(selector) {
    return this.api.url(async (url) => {
        // if we are someplace which doesnt have the nav toolbar, then
        // goto the homepage
        if (!url.value.includes(client.launch_url)){
            await client.url(client.launch_url)
        }
        await this.api.waitForElementPresent(selector, MID_TIMEOUT, false)
        await this.api.click(selector)
    })
}
javascript cucumber e2e-testing nightwatch.js
1个回答
0
投票

我不知道page_oject文件代码所使用的语法,但是您想要执行以下操作来绑定this

navigateWithNav = () => this.navigateWithNavToolbar("#home-nav") 
© www.soinside.com 2019 - 2024. All rights reserved.