how to use Protractor on non angularjs website?
Another approach is to set browser.ignoreSynchronization = true
before browser.get(...). Protractor wouldn't wait for Angular loaded and you could use usual element(...) syntax.
browser.ignoreSynchronization = true;
browser.get('http://localhost:8000/login.html');
element(by.id('username')).sendKeys('Jane');
element(by.id('password')).sendKeys('1234');
element(by.id('clickme')).click();
If your test needs to interact with a non-angular page, access the webdriver instance directly with browser.driver
.
Example from Protractor docs
browser.driver.get('http://localhost:8000/login.html');
browser.driver.findElement(by.id('username')).sendKeys('Jane');
browser.driver.findElement(by.id('password')).sendKeys('1234');
browser.driver.findElement(by.id('clickme')).click();
waitForAngular should now be used instead of the deprecated ignoreSynchronization property.
The following waitForAngular guidance is taken from the Protractor docs for timeouts:
How to disable waiting for Angular
If you need to navigate to a page which does not use Angular, you can turn off waiting for Angular by setting `browser.waitForAngularEnabled(false). For example:
browser.waitForAngularEnabled(false); browser.get('/non-angular-login-page.html'); element(by.id('username')).sendKeys('Jane'); element(by.id('password')).sendKeys('1234'); element(by.id('clickme')).click(); browser.waitForAngularEnabled(true); browser.get('/page-containing-angular.html');
To test on non angular site, you should remove the synchronisation. for that use the following:
browser.ignoreSynchronisation = true;
browser.get('url');
Forget about ignoreSynchronization
!!! It no longer exists in protractor starting from protractor 5.3.0
browser.waitForAngularEnabled(false)
should be used instead
Step by step instructions how to use it
Protractor has builtin handling of waiting for angular and this is what makes it different. However, if the page is not angular protractor will hang until it hits timeout error, so you need to disable that feature in order to test non-angular page. There is also an edge case, when the page is angular, but this feature will STILL make protractor error out. Perform the steps below, to find out when to disable protractor's waiting for angular
- Find out if your page is Angular: open dev console in the browser and inside of 'console' tab run command
getAllAngularTestabilities()
If the output is getAllAngularTestabilities is not defined
, then your page is not angular, go to the last step to disable built-in waiting
- Run these commands in the console
getAllAngularTestabilities()[0]._ngZone.hasPendingMacrotasks
getAllAngularTestabilities()[0]._ngZone.hasPendingMicrotasks
If any of these return true
(if there are micro or macro tasks pending), then go to the last step. If all are false
, congrats, you can use the builtin protractor's waiting for angular. But if you don't like it as I don't, then read the last step to find out how to disable it
- Run the above mentioned command. BUT! It returns a promise, which needs to be handled, preferably using
await
keyword
await browser.waitForAngularEnabled(false)
This command can be ran anywhere: in the it
block, in beforeAll
, or in onPrepare
of your configuration. Just make sure, if you use await
to make the respective block async
beforeAll(async () => await browser.waitForAngularEnabled(false))