Protractor browser.driver.getCurrentUrl vs browser.getCurrentUrl - angularjs

I'm running an Angular app and I'm trying to get the current URL when testing on protractor. Which one to use?
browser.driver.getCurrentUrl() or browser.getCurrentUrl() ?

If this is an Angular application under test - use browser, otherwise - browser.driver.
To quote #jmr from a relevant github issue:
If you need to interact with a non-Angular page, you may access the wrapped webdriver instance directly with browser.driver.
Though, note that both are gonna work if this is Angular application under test. Some people even said that found browser.driver more reliable if it the sync time is longer than usual:
Just to add to this - I find that browser.driver works better on AngularJS apps that take time to sync. I have tried both and for some reason browser.driver is more reliable.
Though, I've personally used browser.getCurrentUrl() and cannot recall any problems.

Related

Detect if the environment is Protractor

In an AngularJS application is it possible to detect if the environment is Protractor?
I would like to disable certain functionality such as Geolocation when running my tests. It's not something I want to test at the moment and I am pretty sure it is what is causing my tests to fail to run.
In my App I use window.jasmine to disable certain polling actions when running Jasmine tests so something similar would be good.
i.e.
if(!window.protractor) {
geoLocationRun()
}
This doesn't work and there doesn't appear to be anything I can use on Window.
Note: I know I can mock out the geolocation which I can do if this isn't possible. Perhaps this is the best approach in any case however it would be good to know if there is a suitable solution. How do I enable geolocation support in chromedriver for Selenium?
I would recommend you to move your geo location code into an angular module. Then you can mock the module in your protractor tests using browser.addMockModule
http://angular.github.io/protractor/#/api?view=Protractor.prototype.addMockModule

With Travis-CI, how do I test chrome packaged apps?

I am writing a chrome packaged app, using angular JS.
My setup uses grunt/karma/jasmine for building/unit testing, and everything was fine until I started using the chrome.* API. This of course was an issue as I had Travis-CI set up to use PhantomJS, but Phantom doesn't know about the chrome global var.
Is there a way to run unit tests with karma that will not throw ReferenceError: Can't find variable: chrome for testing packaged apps, and so pass Travis-CI testing
Is there a way to suppress the errors that PhantomJS is throwing (this sounds bad already)
Few thoughts:
You could polyfill/stub the chrome.* apis before running your tests, to simulate the chrome app environment. Not sure if anyone has already done this already (i.e. here is one quick example for node-webkit I found). It sounds like a useful library someone should write ;)
Just skip those tests that you know will always fail on PhantomJS, by wrapping jasmine test definitions with a guard like if (!is_chrome_app) return;. One clean way to do this in jasmine is to create a defineChromeAppOnly helper that skips the define call if its not a chome app.
You probably want an alternative system for actually running tests inside a real chrome app. I've always just built my own crude CI for this (or done it manually), but since this would be cool to make easier for CI, I've filed a request with Travis CI Team to support Chrome Packaged Apps. Star that issue if you are interested in what they reply.

Testing a websockets server with protractor

I have a custom WebSockets server that triggers route changes in a AngularJS client. I would like to carry out e2e integration tests using protractor. As the route change events are every 30 seconds and involve multiuser interactions, it seems like creating a mock of the service is the best approach. Is there a recommended way of doing this in protractor?
I am not sure if this is exactly what you are looking for, but you can define mock services in Protractor and then upload them to the browser using Protractors browser.addMockModule function. The uploaded module will replace the real one that the application would normally use. See the following blog post for more details.
http://eitanp461.blogspot.com/2014/01/advanced-protractor-features.html
I recently had a similar problem and solved it using addMockModule as stated above. I wrote a post about it describing my solution.
http://www.functionalimperative.com/2015/04/22/protractor-socket-mocks.html
I found organizing my code a certain way helped when mocking the socket modules.

AngularJS e2e tests hang when changing between two separate angular apps

I have a frustrating problem: I want to write end to end tests for a complex angular app. The problem is that the login screen is a separate app. The initial idea was that it would branch out to separate other angular apps in our ecosystem based on the login credentials. It works like a charm. For the tests it is a nightmare though.
The tests work as expected but as soon as correct credentials are entered and the main angular app is loaded the tests just time out. No error message or debug output whatsoever, its just waiting. I can see the page is loading correctly.
Now I thought I would skip this part and test right on the target app but thats not working either since I need to initialize the server with the right credentials first (= go through the login screen).
I tried this with the karma scenario runner and protractor, both show the exact same behavior. Can I tell them to reinitialize after the target page is loaded?
So when protractor times out, the error message shows a link to the faq. Right on top there's the explanation for this problem. Apparently the app sends continuous requests (maybe because I am using socket.io), so Angular is never finished.
This problem has nothing to do with the separate apps.
The issue link was very helpful. Since I am not willing to touch any of the pages code I settled with
browser.ignoreSynchronization = true;
and
browser.sleep( ... );
The tests now work as expected.

Page object design pattern in Angular's e2e tests

Can page object design pattern be implemented in angular e2e or does it follow some other design pattern ?
As far as I'm concerned there is this GitHub project called Astrolabe (by stuplum) which is an extension for protractor that adds page objects to your functional/e2e tests.
https://github.com/stuplum/astrolabe
However, at the time of writing this it has some bugs to fix and it seems to be a WIP project.
You can find here a question regarding Page Object pattern alternatives:
Page Object pattern and alternatives
The protractor getting started page now has a section about it.
Note protractor is now the AngularJS team preferred way for e2e testing.
Regarding Astrolabe i don't believe that's the way to go, see for example this leaking abstraction while using sendKeys on their readme
signInPage.username.sendKeys('a username'); // will fill the username input with the text 'a username'
While the proper way would be more like
signInPage.setUserName('a username');
I prefer to create my own page objects without the need of any extension like Andres shows here

Resources