Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the text input value in selenium webdriver mocha tests

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(); });
});
like image 533
Matt Herbstritt Avatar asked Sep 14 '14 11:09

Matt Herbstritt


People also ask

How do I getText in Selenium?

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 in Selenium Webdriver?

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.

How do I find the partial text of an 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.


2 Answers

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");
like image 106
Steve Weaver Crawford Avatar answered Oct 11 '22 07:10

Steve Weaver Crawford


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();
          });
    });
});
like image 35
Matt Herbstritt Avatar answered Oct 11 '22 09:10

Matt Herbstritt