如何使用nightwatch.js将值设置为textarea属性

时间:2016-01-29 11:54:06

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

我正在使用nighwatch.js进行web ui测试,我想将值设置为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占位符上设置值,方法如下

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');

但没有任何效果。

4 个答案:

答案 0 :(得分:1)

这可能不是最佳解决方案,但可行:

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

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

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

答案 1 :(得分:1)

感谢您提出的所有宝贵建议,您提供的所有建议都能提供良好的知识,但不幸的是,这些建议都没有效果。我已经通过使用以下内容解决了这个问题。

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

实际上属性&#34; data-def占位符&#34;只使用不是实际文本的水印,所以它正在工作。

答案 2 :(得分:0)

您可以使用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 :(得分:0)

这对我有用。

.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"])
相关问题