0

Webdriverio element needs to be type of string?

My code is as followed:

describe('Test Contact Us form WebdriverUni', function() {
  it.only('Should be able to submit a successful submission via contact us form', function(done) {
    browser.pause(5000);
      var firstNameTextField = $("[name='first_name']");
      var firstNameTextField = "[name='last_name']";

      browser.setValue(firstNameTextField, 'Joe');

Using the following code: var and $, I seem to be receiving the following exception: 'element needs to be type of String'.

Also the following fails with the same exception:

var firstNameTextField = browser.element("[name='last_name']");

However the following works:

browser.setValue("[name='first_name']", 'Joe');

Any ideas?

SamP
  • 417
  • 5
  • 24

1 Answers1

3

You see, webdriver setValue takes a selector that must be for type String.

When you use this piece of code: var firstNameTextField = $("[name='first_name']"); you are getting an object, not a string.

When however you call it like that: browser.setValue("[name='first_name']", 'Joe'); you are providing a selector of type String. And it works.

So, you should probably change your variable to var firstNameTextField = "[name='first_name']";

EDIT:

I also noticed you are using the same variable name twice here:

  var firstNameTextField = $("[name='first_name']");
  var firstNameTextField = "[name='last_name']";

Shouldn't the second one be lastNameTextField?

Chris Elioglou
  • 611
  • 1
  • 6
  • 7
  • Chris thanks so much! The variables listed above with the same name was just for illustration purposes, thanks again for spending the time to create a detailed answer. – SamP Sep 08 '18 at 13:11
  • how would I use $ to setup my variables according to: http://webdriver.io/api/utility/$.html ? – SamP Sep 08 '18 at 13:32
  • 1
    If you want to use the element you get from `var firstNameTextField = $("[name='first_name']");` then: Instead of `browser.setValue(firstNameTextField, 'Joe');` Do it like: `firstNameTextField.setValue('Joe');` – Chris Elioglou Sep 08 '18 at 13:52