3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nightwatch clearValueが動かない

Last updated at Posted at 2020-03-11

環境

  • node:10.15.3
  • nightwatch: 1.3.4
  • chromedriver: 80.0.1

前提

  • Nightwatchでフォームへの入力を模すにはsetValue()を使う
  • 既にフォームに何らかの値が入力されている場合、既存の値に付け加えるかたちで入力されてしまう

setValue does not clear the existing value of the element. To do so, use the clearValue() command.

問題

clearValue()が動かない。
issueにあがっていた以下の対策を試してみたがダメだった。

解決法

最終的にこの方針に落ち着いた。

ただsetValue()にRIGHT_ARROWとBACK_SPACEを渡すとエラーが出て自分の環境では動かなかった。

そのため、setValue()の代わりにbrowser.keys(key)を用いてボタン操作をしてやることで期待する操作をすることができた。

削除コマンド
const clearCommand = {
  clear: function(browser, locator) {
    return browser
      .waitForElementVisible(locator, 1000)
      .click(locator)
      .getValue(locator, function(result) {
        const length = result.value.length;

        for (let i = 0; i < length; i++) {
          browser.keys(browser.Keys.RIGHT_ARROW);
        }

        for (let i = 0; i < length; i++) {
          browser.keys(browser.Keys.BACK_SPACE);
        }
      });
  }
};
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?