Emna February 2016

How can i test an angular js web application with selenium?

I'm new in Test Automation. I want to use Selenium in order to test web application done by angular js. Can someone suggest to me how to start with a basic application and do you have an example of an angular js application.

Answers


alecxe February 2016

For e2e-testing AngularJS applications, there is a specialized package called Protractor, which itself is a convenient wrapper around WebDriverJS - javascript selenium bindings.

A great place to start is the Tutorial.


ThreeDots February 2016

I've recently found GitHub project called jProtractor. It implements its own NgWebDriver based on Selenium WebDriver. Most important feature I noticed is that most of basic methods wait for finishing AngularJS script before calling the original method.

Note that I haven't tried it myself so I can't tell how well it works, but it is currently in developement and may contain bugs and lack features.

Hope it helps.


Ardesco February 2016

The below two expected conditions will probably do everything you need to make testing an angular site easy.

This one will be the most used expected condition you will have in your code:

public static ExpectedCondition angularHasFinishedProcessing() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            String hasAngularFinishedScript = "var callback = arguments[arguments.length - 1];\n" +
                    "var el = document.querySelector('html');\n" +
                    "if (!window.angular) {\n" +
                    "    callback('false')\n" +
                    "}\n" +
                    "if (angular.getTestability) {\n" +
                    "    angular.getTestability(el).whenStable(function(){callback('true')});\n" +
                    "} else {\n" +
                    "    if (!angular.element(el).injector()) {\n" +
                    "        callback('false')\n" +
                    "    }\n" +
                    "    var browser = angular.element(el).injector().get('$browser');\n" +
                    "    browser.notifyWhenNoOutstandingRequests(function(){callback('true')});\n" +
                    "}";

            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            String isProcessingFinished = javascriptExecutor.executeAsyncScript(hasAngularFinishedScript).toString();

            return Boolean.valueOf(isProcessingFinished);
        }
    };
}

It will wait for angular to be in a state where it thinks that the site is ready for automated tests to go ahead (This is pretty much stolen from the protector code base and wrapped in Java). You will need to have a script timeout set for it to work though:

webdriver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);

Finally if you are using angular animate to move things around on the screen you will wa

Post Status

Asked in February 2016
Viewed 2,584 times
Voted 9
Answered 3 times

Search




Leave an answer