Use protractor with Java

2019-02-13 00:39发布

问题:

I want to use Protractor on Java and not on Node.js. Is it possible to use Protractor with Java or Python? We do not want to add another technology for testing and want to use existing technologies.

回答1:

Unfortunately you don't have much of a choice in the matter, as Protractor is a JavaScript Testing framework for AngularJS, it is distributed via Node.js.

We do not want to add another technology for testing and want to use existing technologies.

Protractor is customized for angularJS applications. So if your application was created using AngularJS, Protractor will help as it has inbuilt support for AngularJS page load and actions.

If your application is not built on top of Angular, you can use Selenium WebDriver on top of any other languages you prefer.

Selenium provides users with documentation on using Python as a medium to write tests, read more about this here.



回答2:

There is already a library in Java for automating Angular stuffs. Its built based on Protractor called "ngWebDriver"



回答3:

As of 2017, I have found these Protractor libraries for Java:

  • jProtractor - Development is somewhat inactive but I have tested it to work. Click here for more details.
  • ngWebDriver - Developed by Paul Hammant (co-creator of Selenium). Currently in active development with good documentation.

Code Snippet:

<input type="text" ng-model="startBalance" placeholder="Enter your current balance" class="ng-pristine ng-valid">    

// jProtractor
WebElement startBalanceField = driver.findElement(NgBy.model("startBalance"));

// ngWebDriver
WebElement startBalanceField = driver.findElement(ByAngular.model("startBalance"));


回答4:

Protractor is a JS library so you can't run it in Java and testing Angular apps without Protractor is difficult because your tests code needs to wait for Angular processes to complete before interactions like clicking occur.

Fortunately, Angular has made it easy to identify when it's done processing.

There is a JS function that takes a callback and will notify you once the Angular is ready.

angular.getTestability("body").whenStable(callback);

NOTE: That this works with Angular 1.4.8. Some other versions of Angular have a different method that is similar.

You can invoke the testability method from your Java test code using the following simple method or something similar.

private void waitForAngular() {

    final String script = "var callback = arguments[arguments.length - 1];\n" +
            "var rootSelector = \'body\';\n" +
            "var el = document.querySelector(rootSelector);\n" +
            "\n" +
            "try {\n" +
            "    if (angular) {\n" +
            "        window.angular.getTestability(el).whenStable(callback);\n" +
            "    }\n" +
            "    else {\n" +
            "        callback();\n" +
            "    }\n" +
            "} catch (err) {\n" +
            "    callback(err.message);\n" +
            "}";

    ((JavascriptExecutor) driver).executeAsyncScript(script, new Object[0]);
}

Call waitForAngular() before interacting with the driver with a method like click.

You may want a different rootSelector from 'body' and you may want to throw an error if angular doesn't exist but this works well for my needs.

Protractor provides other selectors which may make testing an Angular app easier but personally I use ID and Class selectors so I don't need them.



回答5:

To add something to Tom's answer above, you can even test non-angular based apps/websites with Protractor. But there is still no way, you can write Protractor tests using Java or Python as Protractor's core is built on Javascript(node.js) and is purely Javascript. Hope it helps.



回答6:

The best way to use protractor is , have protractor tests separately written in javascript, and call those tests from java/python when ever required. Thats what we are currently doing!