Nightwatchjs: how to check if element exists without creating an error/failure/exception

前端 未结 4 1858
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 15:11

In the page I\'m testing, two buttons may be displayed: BASIC or ADVANCED.

I want to be able to tell if the ADVANCED button is showing -- and if so, click it.

<
4条回答
  •  走了就别回头了
    2021-01-01 15:42

    My team uses a single function to authenticate with a few different signin forms, and we utilize a custom command called ifElementExists to accomplish the branching logic for understanding which form we're on. We also use this on a few other pages that don't have a better method for determining current state.

    import { CustomCommandShorthand } from './customCommands';
    import { isFunction } from 'lodash';
    
    exports.command = function ifElementExists(this: CustomCommandShorthand, selector: string, ifTrue: Function, ifFalse?: Function) {
        this.perform(() => {
            if (!isFunction(ifTrue)) {
                throw new Error(`The second argument must be callable. You passed a ${typeof ifTrue} instead of a function.`);
            }
    
            this.element('css selector', selector, function ifElementExistsCallback({ status }) {
                if (status !== -1) {
                    return ifTrue();
                }
    
                if (isFunction(ifFalse)) {
                    ifFalse();
                }
            });
        })
    }
    

提交回复
热议问题