需要帮助测试用户与mocha selenium webdriver的交互。不知道在哪里找到正确的方法来获取文本输入的值。尝试了getAttribute(),getText等各种变体。获取相同的错误:
TypeError: Object [object object] has no method 'getElementText'
这是我的代码:
var assert = require('assert'),
fs = require('fs');
var webdriver = require('../node_modules/selenium-webdriver'),
test = require('../node_modules/selenium-webdriver/testing'),
remote = require('../node_modules/selenium-webdriver/remote');
test.describe('Google Search', function() {
var driver;
this.timeout(60000);
test.before(function() {
driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
});
test.it('should append query to title', function() {
//setTimeout(done, 60000);
driver.get('http://localhost:8080/devanagariTextField/index.html');
driver.findElement(webdriver.By.id('textbox')).sendKeys('gaa');
//driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
return driver.getElementText('value').then(function(text) {
console.log('TEXT: ' + text)
return 'गा' === text;
});
}, 1000);
});
//test.after(function() { driver.quit(); });
});
答案 0 :(得分:8)
您需要对元素执行getText / getAttribute调用,而不是驱动程序对象
var myTextBox = driver.findElement(webdriver.By.id('textbox'));
myTextBox.sendKeys('gaa');
//You will probably want to use getAttribute()
var elementContent = myTextBox.getText();
var elementValue = myTextBox.getAttribute("value");
答案 1 :(得分:3)
这最终对我有用。我需要在.then()
调用后使用sendKeys()
函数,然后在该点返回value属性。史蒂夫的回答很有帮助,但我通过在git hub上设置了一个名为selenium-mocha-chai-saucelabs的示例来实现这个目标。
describe('textbox value', function (done) {
it('is expected to equal ka when keys ka are sent', function (done) {
driver.findElement(webdriver.By.id("textbox"))
.sendKeys("ka")
.then(function(){
return driver.findElement(webdriver.By.id("textbox")).getAttribute("value");
})
.then(function (value) {
expect(value).to.equal('ka');
done();
});
});
});