简体   繁体   中英

How to clear field using nightwatch framework?

I'm trying to clear input field using different ways:

  1. clearValue() - doesn't work
  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?

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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