Use protractor with Java - java

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.

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.

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.

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

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"));

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.

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!

Related

Can Java Handle ADO Errors

I'm wondering if it's possible to use ADO Errors in Java.
I'm looking at some ASP code that uses ADO for Errors, I believe I've converted it to JAVA.
Is it possible to even do or is there better error handling in Java?
I'm familiar with using a try catch block, but I'm not sure how to incorporate all the errors into one.
There are about 10 different errors, I cut them out for this question and just left a few errors for this question
Snippet of ASP Code:
If Err.number <> 0 Then
Response.Write("<!-- ADO Errors Begin -->" & vbCrLf)
For each objError in con_duns_sdo2.Errors
Response.Write("<!-- ADO Error.Number = " & objError.Number & "-->" & vbCrLf)
Response.Write("<!-- ADO Error.Description = " & objError.Description & "-->" & vbCrLf)
Next
Response.Write("<!-- ADO Errors End -->" & vbCrLf)
Response.Write("<!-- VBScript Errors Begin -->" & vbCrLf)
Snippet of Java that I'm trying to convert:
for (objError : con_currency_sdo) {
if (con_currency_sdo.Errors == "true") {
System.out.println("ADO Error.Number" + objError.Number + "\r\n");
System.out.println("ADO Error.Description" + objError.Description + "\r\n");
}else{
System.out.println("ADO Errors End" + "\r\n");
System.out.println("VBScript Errors Begin" + "\r\n");
}
}
According to Microsoft, ADO.NET is a set of classes that expose data access services for .NET Framework programmers. ADO.NET provides a rich set of components for creating distributed, data-sharing applications. It is an integral part of the .NET Framework, providing access to relational, XML, and application data. ADO.NET supports a variety of development needs, including the creation of front-end database clients and middle-tier business objects used by applications, tools, languages, or Internet browsers.
Given that, the answer is no. Java does not and cannot natively integrate with any .NET Framework (there are third-party commercial libraries that enable it, but they're typically non-trivial to work with). Instead Java uses JDBC to perform database operations in a vendor neutral cross-platform way.

Test my js file loaded or not using phantom in play framework

I am using java play framework. How can I implement phantomJS to check whether my external js file is loading or not?
Since you are asking about phantomjs, I suspect this is about running tests on your application.
You would need to write a phantomjs script. There are event listeners that you can use to detect if resources like referenced javascript files are loaded:
Use page.onResourceError when a resource for whatever reason cannot be loaded.
Use page.onResourceTimeout when you suspect a timeout.
Example:
page.onResourceError = function(resourceError) {
if (resourceError.url.match(/\.js/)) {
// only javascript
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
}
};

How can i send an query from java to lpsolve as String

Hi i formulated a linear programing problem using java
and i want to send it to be solved by lpsolve without the need to create each constraint seperatlly.
i want to send the entire block (which if i insert it to the ide works well) and get a result
so basically instead of using something like
problem.strAddConstraint("", LpSolve.EQ, 9);
problem.strAddConstraint("", LpSolve.LE, 5);
i want to just send as one string
min: 0*x11 + 0*x12 + 0*x13
x11 + x12 + x13= 9;
x12 + x12<5;
can it be done if so how?
LpSolve supports LP files as well as MPS files. Everything is thoroughly detailed in the API documentation (see http://lpsolve.sourceforge.net/5.5/).
You can do your job like this in java :
lp = LpSolve.readLP("model.lp", NORMAL, "test model");
LpSolve.solve(lp)
What is sad with file based approaches is that you will not be able to use warm start features. I would not suggest you to use such approach if you want to optimize successive similar problems.
Cheers

Easy way to include Rhino shell methods in scripts or to load other js scripts?

I'm trying to use the Rhino shell methods such as load, print, etc. My question is SIMILAR to this one, however, I DO NOT have access to the actual java code without hacking apart the framework itself (accela automation FWIW). I'd like to be able to easily add on other .js scripts such as jQuery. But the big caveat is that I only have access to the javascript scripts - not the actual java context. That being said - I can (of course) do the typical Rhino things such as call on java classes, objects, etc.
Has anyone done this or have any good ideas how I would go about it?
Seems this is what I was looking for:
var manager = new Packages.javax.script.ScriptEngineManager();
var engine = manager.getEngineByName( "js" );
var scriptFile = "/pathToScript/scriptFileName.js";
var eval = engine.eval( new Packages.java.io.FileReader( new Packages.java.io.File( " + scriptFile + " ) ) );

JavaScript in Android

Is it possible to use JavaScript in Android?? if so, how? Please provide some examples.
Thanks.
I am way late to the party here, but I had this exact need. iOS 7 now includes JavaScriptCore
natively and it is really easy to use (despite limited documentation). The problem is that
I didn't want to use it unless I could also use something similar on Android. So I created the AndroidJSCore project. It allows you to use your JavaScript code natively in Android without requiring a bulky WebView and injection. You can also seamlessly make asynchronous
calls between Java and Javascript.
Update 27 Mar 17: AndroidJSCore has been deprecated in favor of LiquidCore. LiquidCore is based on V8 rather than JavascriptCore, but works essentially same. See the documentation on using LiquidCore as a raw Javascript engine.
From the documentation:
... to get started, you need to create a JavaScript JSContext. The execution of JS code
occurs within this context, and separate contexts are isolated virtual machines which
do not interact with each other.
JSContext context = new JSContext();
This context is itself a JavaScript object. And as such, you can get and set its properties.
Since this is the global JavaScript object, these properties will be in the top-level
context for all subsequent code in the environment.
context.property("a", 5);
JSValue aValue = context.property("a");
double a = aValue.toNumber();
DecimalFormat df = new DecimalFormat(".#");
System.out.println(df.format(a)); // 5.0
You can also run JavaScript code in the context:
context.evaluateScript("a = 10");
JSValue newAValue = context.property("a");
System.out.println(df.format(newAValue.toNumber())); // 10.0
String script =
"function factorial(x) { var f = 1; for(; x > 1; x--) f *= x; return f; }\n" +
"var fact_a = factorial(a);\n";
context.evaluateScript(script);
JSValue fact_a = context.property("fact_a");
System.out.println(df.format(fact_a.toNumber())); // 3628800.0
You can also write functions in Java, but expose them to JavaScript:
JSFunction factorial = new JSFunction(context,"factorial") {
public Integer factorial(Integer x) {
int factorial = 1;
for (; x > 1; x--) {
factorial *= x;
}
return factorial;
}
};
This creates a JavaScript function that will call the Java method factorial when
called from JavaScript. It can then be passed to the JavaScript VM:
context.property("factorial", factorial);
context.evaluateScript("var f = factorial(10);")
JSValue f = context.property("f");
System.out.println(df.format(f.toNumber())); // 3628800.0
Do you mean something like making a native app using Javascript? I know there are tools like Titanium Mobile that let you make native apps using web tools/languages.
You could also make Web Apps. There are loads of resources and tutorials out there for that. Just search "Android Web App tutorial" or something similar.
Yes you can, just create a wrap up code that points to html page and includes your javascript and css.
There are different libraries that can help you with this:
http://www.phonegap.com/
http://www.sencha.com/products/touch/
http://jquerymobile.com/
Just posting this for posterity but React Native is a great solution in this space. You can write a complete app in JS using native views under the hood, or embed a JS component inside an existing Java app. https://reactnative.dev/

Categories