Helping Hands
Helping Hands

Reputation: 5396

ReferenceError : By is not defined - webdriverIO

Code :

var assert = require('assert');
describe('webdriver.io api page', function() {
    it('should open login page', function () {
        browser.url('/login');
        browser.pause(10000);
})

it('Should enter login details and submit', function() {

    browser.pause(10000);
    browser.element(By.xpath("//input[@type='text' and @aria-label='user name']")).sendKeys("mikestr");

})
});

I am new to webdriverIO and trying to locate username textbox using xpath but it throws me error. I look around many webdriverIO material but did not get solution for this yet.

Upvotes: 0

Views: 4983

Answers (3)

Taran
Taran

Reputation: 14166

I had a different issue using angular with the same By is not defined error. For anyone else that might end up here the solution for me was to add the import

import { By } from "@angular/platform-browser";

Upvotes: 0

Denzik
Denzik

Reputation: 316

WebdriverIO's Selectors are different than Selenium Webdriver. It will accept the String value for either "XPath" or "CSS" selectors. The "By is not defined" Error you're seeing is because it's looking for a "String" value of a selector.

Documentation: http://webdriver.io/guide/usage/selectors.html#CSS-Query-Selector

In your situation you would only need to pass something like:

browser.element('"//input[@type='text' and @aria-label='user name']"')
       .setValue('mikestr')

Alternatively you could set the variable and pass a variable like:

var XPath = "//input[@type='text' and @aria-label='user name']"
browser.element(XPath).setValue('mikestr')

Upvotes: 2

user1207289
user1207289

Reputation: 3273

The locator has to be a string

For setting value , you can use setValue

browser.setValue("//input[@type='text' and @aria-label='user name']", 'mikestr');

Upvotes: 1

Related Questions