在 Java 中使用量角器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29983241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use protractor with Java
提问by user3762901
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.
我想在 Java 上而不是在 Node.js 上使用量角器。是否可以将量角器与 Java 或 Python 一起使用?我们不想添加另一种技术进行测试,而是想使用现有技术。
回答by Tom Nijs
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.
不幸的是,您在这件事上没有太多选择,因为 Protractor 是 AngularJS 的 JavaScript 测试框架,它通过 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.
量角器是为 angularJS 应用程序定制的。因此,如果您的应用程序是使用 AngularJS 创建的,Protractor 会有所帮助,因为它内置了对 AngularJS 页面加载和操作的支持。
If your application is not built on top of Angular, you can use Selenium WebDriver on top of any other languages you prefer.
如果您的应用程序不是基于 Angular 构建的,您可以在您喜欢的任何其他语言之上使用 Selenium WebDriver。
Selenium provides users with documentation on using Python as a medium to write tests, read more about this here.
Selenium 为用户提供了有关使用 Python 作为编写测试的媒介的文档,请在此处阅读更多相关信息。
回答by Manoj
There is already a library in Java for automating Angular stuffs. Its built based on Protractor called "ngWebDriver"
Java 中已经有一个库用于自动化 Angular 的东西。它基于名为“ ngWebDriver”的量角器构建
回答by silver
As of 2017, I have found these Protractor libraries for Java:
截至 2017 年,我发现了这些用于 Java 的 Protractor 库:
- jProtractor- Development is somewhat inactive but I have tested it to work. Click herefor more details.
- ngWebDriver- Developed by Paul Hammant (co-creator of Selenium). Currently in active development with good documentation.
- jProtractor- 开发有点不活跃,但我已经测试过它可以工作。单击此处了解更多详细信息。
- ngWebDriver- 由 Paul Hammant(Selenium 的共同创建者)开发。目前正在积极开发并提供良好的文档。
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"));
回答by Scott Boring
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.
Protractor 是一个 JS 库,因此您无法在 Java 中运行它,并且在没有 Protractor 的情况下测试 Angular 应用程序很困难,因为您的测试代码需要等待 Angular 进程完成,然后才会发生单击等交互。
Fortunately, Angular has made it easy to identify when it's done processing.
幸运的是,Angular 可以很容易地识别它何时完成了处理。
There is a JS function that takes a callback and will notify you once the Angular is ready.
有一个 JS 函数接受回调,一旦 Angular 准备好就会通知你。
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.
注意:这适用于 Angular 1.4.8。其他一些版本的 Angular 也有类似的不同方法。
You can invoke the testability method from your Java test code using the following simple method or something similar.
您可以使用以下简单方法或类似方法从 Java 测试代码中调用可测试性方法。
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.
在使用 click 等方法与驱动程序交互之前调用 waitForAngular()。
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.
您可能需要一个与 'body' 不同的 rootSelector,如果 angular 不存在,您可能想抛出一个错误,但这很适合我的需要。
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.
Protractor 提供了其他选择器,这可能会使测试 Angular 应用程序更容易,但我个人使用 ID 和类选择器,所以我不需要它们。
回答by Girish Sortur
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.
要在上面 Tom 的回答中添加一些内容,您甚至可以使用 Protractor 测试基于非角度的应用程序/网站。但是还是没有办法,你可以使用Java或Python编写Protractor测试,因为Protractor的核心是基于Javascript(node.js)构建的,并且是纯Javascript。希望能帮助到你。
回答by Saandeep Sreerambatla
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!
使用量角器的最佳方法是,分别用 javascript 编写量角器测试,并在需要时从 java/python 调用这些测试。这就是我们目前正在做的事情!