【问题标题】:How to set value into textarea attribute using nightwatch.js如何使用 nightwatch.js 将值设置为 textarea 属性
【发布时间】:2016-05-07 03:42:12
【问题描述】:

我正在为 web ui 测试使用 nighwatch.js,我想为 textarea 设置值,而 textarea 有一个包含我的实际文本的属性,我正在编写完整的 textarea 以及我如何设置以下内容。

<div class="textarea-description">
    <textarea cols="50" class="myClass setText" data-def placeholder="text to be replaced using nightwatch"/>
</div>

我正在尝试通过以下方式在 textarea 的属性 data-def placeholder 中设置值

browser.setValue('.textarea-description textarea[type=text]','nightwatch'); or
browser.setValue('.textarea-description textarea[data-def placeholder=text]','nightwatch'); or
browser.setValue('.textarea-description textarea[type=data-def placeholder]','nightwatch');

但没有任何效果。

【问题讨论】:

    标签: javascript jquery ui-automation ui-testing nightwatch.js


    【解决方案1】:

    这可能不是最好的解决方案,但它确实有效:

    browser
    .execute("document.querySelector('.textarea-description .myClass').setAttribute('placeholder', 'nightwatch');")
    

    如果你有 jQuery,你可以让它更好一点:

    browser
    .execute("$('.textarea-description .myClass').attr('placeholder', 'nightwatch');")
    

    【讨论】:

    • 你尝试过 JavaScript 和 jQuery 吗?
    • 不,我只尝试了第一个。我会尝试第二个并告诉你。
    • 您的 textarea 没有结束标签(请参阅我对您的问题所做的编辑),我认为这也可能是一个问题。
    【解决方案2】:

    你可以使用 xpath 来获取属性。

    .useXpath().setValue('//textarea[contains(@placeholder,'text to be replaced using nightwatch')]@placeholder', 'nightwatch')
    

    How to select specified node within Xpath node sets by index with Selenium?

    【讨论】:

      【解决方案3】:

      感谢您提出的所有宝贵建议,您提供的所有建议都能够提供很好的知识,但不幸的是,这些建议都没有奏效。我已经通过以下方式解决了。

      client.setValue('.textarea-description textarea','new text to be write.');
      

      实际上属性“data-def placeholder”只使用了不是实际文本的水印,所以它正在工作。

      【讨论】:

      • 那我误解了你的问题。这不是设置属性,而是设置文本区域中的直接值?
      【解决方案4】:

      这对我有用。

      .assert.visible('div.textarea-description textarea.setText')
      .moveToElement('div.textarea-description textarea.setText', null, null)
      .mouseButtonClick('left')
      .keys([browser.Keys.CONTROL, "a"])
      .keys([browser.Keys.CONTROL, "nightwatch"])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多