Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Knockout app be tested with Protractor?

I'm working on a web page that uses Knockout. I set up Protractor after seeing this post about using Protractor on non-Angular pages, but it doesn't seem like Protractor can 'see' any elements that are part of a KO component.

describe('a simple test', function () {
  it('works', function () {
    browser.ignoreSynchronization = true;
    browser.get('profile');

    expect(browser.getTitle()).toEqual('Title');  // this passes (outside KO)

    expect(element(by.id('ko-component')).getText()).toEqual('Hello World!'); // this fails (inside KO)
  });
});

The second assertion results in this error, even though the element is definitely in the HTML.

Message:
 NoSuchElementError: No element found using locator: By.id("ko-component")

If I can't use Protractor, then suggestions for other e2e testing frameworks are welcome.

like image 724
chinaowl Avatar asked May 05 '15 22:05

chinaowl


1 Answers

protractor is basically a wrapper around WedDriverJS (javascript selenium bindings). protractor makes testing AngularJS page easier and more natural, knowing when angular is settled and a page is ready to be interacted with and introducing several angular-specific locators.

In other words, you can definitely test knockout pages with protractor. In this case, you need to explicitly wait until the ko-component element is present by using presenceOf Expected Condition:

var EC = protractor.ExpectedConditions;
var e = element(by.id('ko-component'));
browser.wait(EC.presenceOf(e), 10000);

expect(e.getText()).toEqual('Hello World!');
like image 176
alecxe Avatar answered Oct 05 '22 23:10

alecxe