Powered By Blogger

Search Here!

Wednesday, April 2, 2014

AngularJS Wait for element to come into view !

Wait for element in protrator JS.

helper.js

/**
* Usage: wait(element, label)
* element : It will wait for this element to come into view
* label : just used for the error message
*/
exports.wait = function (element, label) {
    browser.wait(function () {
        return element.isPresent().then(function (state) {
            if (state == true) {
                return element.isDisplayed().then(function (state2) {
                    return state2 == true;
                });
            } else {
                return false;
            }
        });
    }, 10000, label + " did not appear");
    browser.sleep(250);
};


Call This Function

Test.js

this.bookContextMenuIcon = $('.context-info-icon');

helper.wait(this.bookContextMenuIcon, "book context menu icon");

AngularJS available Testing Frameworks and Tooling !

Recently I’m discovering a whole new world. The client side world. So looking for all available frameworks and tools I kept in mind it should be working with Visual Studio but it should also stay close to how the inventors of AngularJS did meant it. I made a list of tools and frameworks that are out now and are very helpful.

 Tools and Frameworks
Jasmine: Behavior driven JavaScript testing framework. Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. Runs in Chutzpah. Protractor tests on the AngularJS documentation site are made in Jasmine.
Mocha: JavaScript testing framework. Mocha is a feature-rich JavaScript test framework running on NodeJS and the browser. Runs also in Chutzpah and it’s also possible to write protractor tests with it.
QUnit: QUnit is a JavaScript unit testing framework. It’s used by the jQuery, jQuery UI and jQuery Mobile projects and is capable of testing any generic JavaScript code. Runs also in Chutzpah.
SinonJS: Standalone test spies, stubs and mocks for JavaScript. No dependencies, works with any unit testing framework.
Chai: Assertion library. Chai is a BDD / TDD assertion library for node and the browser that can be paired with any JavaScript testing framework.
BlanketJS: An easy to install, easy to configure, and easy to use JavaScript code coverage library that works both in-browser and with NodeJS.
PhantomJS: A headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
Chutzpah: JavaScript runner as a Visual Studio extension. Uses PanthomJS as headless browser to run the tests.
NodeJS: Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. NodeJS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Karma: JavaScript Test runner build by the AngularJS team formally known as Testangular. Support unittests and e2e tests. Needs NodeJS to run.
Angular scenario Framework: Since Angular v1.2. On their site they advise to use Protractor. If you are starting a new project, we recommend using Protractor for e2e testing AngularJS projects.
Protractor: Protractor is an end to end test framework for AngularJS applications. It’s built on top of WebDriverJS. Protractor runs tests against your application running in a real browser, interacting with it as a user would. You can use together with Jasmine. To run it’s tests it uses selenium. It will replace the Angular scenario Framework.

Conclusion
For now my choice will be a combination of the Jasmine framework for unit testing and Protractor for e2e testing. Jasmine is easy to use and there’s a lot of documentation on the internet. For e2e testing I’ll work with protractor. Integration with Visual Studio is also available but this stays close to how the AngularJS team meant it.

Monday, March 31, 2014

Adding the HTML Reporter in Protractor JS !

When we run the tests, we can see that tests are passing or failing in the command console. What if we have hundreds of tests and also want to keep a record of the test results  whenever we run the tests? We need test reporters for that.

Protractor gives the options to add onPrepare function property on its configuration file. This onPrepare function will be called before each tests. Jasmine test framework(which we are using inside protractor) provides the option to add test reporter. We will get the metadata about each test by using these two configurations.

There is a node module available for generating screenshots for each tests. protractor-screenshot-reporter. However this doesn't process the meta data and gives us the result in a readable format.

I couldn't find a node module doing this for protractor. So, I created a module for generating this HTML report, on top of protractor-screenshot reporter, protractor-html-screenshot-reporter. What it does is generate an HTML report with all the details of the test like status, browser used, message, link to screenshot etc. (If you are finding some other modules which are doing the same, please add in the comments. )

Let’s install protractor-html-screenshot-reporter module in our project.
$ npm install protractor-angular-screenshot-reporter --save-dev
We need to add this reporter in the Protractor configuration file.
1. Require the protractor-html-screenshot-reporter module and assign to a variable.
var reporter = new HtmlReporter({
   baseDirectory: '/tmp/screenshots'
});
2. Add this reporter on the onPrepare function.
var HtmlReporter = require('protractor-angular-screenshot-reporter');

exports.config = {
   // your config here 
   onPrepare: function() {
      // Add a screenshot reporter and store screenshots to `/tmp/screenshots`:
      jasmine.getEnv().addReporter(new HtmlReporter({
         baseDirectory: '/tmp/screenshots'
      }));
   }
}
3. Run the test using command line.
$ protractor protractor.conf.js
We can see that the tests are passing. Let’s see if the reports are generated.
Screenshots folder is created.


Inside the screenshots folder, one json and png with a guid filename is available. This is the metadata and screenshot respectively for the single test. There is also a combined.json which combines all the metadata into a single file and a reporter.html file which shows all the test reports.

Opening the reporter.html looks like below.

So, now you have an html reporter ready. But, there is one issue though. Whenever you run the tests, the reporter files are getting overridden. If I want to keep a track of tests, this is not going to help.

Protractor-html-screenshot-reporter provides the option of pathBuilder function property to give your own dynamic paths. We will add this in Protractor configuration file.

I am just using a basic function to give the folder name as the current date and time and browser.
Note that I am using the path module for creating the file path (path is a native node module for handling and transforming file paths. ). Make sure that, you have required the path and assigned to a variable.

Wednesday, November 27, 2013

E2E Protractor for .NET

Protractor is an end to end test framework for AngularJS applications built on top of WebDriverJS. In this tutorial I’ll guide you through the process of getting it up and running. Protractor can run Jasmine and Mocha tests so feel free to choose the one that suits your needs best.
The .NET port of Protractor, an end to end test framework for Angular applications.
Protractor for .NET is built on top of Selenium WebDriver C# binding.
Get it from NuGet!
PM> Install-Package Protractor
Supports Microsoft .NET Framework 3.5 and higher..

Write Tests!

         
///
/// User Login.
///

private void UserLogin()
{
//Create Webdriver Instance
webDriver = new ChromeDriver();
webDriver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));
IWebDriver ngDriver = new NgWebDriver(webDriver);
//Navigate Browser
ngDriver.Navigate().GoToUrl("https://mail.gmail.com");
//Enter User Name                    ngDriver.FindElement(NgBy.Input("user.Email")).SendKeys("dummy.user@gmail.com");
//Enter User Password
ngDriver.FindElement(NgBy.Input("user.Password")).SendKeys("qwertyuio");
//Wait For Element
webDriver.FindElement(By.CssSelector("button[data-qa-id*='login-button']")).Click();
//Click Save Button
webDriver.FindElement(By.CssSelector("span[class*='ico-profile']")).Click();       
}

Saturday, November 16, 2013

Changes in Visual Studio 2012 with respect to test features !

Welcome to the first post in this series which will give an overview of the differences in Visual Studio 2012 (VS12) retrospect to Visual Studio 2010 (VS10). In this particular post, we will cover the basic differences between VS2012 and VS2010 Test features, mostly with respect to navigation, running the tests, results and some features that have been deprecated in VS12. Let us now dive into this.

Test List Editor which is very commonly used to run tests in VS10 is deprecated in VS12 and is replaced by Test Explorer. Below screenshots show this difference with a red highlight.


Next question would obviously be, how do I run my tests from Test Explorer and how will this be different from VS2010. The experience of running the tests in VS12 is seemingly different from VS10 for a first time user. When you select Test Explorer in VS12 as shown in the above screen shot (Test – Windows – Test Explorer), it will open the Test Explorer window and you will see ‘Run All’ or ‘Run’ option to execute your tests as compared to ‘Run Checked Tests’ or ‘Debug Checked Tests’ in VS10. Below screen shots will show this difference


In VS12 ‘checkbox’ option is removed which means that if you want to select any particular tests to run, you have to click ‘CTRL + Select’.
Now that we understand the differences on how to run the tests in VS12, let us look at the features that have changed in VS12 while the tests are in progress. Unlike, VS10 where the progress of the test is shown with a distinct ‘In Progress’ icon, this icon is replaced in VS12 with a simple status bar. We have highlighted the differences in the below screenshots