Test my java applet

How should I test JApplets and Applets?

Parameters requested by applet code. Helps avoid applet class caching.

How should I test JApplets and Applets?

Been playing with Java for quite a while now, and I just started working with JApplet s/ Applet s. The problem I’m having is actually testing them. As you all probably know, most browsers cache the files, so if you update any of your code, the JApplet s/ Applet s don’t refresh. I’ve read that you can change the name of the HTML file that hosts the JApplet / Applet , and thus «trick» the browser into caching a «new» program. This doesn’t always seem to work unfortunately.

Another method I see quite often is using the command appletviewer in command line, but I’ve never been able to get this to work.

So I was wondering, how should I test my JApplet s/ Applet s? What is the best way? How do you test your them?

Applet Testing

1. The appletviewer

The appletviewer is relatively easy to use. Here is the example from the Applet info. page.

/* */ import javax.swing.*; /** An 'Hello World' Swing based applet. To compile and launch: prompt> javac HelloWorld.java prompt> appletviewer HelloWorld.java */ public class HelloWorld extends JApplet < public void init() < // Swing operations need to be performed on the EDT. // The Runnable/invokeLater() ensures that happens. Runnable r = new Runnable() < public void run() < // the crux of this simple applet getContentPane().add( new JLabel("Hello World!") ); >>; SwingUtilities.invokeAndWait(r); > > 
2. Appleteer

The Appleteer — applet test tool was designed by me to provide more feedback on an applet launch or the reasons for failure. Main features:

  • Loads applet web pages in an applet enabled JEditorPane.
  • Supports multiple applets on the same page, along with applet communication/object sharing & the 1.4+ InputStream sharing mechanism.
  • Supports the applet showDocument()/showStatus methods.
  • Helps avoid applet class caching.
  • Allows inspection of
    1. The getAppletInfo() & getParameterInfo() defined for the applet.
    2. Parameters requested by applet code. Very handy for poorly documented applets!
  • Can provide easy access to the following information.
    1. Split output and error streams,
    2. Has in-built logging, and all applet logs to the anonymous logger are added to the main log. Along with easy configuration of..
      • Log level.
      • Log history length.

      Java Applet Tutorial, Simple example of Applet by html file: To execute the applet by html file, create an applet and compile it. After that create an html file and place the applet code in …

      JApplets: Loading Images

      After having spend 1 year working on Android I’m a bit rusty in Traditional Java GUI.

      I need to know 2 things in the way I’m opening Images

      /** * Load the image for the specified frame of animation. Since * this runs as an applet, we use getResourceAsStream for * efficiency and so it'll work in older versions of Java Plug-in. */ protected ImageIcon loadImage(String imageNum, String extension) < String path = dir + "/" + imageNum+"."+extension; int MAX_IMAGE_SIZE = 2400000; //Change this to the size of //your biggest image, in bytes. int count = 0; BufferedInputStream imgStream = new BufferedInputStream( this.getClass().getResourceAsStream(path)); if (imgStream != null) < byte buf[] = new byte[MAX_IMAGE_SIZE]; try < count = imgStream.read(buf); imgStream.close(); >catch (java.io.IOException ioe) < System.err.println("Couldn't read stream from file: " + path); return null; >if (count return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf)); > else < System.err.println("Couldn't find file: " + path); return null; >> 
      loadImage("share_back_img_1_512", "jpg"); 

      My problem is : How to make it more dynamic.

      For the moment I’m testing on a few images but I have something like a 100 images for the final applet.

      I had to store the images in a package to be able to access them.

      Is there a way to Load images depending on the content of a package? getting the name, size, extension.

      Basically a simpler way to generate the ImageIcons

      The way you’ve written your stream read — it could result in a partial read, since just one call to read is not guaranteed to return all the bytes that the stream may eventually produce.

      Try Apache commons IOUtils#toByteArray(InputStream), or include your own simple utility method:

      public static final byte[] readBytes(final InputStream is) throws IOException < ByteArrayOutputStream baos = new ByteArrayOutputStream(Short.MAX_VALUE); byte[] b = new byte[Short.MAX_VALUE]; int len = 0; while ((len = is.read(b)) != -1) < baos.write(b, 0, len); >return baos.toByteArray(); > 

      As for your organizational concerns. there is no simple+reliable way to to get a «directory listing» of the contents of a package. Packages may be defined across multiple classpath entries, spanning JARs and folders and even network resources.

      If the packages in question are contained within a single JAR, you could consider consider something like what is described here: http://www.rgagnon.com/javadetails/java-0513.html

      A more reliable and portable way might be to maintain a text file that contains a list of the images to load. Load the list as a resource, then use the list to loop and load all images listed in the text file.

      Java — Changing from JFrame to JApplet, I’m trying to change some Java applications that I made using JFrame to JApplets so they can be placed on a website that I am also trying to make. I …

      How to make a layout for JApplet

      I am creating a simple Sudoku game. Since this is my first «big» i want to do everything myself (without the NetBeans interface designer that i normally use to make GUIs). So for the GUI i created a class that extends japplet and i drew a simple sudoku field in the paint() method.

      Now i need to make 81 text fields each of which will take in 1 number. How do i position them on the screen? Also, i was thinking of making an array so i’ll be able to change the enitre matrix of fields with one for loop.

      • Never draw directly in the paint method of a top-level component such as a JApplet, JFrame, JDialog, or the like.
      • If you need to do custom drawing, do this instead in the paintComponent(. ) method override of a component that extends JComponent such as a JPanel or JComponent itself.
      • Your problem doesn’t really require custom painting, at least not yet, and is much better and more simply solved by other means.
      • Use components such as JLabels, JTextFields, etc. and position them using the Swing layout managers (the other means noted above). You can find the tutorial on how to use this here: Laying Out Components in a Container
      • Layouts to focus on first include GridLayout for your Sudoku «cells» and BorderLayout for the overall GUI. Avoid the GridBagLayout and GroupLayout, at least when starting out.
      • Remember that you can create complex applications by nesting JPanels that each use a simple layout manager.
      • A simple way to «paint» gridlines is to set the background color of the the JPanel that uses GridLayout and holds the JTextFields to Color.BLACK, and be sure to give your GridBagLayout a small vertical and horizontal gap so that the black shows through. The tutorials listed above will show you how to do this.
      • If this were my application, I’d gear my GUI towards creating a JPanel that held the application. Then if I needed to display it in a JApplet, I’d create a very small application that subclasses JApplet and then in the init() method, add my Sudoku JPanel into the JApplet’s contentPane. This way, if I wanted to instead display my app in a JFrame, all I’d need to do would be to create another small class that creates a JFrame and add’s my Sudoku JPanel into the JFrame’s contentPane, then call pack() on the JFrame, and then setVisible(true) .

      Regarding your question on how to add a JPanel to a JApplet, again the tutorials will show you how to do this. If you haven’t linked to the tutorial’s big index, you will want to do so: The Really Big Index.

      A very simple example goes like so:

      import java.lang.reflect.InvocationTargetException; import javax.swing.*; public class MyApplet extends JApplet < @Override public void init() < try < SwingUtilities.invokeAndWait(new Runnable() < public void run() < getContentPane().add(new MyJPanel()); >>); > catch (InterruptedException e) < e.printStackTrace(); >catch (InvocationTargetException e) < e.printStackTrace(); >> > 

      Java Applet Basics, 1. init ( ) : The init ( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the …

      JApplet: setting the size of the frame

      I read that this is a tricky question because an applet is run in the browser. But I would like my applet window to always maintain the same size. (Right now working with Eclipse I can slide the size of the window.)

      For the moment I only do this:

      public class myJApplet extends JApplet < public void init() < this.setSize(800, 480); >> 

      Is there a way to add a this.setResizable(false) ?

      The applet will still be resizable when the HTML is loaded in the AppletViewer, but that is not relevant to deployment.

      Java — JApplets and JDialog, JApplets and JDialog. Ask Question Asked 11 years, 3 months ago. Modified 11 years, 3 months ago. Viewed 422 times 1 0. I have a japplet which …

      Источник

      Testing Flash Applications or Java Applets Using Sikuli

      When testing applications, sometimes it may be necessary to test the performance of a component that cannot be interacted with using Selenium. For example, when the application uses a map (such as Google or Yandex maps), or when there is a Flash application or Java applet. In such situations, a tool called Sikuli can be useful.

      What is Sikuli? In short, it is a symbiosis of the Robot class and the OpenCV computer vision library. More specifically, it is a tool for automating actions on an application using screenshots of the elements of this application. That is, you think over the actions to perform a specific operation in the application, create screenshots of the elements that will be used for performing these actions, and write code using the Sikuli API, which will use the prepared images and perform the necessary actions. For example, if you want to check the operation of the page print button , first you need to take a screenshot of this button, then, using the Sikuli API, find the region where this button is located, hover the cursor over this region and click this button.

      Installation

      To install the latest version of Sikuli 1.0.1 and sikuli-api 1.0.2:

      Run sikuli-setup. The following dialog box will appear:

      Then go to the folder indicated in the dialog box and run runSetup.cmd

      Click “OK” in the next dialog box.

      For programming in Java, just select items 4 and 6.
      Next, click “Setup Now”.

      A confirmation window will appear. Click “YES”.
      The installer will download the necessary files.

      Then add two .jar files (sikuli-api and sikuli-java) to the classpath.

      Usage example

      Let’s consider a simple example of using Sikuli. In this example, we will test changing the player resolution.

      To do this, we will perform the following steps:

      1. Open a browser with Selenium and go to the page with the player
      2. Click the “Settings” icon
      3. Check that the required menu has opened by finding the “Quality” item
      4. Set the resolution to “480p”
      5. Repeat steps 1 and 2
      6. Verify that the “480p” item is checked

      Code

      Since this article concentrates on getting started with this tool, we will not touch on the initialization and startup of Webdriver and navigation to the desired page. Instead, we’ll jump straight into the code that uses the Sikuli API.

      First, we create global objects required for testing, namely Firefox, desktop region and mouse:

      final App app = new App("Firefox"); final ScreenRegion s = new DesktopScreenRegion(); final Mouse mouse = new DesktopMouse();

      Then we focus on the browser:

      Declare the images that will be used in the test:

      final Target targetSettings = new ImageTarget(new File("settings.PNG")); final Target targetQuality = new ImageTarget(new File("quality.PNG")); final Target target480p = new ImageTarget(new File("480p.PNG")); final Target targetChecked480p = new ImageTarget(new File("checked_480p.PNG"));

      Find the “settings” icon and click it:

      findAndClick(s, mouse, targetSettings);

      Check that there is the “quality” label:

      Assert.assertNotNull(s.find(targetQuality));

      Find and click the “480p” menu item:

      findAndClick(s, mouse, target480p);

      Find and click the “settings” icon:

      findAndClick(s, mouse, targetSettings);

      Check that there is the “quality” label:

      Assert.assertNotNull(s.find(targetQuality));

      Verify that the “480p” item is checked:

      Assert.assertNotNull(s.find(targetChecked480p ));

      Since there is a repetitive sequence of actions in the test (i.e. find and click), they were moved to a separate method:

      private static void findAndClick(final ScreenRegion s, final Mouse mouse, final Target target)

      Conclusion

      Sikuli can certainly be useful in situations where Selenium fails, but it also has disadvantages. For example, the fact that you have to take screenshots of the elements required for testing, and if the application interface changes, you will have to take screenshots again. The scale of the element in the screenshot must match the scale of the element in the application, otherwise Sikuli simply won’t find this image. This problem can be worked around by gradually rescaling the image and trying to find the region with that image every time the scale was changed. There were a few cases when the image search worked incorrectly, resulting in finding a completely different element.

      Источник

      Читайте также:  Httpresponse java get body
Оцените статью