【问题标题】:Not able to add a custom command无法添加自定义命令
【发布时间】:2020-05-05 13:51:32
【问题描述】:

我想在 typescript webdriverIO 项目中创建一个自定义命令。但无论我做什么,命令总是以错误结束:

TypeError: browser.waitAndClick 不是函数。

基本上我想添加他们在 webdriverIO 文档中提到的相同功能。我在我的规范中从 beforeAll() 中添加它。

import { DEFAULT_TIMEOUT } from "../constants";
class CustomCommand {
    private static alreadyAdded = false;
    static addCommands(){
        if(!this.alreadyAdded) {
            browser.addCommand('waitAndClick', (el: WebdriverIO.Element) => {
                el.waitForDisplayed({timeout: DEFAULT_TIMEOUT});
                el.click();
            }, true);

            browser.addCommand('waitAndSetValue', (el: WebdriverIO.Element, text: string) => {
                el.waitForDisplayed({timeout: DEFAULT_TIMEOUT});
                el.setValue(text);
            }, true);

            this.alreadyAdded = true;
        }
    }
}

export default CustomCommand;

我正在从规范的 beforeAll() 中调用这个 addCommands() 函数。但没有运气!

【问题讨论】:

    标签: webdriver-io


    【解决方案1】:

    来自 slack 频道的一位好人帮助我找出了确切原因。实际上我忽略了文档中的一些内容:If you register a custom command to the browser scope, the command won’t be accessible for elements. Likewise, if you register a command to the element scope, it won’t be accessible in the browser scope。原来这就是原因。现在已经解决了。

    false 作为addCommand() 中的第三个参数传递已修复。

    【讨论】:

      【解决方案2】:

      欢迎来到堆栈溢出!

      请注意,根据文档here,webdriverio 中没有“beforeAll”挂钩。 如果你在钩子之前调用它应该可以工作。

      【讨论】:

        【解决方案3】:

        基于 webdriverio 文档:https://webdriver.io/docs/api/browser/addCommand/

        注意:不要忘记在钩子之前包裹里面,如下所示:

        before: async function (capabilities, specs) {
        
                browser.addCommand('waitAndClick', async function (selector) {
                    try {
                        await $(selector).waitForExist();
                        await $(selector).click();
                    } catch (error) {
                        throw new Error(`Could not click on selector: ${selector}`);
                    }
                });
        
        
            },
            
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-16
          • 1970-01-01
          • 2010-12-11
          • 2018-03-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多