Need help testing user interactions with mocha selenium webdriver. No idea where to find what the correct method is to get value of text input. Tried various variations on getAttribute(), getText etc. Get the same error:
TypeError: Object [object object] has no method 'getElementText'
Here is my code:
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(); });
});
We can get the text from a website using Selenium webdriver USING the getText method. It helps to obtain the text for a particular element which is visible or the inner text (which is not concealed from the page).
What Is getText() Method? The Selenium WebDriver interface has predefined the getText() method, which helps retrieve the text for a specific web element. This method gets the visible, inner text (which is not hidden by CSS) of the web-element.
We can find an element using the link text or the partial link text in Selenium webdriver. Both these locators can only be applied to elements with the anchor tag. The link text locator matches the text inside the anchor tag. The partial link text locator matches the text inside the anchor tag partially.
You need to perform the getText/getAttribute call on the element, not the driver object
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");
This is what worked for me in the end. I needed to use the .then()
function after the sendKeys()
call and then return the value attribute at that point. Steve's answer was helpful but I came to this by playing around with an example set up on git hub called 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();
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With