简体   繁体   English

如何使用 nightwatch 框架清除字段?

[英]How to clear field using nightwatch framework?

I'm trying to clear input field using different ways:我正在尝试使用不同的方式清除输入字段:

  1. clearValue() - doesn't work clearValue() - 不起作用
  2. send some combination ok keys - doesn't work发送一些组合确定键 - 不起作用

I have the following code which worked for me我有以下代码对我有用

.setValue('field, [
    browser.Keys.CONTROL,
    "a",
    browser.Keys.DELETE,
  ])

Have you tried to clear value, setting empty value ?您是否尝试清除值,设置空值? Like this像这样

.setValue('field', '')

Have you tried removing the Input with the Webriver-Key commands?您是否尝试过使用 Webriver-Key 命令删除输入?

In my Project i do it like this:在我的项目中,我这样做:

for (let i = 0; i < amountChars; i++) {
            browser.keys(WEBDRIVER_KEYS['Backspace']);
        }

WEBDRIVER_KEYS is part of the spectron-keys npm package. WEBDRIVER_KEYS 是 spectron-keys npm 包的一部分。

I´ve made a custom command like this:我做了一个这样的自定义命令:

 this.waitForElementVisible(fieldSelector)
        .execute(
            function (fieldSelector) {
                const field = document.querySelector(fieldSelector);
                field.value = null;
            },
            [fieldSelector]
        );

And then in the test file i just call it like this:然后在测试文件中,我只是这样称呼它:

browser.clearField(fieldSelector)

For me, this works like a charm:对我来说,这就像一个魅力:

browser.getValue('css selector', '#id', (textValue) => {
        for(let i = 0; i < textValue.value.length; i ++) {
            this.setValue('#id', '\u0008'); 
        }
});

Had to come up with this kind of a solution, since any of the provided ones didn't work for me.不得不想出这种解决方案,因为提供的任何解决方案都不适合我。

browser.clearValue('yourTargetFieldSelector')

所以在使用 Page 对象的情况下:

yourPageObjectInstance.clearValue('@yourTargetFieldSelector')

I really suffered with this one today, so I share my solution since none of the other solutions worked for me for chrome & firefox:我今天真的很受这个问题的困扰,所以我分享了我的解决方案,因为其他解决方案都不适用于 chrome 和 firefox:

exports.command = function (fieldSelector) {
  const browserName = this.options.desiredCapabilities.browserName;

  if (browserName === 'chrome') {
    this.getValue(fieldSelector, function (result) {
      this.setValue(fieldSelector, [...(result.value || '')].map(char => '\u0008'));
    });
  } else {
    this.clearValue(fieldSelector);
  }

  return this;
};

This is a command to do one thing for chrome and other one for firefox这是一个为 chrome 做一件事而为 Firefox 做另一件事的命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM