Any idea why protractor tests in html report shows skipped - angularjs

After running my protractor tests no failed tests, but "skipped" in displayed on some tests as shown in the following image any idea why this happens

Jasmine skipped test case if any expect statement is not present in it method by default
//IT statement without expect
it('should verify Deposit Amount without Expect', function () {
customerPage.depositMenubtnClick;
browser.logger.info("------------Deposit Menu Button Clicked -----------------------------------------");
//expect(customerPage.depositMessagetxt).toEqual('Deposit Successful');
browser.logger.info("------------Exppect : Deposit Message -----------------------------------------");
});
html report of a test without any expect statement
Hope this help.....

Protractor it blocks should contains a test. In your case you should include at least one test there.
Eg:
expect(element(by.css('.foo')).isDisplayed()).toBe(true, 'Element - foo - is not Visible!');
More locators can be found in here: Protractor API
This is the reason for the reporter to say skipped unless you have intentionally skipping an it block by using
xit
But if xit being used the reporter will show the xit count in the report summery.

Related

How to make mocha test wait until execution completes

I am writing tests in nodejs using mocha framework.I have multiple it blocks in the test. I am facing problems when executing the code which does not return promises.The test report gets generated with out finishing the it block execution. How can I handle this scenario?
For the it blocks which deal with promises, return statements is serving the need. For the it block which doesn't deal with promises,I have tried by giving done method and it doesn't help.
it('Fill in the details', function(done) {
homepg.name().enterValue('user1');
homepg.group().enterValue('grp1');
done();
}
homepg is a class file which has methods name and group. Both these methods return custom webelements. These custom web elements handle promises internally.
Expected Output: Mocha test should complete first and then test report should get generated.
Actual Output: Test report gets generated without Mocha test being completed.

Non-angular page opened with click - angular not defined using ignoreSynchronization or waiting for Angular without

After a lot of research, and tinkering, I can't seem to actually get my Protractor test to do anything else other than have an Angular related error, even though I am using browser to avoid Angular being detected at all.
The test involves an Angular app, opening a dropdown in it, and clicking on the link for the console; the console opens a non-Angular admin page in a separate window.
So based on the many informative SO posts I found, I first used this...
browser.driver.getAllWindowHandles().then(function(handles) {
browser.driver.switchTo().window(handles[1]).then(function() {
//expect for new window here
});
});
Which appeared to work, as I could get to the window through repl pretty easily.
The issue is when either of the following were added...
browser.driver.getAllWindowHandles().then(function(handles) {
browser.driver.switchTo().window(handles[1]).then(function() {
expect(browser.getLocationAbsUrl()).toContain('/console/login.jsp');
expect(browser.driver.findElement(By.css('th.login')).getText()).toEqual('Login');
});
});
One expect check the URL and the other checks for the header element on the page, which is a table header. When I run this, I get the following:
Error while waiting for Protractor to sync with the page: "angular could not be found on the window"
When I decide to use browser.ignoreSynchronization = true, both in the function, or in a beforeEach, with or without a following afterEach setting it to false, I get the following:
JavascriptError: angular is not defined
I can't seem to get any "useful" errors to help me debug it, and trying it in repl does not help, as I get the same issue.
To be comprehensive, trying my URL expect without getting the second window will give me the root, and the other will fail.
Just doing one or the other will cause the same problem.
Changing to regular syntax (element(by.css...)) does not change things.
So much for my first question...
It appears that my use of browser.getLocationAbsUrl() is meant to be used for an Angular page, and was causing my issue...
Essentially, even though I believed I was using pure Webdriver calls, that call still required Angular on the page to work...
As stated in another post, the use of browser.driver.getCurrentUrl() is a non-Angular call using Webdriver, and fixed the problem. Thus, the final code is the following...
browser.sleep(1000); //to wait for the page to load
browser.driver.getAllWindowHandles().then(function(handles) {
browser.driver.switchTo().window(handles[1]).then(function() {
expect(browser.driver.getCurrentUrl()).toContain('/console/login.jsp');
expect(browser.driver.findElement(By.css('th.login')).getText()).toEqual('Login');
});
});
This works without setting ignoreSynchronization, BTW.
I realized it would probably be something relatively simple to fix it, just didn't expect I'd get it that quickly (I intended on submitting the question last night, but posted it this morning instead).
In any case, I hope this will at least be a good reference for anyone else facing the same issue.
Seems like getLocationAbsUrl is angular abs url.
Try using the native driver getCurrentUrl instead.
-- expect(browser.getLocationAbsUrl()).toContain('/console/login.jsp');
++ expect(browser.driver.getCurrentUrl() ...

Is it possible to force Protractor to pass or skip a test with a message to the console?

I'm working with a system that has several external system dependencies. These external systems are only hooked into certain SDLC environments (local, dev, qa, and prod). Due to these restrictions, I have put environment checks in place on my some of my protractor tests to determine which environment they are in before executing.
For example:
'Test A' is being run, but it is dependent on interacting with 'external system 1' which is only enabled for the QA environment. So if 'Test A' is being run in Local, Dev, or Prod then the test will fail with a message to the console using fail().
My question is...
Is there a way to force the test to Pass or be Skipped with a message similar to using fail()? I'm trying to delineate between tests actually passing or failing cause of functionality and if the test was simply skipped due to environment dependencies in my reports.
I know you can technically "skip" tests when your use "fdescribe" or "fit" and the console will print out something similar to the below
Executed 1 of 25 specs (1 FAILED) (24 SKIPPED) in 18 secs.
How can I invoke that skipping capability from with my tests?
Add x before it{}
describe("", function() {
});
it('Would perform this test', function() {
});
xit('would skip this test', function() {
});
Jasmine publishes a global function pending(message), which works pretty the same as fail(message). You should call it inside a spec to mark it as pending (to skip it):
it('should be skipped', function () {
pending('Force skip');
expect(true).toBe(true);
});
See a working sample
Here is a section in Jasmine docs about it.

Some of your tests did a full page reload - error when running Jasmine tests

I'm running into an issue where when I run my tests on Jasmine, I get this error below. The problem is, it seems to happen when I try to execute a certain amount of tests. It doesn't seem to be tied to a particular test, as if I comment out some, the tests pass. If I uncomment some tests, the error appears. If I comment out ones that were uncommented before, they all pass again. (ie if I have red, green, blue and orange test and it fails, I comment out orange and blue it passes, then I uncomment blue and orange it fails again, but if I comment out red and green it passes again).
Chrome 41.0.2272 (Mac OS X 10.10.1) ERROR Some of your tests did a
full page reload! Chrome 41.0.2272 (Mac OS X 10.10.1): Executed 16 of
29 (1 FAILED) ERROR (0.108 secs / 0.092 secs)
I'm stumped as to what is going on. The more tests I add, that's when this becomes an issue. Has anyone encountered this before? I have no idea what could be causing it, as nothing in any of my tests do any kind of redirection, and they all pass universally on another persons machine.
In my case the problem was that in my source code I had code directly setting the href on the location object, like window.location.href = 'somewhere';
In my specs I set up a onbeforeunload listener that just returns a string instead of allowing the redirect to take place:
beforeAll(() => {
window.onbeforeunload = () => 'Oh no!';
});
I suppose you are using window.location somewhere in your targeted code. In order to pass it just create a spy for the window.onbeforeunload
Example:
window.onbeforeunload = jasmine.createSpy();
Or even better use $window instead, and this will not happen.
Make sure that your tests are properly isolating all modules under test with mocks/spies. The behavior you are seeing says to me that your tests are not truly running in isolation - they are changing some state somewhere that will trigger a reload.
I recently encountered this error with Karma 0.13.12. I upgraded to Karma 0.13.14 and my tests work again. The problem for me (and probably also for #mqklin) was related to https://github.com/karma-runner/karma/issues/1656 and https://github.com/jasmine/jasmine/issues/945.
There are many ways this error can happen.
If your component has a form element, this might be the cause.
Whenever a button on the form is clicked, this error can happen, even tho your component contains no navigation logic.
What worked for me was upgrading Karma from 1.4.0 to 1.4.1 and changing the maximumSpecCallbackDepth in my jasmine.js file from 20 to 100.
creating a spy on the function which has the window.location / reload fixed the issue for me
Hope you have used window.location = "some url" in your code;
Faced similar problem, and solved by using the below changes.
Replaced window.location in the code with,
window.location.assign("some url");
Do the below in unit test:
spyOn(window.location, "assign").and.callFake(() => {
// Dummy assign call - so that your actual call will be faked and the reload will not happen.
});
You also need to make sure that modules are not being loaded twice. In my case, I had an AngularJS module file -e.g., auth.controller.js which contents were already bundled in a core.js file. Once I excluded the bundled files on karma, the error disappeared.
Try to reduce amount of describe sections or completely remove them. I don't know why, but it works for me.
I was using setTimeout(() => window.location.replace('/'), 10);
I used below code in my unit test and it worked for me.
spyOn(global, 'setTimeout');
In case it was ng-submit callback, which doesn't call "event.preventDefault()" and the browser reloads page. Mocking $location doesn't help in that situation.
According to angularjs documentation you should inject the $window module to be able to solve the testability issue you get. If you really want to do a full page refresh while routing, which will reload the whole application. But anyway...
So for example in component
.controller('ExampleController', ['$scope', '$window', function($scope, $window**)
{
$scope.doRerouteWithPageReload = function() {
return this.$window.location.href = "/myUrl";
};
And then in your test-file you import $window to the test-controller your way, then where you assign spies you can do something like this:
$window = { location: {href: jasmine.createSpy() };
And then the actual test is something like this:
expect($window.location.href).toBe("/myUrl");
Angularjs documentation for reading more about $window.
It will solve this Karma redirect error!
var html = '<script type="text/javascript">';
html += 'window.location = "' + urlToRedirect +'"';
html += '</script>';
$( '.wrapper' ).append( html );

view console.log output in angular protractor jasmine test

How can I view console.log output in an angularjs protractor jasmine test? As of now, the browser closes by itself too quickly.
more info - I am working with the angularjs tutorial, step 8. I am trying to change the e2e test to protractor. The protractor config file I'm using is based on %appdata%\npm\node_modules\protractor\referenceConf.js. In spec js files referenced by the config file, I have instances of console.log. However, during execution of the protractor e2e test, the web site opens in chrome, I see things happen in the browser, then the browser closes before I can examine any console.log output. I think I need to keep chrome open somehow. How?
Use browser.manage().logs().get('browser')
browser.manage().logs().get('browser').then(function(browserLogs) {
// browserLogs is an array of objects with level and message fields
browserLogs.forEach(function(log){
if (log.level.value > 900) { // it's an error log
console.log('Browser console error!');
console.log(log.message);
}
});
});
A general misconception is console.log will log the things in your browser. That is incorrect. When you run your tests, along with the results of tests you should see the console.log() values also in the terminal. Browser console is completely different from this.
A general example:
it('get name as John', function(){
element(by.id('name')).getText().then(function(value){
console.log(value);
})
});
Results in Terminal:
John
get name as John - pass
Hope it helps.
This can now be achieved without writing any special code and via plugins:
first party protractor-console-plugin (Chrome only)
third party protractor-console
In order to keep the browser's window open you should try running Protractor in debug mode:
$ <route-to-protractor> debug <route-to-conf-file>
Then in your spec file add this line where you want the execution to stop:
browser.debugger();
In the Protractor debugger console, you can step over the stops by typing c or cont.
More info here: https://github.com/angular/protractor/blob/master/docs/debugging.md
Now, in order to get the console content you can check how to do it in Protractor FAQ:
https://github.com/angular/protractor/blob/master/docs/faq.md#how-can-i-get-hold-of-the-browsers-console
Something like:
browser.manage().logs().get('browser').then(function(browserLog) {
console.log('log: ' + require('util').inspect(browserLog));
})
You may also want to change the logging level to see other types of console outputs.
See my proposed update to the protractor faq to do this here
You could always just override console.log in your test :)
logMessages = [];
console.log = function(message) {
logMessages.push(message);
}
You could also use $log instead of console.log and use a solution like this to put some hooks into the log messages: https://gist.github.com/lrvick/6938531
A simple option is to use:
browser.pause(); protractor/api/browser.pause
for example:
it('should .. test stuff', function() {
browser.pause(); //--->the automation will pause here - follow the instructions in your terminal to continue
//--->your broken testing magic here...
});
Place that method call as the first item in the spec body where you need to view the browsers console.
After the browser pauses you'll have control of the automation. You can then interact with the browser as usual, inspecting elements at different states, checking the browsers console, etc.
Continue the tests by entering c for continue in your terminal - there will be a prompt with instructions waiting for your entry.
If you
install protractor-console
add this to your config
plugins: [
{
package: 'protractor-console',
logLevels: ['severe'],
},
],
this will log all browser error logs to your command line where you run protractor

Resources