Peter Boomsma
Peter Boomsma

Reputation: 9806

Protractor can't find by class value

I'm trying to set up a simple Protractor test, but for some reason I'm getting the error:

NoSuchElementerror: No element found using locator: By(css selector, btn-primary3)

This is the test I'm running,

describe('Login to **: ', function () {
    describe('When logging in with faulty data', function () {
        it('Should show an error toast', function () {
            browser.get('https://peter.dev.zen/#/inloggen');
            element(by.model('credentials.Gebruikersnaam')).sendKeys('JonSnow');
            element(by.model('credentials.Wachtwoord')).sendKeys('WinterIsComming');

            element(by.css('btn-primary3')).click();
        });
    });
});

This is the html from the browser,

<fieldset>
    <div class="text-input">
        <label for="username"> Gebruikersnaam </label>
        <input data-ng-model="credentials.Gebruikersnaam">
    </div>
    <div class="text-input>
        <label for="password"> Wachtwoord </label>
        <input data-ng-model="credentials.Wachtwoord">
     </div>
    <div class="buttons">
        <a class="btn-primary3">Inloggen</a>
    </div>
< /fieldset>

Both the by.model work fine, but the

element(by.css('btn-primary3')).click();

Throws the error, but the class is right there in the browser. So what's going wrong?

Upvotes: 1

Views: 1235

Answers (1)

Peter Boomsma
Peter Boomsma

Reputation: 9806

Already found the answer through https://github.com/angular/protractor/blob/master/spec/basic/elements_spec.js

The problem is that if you do by.css(body) it searches for the body element. If you do by.css(.body) it searches for the element with the class name body.

it('should allow accessing subelements from within each', function() {
  browser.get('index.html#/form');
  var rows = element.all(by.css('.rowlike'));

  rows.each(function(row) {
    var input = row.element(by.css('.input'));
    expect(input.getAttribute('value')).toEqual('10');
  });

  rows.each(function(row) {
    var input = row.element(by.css('input'));
    expect(input.getAttribute('value')).toEqual('10');
  });
});

Upvotes: 1

Related Questions