I know that the following is some kind of the same in GWT:
Window and $wnd
Document and $doc
Are there any differences, beside that the first is used in Java and the second in JSNI (JavaScript)?
Window is a class plenty of static methods, which you can use for getting or setting certain properties of the browser window, but actually it is not the native browser window object. These method implementations are eventually JSNI blocks which use the $wnd variable to set/get those properties. Like Window.alert() whose implementation would be $wnd.alert(). Note that Window.java doesn't have access for everything in the browser's window object like window.console etc.
GWT uses $wnd instead of window because compiled code is executed normally in an iframe, and in this context, window will reference the iframe window while $wnd will reference the parent window. The same occurs with $doc which is a reference in the iframe to the parent document.
In the other side Document is a java class which extends JavaScriptObject, it means that it is an Overlay type which basically means that it is a special wrapper for a native javascript object, it does not modify the underlying JavaScript but adds a set of java methods to interact with it. It can be safely cast when passing to jsni.
In summary, although Document and $doc are not the same in java world, when it is compiled they will be the same, otherwise Window it's not an overlay of $wnd, it is just a way to access certain methods of the browser window.
Although GWT compiled code delegates to native js objects and methods, don't try to find similarities between js and java objects. GWT has designed an API to develop ajax applications using a set of java objects, widgets, patterns, etc. Some objects and methods are named in the same way, but almost the API is different. There are though, other projects which compiles java to javascript which has a rigid parallelism between both worlds like ST-JS, and GWT provides an experimental library called Elemental whose API is almost identical to javascript (it's only available for Chrome).
Related
I have a Java project and now I also want to use this code for a GWT project so I don't have to write the same code twice. The problem is that some classes are not available in gwt, for example java.awt.geom.Rectangle2D.Double.
I was thinking of creating a wrapper class called RectangleWrapper that would extend either the standard Rectangle class or a gwt version of the same class.
In the shared code I would replace all my Rectangle2D.Double with my Rectangle and depending on a final boolean my wrapper class (compiler flags java) would either extend on or the other Rectangle class.
Is this even possible or is there a better way to do this?
I just took one of my GWT-project client-side classes and added the following lines:
if (false) {
String a = StringEscapeUtils.escapeHtml("lalala");
}
This obviously compiles just fine, but when I launch the GWT app I get this:
ERROR: Errors in 'file:/C:/gwtproject/src/main/java/package/ClientSideClass.java'
ERROR: Line 119: No source code is available for type org.apache.commons.lang.StringEscapeUtils; did you forget to inherit a required module?
So the answer would be no, you can't use a wrapper like that.
Is there any other way of achieving that? I highly doubt it.
One of the main features of GWT is replacing the old AWT/Swing desktop GUI components with a whole set of web GUI components designed for Javascript compatibility; there's no point in making the old components available or supported in any way.
If you were talking about utility libraries like Apache Commons, I could advise you to make an RPC call instead (client-side calls server-side where you can use anything you like, and return the results asynchronously), but that's not the case.
A colleague who coded the client side of a GWT application has essentially made all widgets and their methods and fields static. I am trying to evaluate it's impact.
Based on my reading so far, this has the result of making the javascript objects global for each user's compiled nocache.js file. In terms of drawbacks, this is potentially inefficient in that objects are not created dynamically and freed on demand. Also, it's a major hassle to maintain this code.
Are there any other risks in coding the client this way?
You cannot use your widget in two places in your app: they will keep the last entered data in the lastly used form.
This must be time consuming at startup (page load), since all widgets of the app are created : there is no control on the instant the widget is created
The recommended way is to have a ClientFactory (http://www.gwtproject.org/doc/latest/DevGuideMvpActivitiesAndPlaces.html#ClientFactory) :
Widget lifetime is controlled through this object that decides whether to create a new instance for each widget request or use the same one
I'm working on building a custom Simulink block as a Matlab Toolbox. In order to avoid matlab's language to program the system, I'd like to make the system in Java as much as possible. I've researched the Matlab <-> Java interface, and it seems possible to do this. However, the one thing I couldn't find any information about was storing my custom Java object (holding the block's data) inside the Simulink block.
I conducted a quick test, and it seems storing a Java.lang.String instance is possible. However, that was a relatively simple test. Before jumping in head first, I wanted to check if this was even possible. Does anyone have experience with a similar setup? Does the object simply need to be Serializable to work?
For background information, I'm looking to implement the non-math part (GUI code, processing, etc) in Java. Math related elements would likely remain in matlab.
To store your Java object inside the block you should use its UserData block parameter. According to the documentation, you can put any data type in this parameter.
The only problems I can see with this are saving/loading and creation of new blocks. Saving/loading should be solved using serialization, but you will have to try it to see. If this doesn't work, then you could create a hidden mask parameter for your blocks, serialize your Java object to a string, and save the data in this mask during the PreSaveFcn callback. The data could be deserialized from the mask parameter in the LoadFcn callback.
For the creation of new blocks, you should set the PreCopyFcn callback of your library block and create your new Java object there. I have the feeling that if you don't do this, then MATLAB will copy the reference to your object from UserData (if one exists there already), which is probably not what you want.
You probably also want to override the OpenFcn callback since your aim is to use your Java object as a kind of souped-up mask, so that when a user double-clicks on the block you can show your custom UI.
For more information on block callback parameters, see this.
I'm having an applet in which the class name is known but the text box fields and other controls and their ids are not known.
Now i need to get the id of the textbox or the total controls exists in the applet from javascript or some java code.
If you're trying to say the text boxes (and whatnot) are part of the Java applet -- these are not HTML elements (although they may look similar), so they are not directly accessible from JavaScript. The applet may provide indirect access to them via LiveConnect, but by default there is no interface provided.
I have a hard time to get used to the WebDriver PageObject pattern. Please share your experience and ways of using PageObjects pattern and loadableComponents.
As PageObject represents usually a component or piece of functionality, it occurred to me at first that I should use it to actually do some things in load() and see if it does what it should in isLoaded().
Then I realized that it should be utilized only for "loading" objects (possibly initialization) as it is in case of moving around a website, each object with its own url. And using isLoaded() to test whether the object IS READY for testing.
But if you have a complex JavaScript order submitter to test, that is compound of JS file uploader, JS form that is based on 2 independent parts and there are three kinds of Orders, you don't move anywhere (regarding URL), just the state of elements is changing.
Consider the get() method. You get into the page with the interactive Form. It is loaded when the form exist on the page. Then you have form1 and form2 objects ... what should their load() and isLoaded() method look like, they are ready for action right away because they don't need any loading, just testing their services.
It's a mess, one doesn't know if the isLoaded() method is used for checking whether object loaded, or whether object loaded and was setup properly. But I guess the former way is correct and that validity of setting it up should be ensured within the tests.
Scenario:
Testing first part of html form - test that field client side validation works
Testing the second one that depends on the first one
Testing the following file uploader - upload, canceling, clearing, order, fileIDs
Testing the overall html form submission - ServerSide validation errors, results
The documentation says :
LoadableComponent
PageObjects
The public methods represent the services that the page offers
Validate, upload, upload Multiple, cancel, clear
Try not to expose the internals of the page
The only thing that occurs to me is having Driver instance hidden to UnitTests and use Only PageObjects to keep all field names, css class names in PageObjects + supply PageObjects with input data and assert boolean result of the services/functionality
Methods return other PageObjects
This is the most difficult thing to get used to. Having 4 page objects for one interactive form kinda doesn't feel natural. They recommend Form1, Form2(Form1), Upload(Form2), Submit(Upload), although I find the chaining and handing over a reference to the preceding object very chaotic. Calling get() on all of them in a test method seems better. But I guess that the idea behind it is not to expose the Driver instance to the Tests, but use only PageObjects that are using Driver instance internally
Different results for the same action are modelled as different methods
I suppose that this means that it shouldn't be decided about validity of that action on the Page object side, but on the Test side
I have a bit different approach when writing page objects. Translated to your terms load() method ends with conditional wait ensuring the page contains what you desire. Then I do not need any isLoaded() - either I'm there or Exception is thrown.
Components in page are properties of the page containing them and I set two-way relationship between a component and the page. They are not fully functional page objects. Maybe your forms can be modeled this way too.
The loadable component is an extension to the Page Object pattern. The LoadableComponent class in the WebDriver library will help test case developers make sure that the page or a component of teh page is loaded successfully. It tremendously reduces the efforts to debug your test cases. The page object should extend this Loadable Component abstract class and as a result, it is bound to provide implementation for the following two methods:
protected abstract void load()
protected abstract void isLoaded() throws java.lang.Error
The page or component that has to be loaded in the load() and isLoaded() methods determines whether or not the page is fully loaded. If it is not fully loaded, it throws an error.
https://code.google.com/p/selenium/wiki/LoadableComponent