How to make mocha test wait until execution completes - selenium-webdriver

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.

Related

Any idea why protractor tests in html report shows skipped

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.

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.

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

How do I use resume() in Angular JS e2e testing?

I am going through step three of this angular js tutorial. http://docs.angularjs.org/tutorial/step_03
The very last task is to "Add a pause() statement inside of an end-to-end test and rerun it."
I was able to add pause() and the test paused as it should. The question is, how to resume?
The documentation for pause() here indicates calling resume() in the console or clicking the resume link in the Runner UI, but I can't seem to figure out how to call resume from the console, nor can I find the resume link in the Runner UI.
How do you call resume()?
My guess is you put the pause() function call in the wrong place in your test\e2e\scenarios.js file. I did this same thing - the test doesn't pause, it finishes the first or both or neither, depending on where you put the pause() call.
Put the pause() call in your beforeEach(...) call like so
beforeEach(function() {
browser().navigateTo('app/index.html');
pause();
});
Then add the new it(...) method given in the tutorial directly after the closing parenthesis of the it('should filter the phone...) function.
Now the e2e test page will pause before executing each it(...) section and let you change the DOM contents before the test runs. For example, if you click 'resume' without searching for anything the first test passes because there are 3 phone entries in the list. But if you search for xyz it will fail because there will not be any phone entries in the list.
You can also place the pause() call before each expect(repeater(...)) or input('...') call to have the test pause before each sub test. By placing a pause() before each expect(...) call you can edit the DOM after the input('...') call has changed it but before the assertion is made so you can see how the input changes the content and why changing that content yourself might cause an asssertion (expect(...)) to fail or succeed.

End-to-End testing with Jasmine

I am trying to perform some end-to-end tests of an application written with AngularJS. Currently, I have the following tests setup in e2e.tests.js:
describe('MyApp', function() {
beforeEach(function() {
browser().navigateTo('../index.html');
});
it('should be true', function() {
expect(true).toBe(true);
});
});
Oddly, this basic test fails. The test itself runs. However the results are:
203ms browser navigate to '../index.html'
5ms expect undefined toBe true
http://localhost:81/tests/e2e.tests.js:10:9
expected true but was undefined
Considering "true" is hard-coded, I would expect it to pass this test. I have no idea how true can be undefined. What am I missing?
The thing about Angular's E2E test framework is that looks like Jasmine, but it is not Jasmine. The E2E test code is actually all asynchronous, but it is written in a clever way so that the calls look normal. Most of the calls create asynchronous tasks and Future objects that are tested later. The Future object is kind of like a promise, but it's a little different. It has a value property that it sets when it's ready, and then it calls a done function to move to the next step. In E2E tests, the expect function takes Future objects, not values. You're seeing undefined because expect is testing against future.value, which in this case is true.value, which is undefined.
Try using one of the available selectors that return futures and then test the result. Something like this:
expect(element("html").text()).toMatch("Our App");
The Future objects are not well documented, but you should be able to create a Future manually like this:
var trueFuture = angular.scenario.Future(
"a true value", // name that is displayed in the results
function(callback) { // "behavior" function
callback(null, true); // supposed to call callback with (error, result)
});
expect(trueFuture).toEqual(true);
If you look in the ng-scenario source code, you can see the place where the matcher tests future.value in the angular.scenario.matcher function.
I too have faced the similar problem and here's what I found.
You must be using ng-scenario as your framework with jasmine in config file.
The fact is that expect function in ng-scenario doesn't take any var value or Boolean value. It only takes functions like
expect(browser().location().path()).toContain('some/string')
or some other ng-scenario function like
expect(element('some element').html()).toContain('some String');
Any variable value or Boolean in expect function is undefined.
If you want to use Boolean(true/false) or you want your test to be passed then you have to remove 'ng-scenario' from your framework section of config file.
Try It with only jasmine!

Resources