清除 textarea 或文字輸入元素的值。

clearValue() 命令將會自動等待元素出現 (直到指定的 timeout)。如果找不到元素,則會拋出錯誤,導致測試失敗。您可以透過將 selector 參數指定為物件,並傳遞 suppressNotFoundErrors = true 選項來抑制找不到元素的錯誤。

有關在 Nightwatch 中使用 DOM 元素的更多資訊,請參閱尋找 & 與 DOM 元素互動 指南頁面。

用法

                    browser.clearValue('<SELECTOR>', function (result) { }])
                
                    // using global element()
browser.clearValue(element('<SELECTOR>'))
                

範例

module.exports = {
  demoTest(browser) {
    browser.clearValue('#login input[type=text]');

    browser.clearValue('#login input[type=text]', function(result) {
      console.log('clearValue result', result);
    });

    // with explicit locate strategy
    browser.clearValue('css selector', '#login input[type=text]');

    // with selector object - see https://nightwatch.dev.org.tw/guide/writing-tests/finding-interacting-with-dom-elements.html#postdoc-element-properties
    browser.clearValue({
      selector: '#login input[type=text]',
      index: 1,
      suppressNotFoundErrors: true
    });

    browser.clearValue({
      selector: '#login input[type=text]',
      timeout: 2000 // overwrite the default timeout (in ms) to check if the element is present
    });
  }
}

參數

名稱 類型 描述
using
可選
字串

要使用的定位策略。請參閱 W3C Webdriver - 定位策略

selector 字串 | 物件

用於定位元素的選擇器 (CSS/Xpath)。可以是字串或指定 元素屬性的物件。

callback
可選
函式

命令完成時要呼叫的可選回呼函式。

W3C WebDriver 規範