How to automate a swing java web start application which runs clicking a link into a web application, which is automated with Selenium WebDriver?

StackOverflow https://stackoverflow.com/questions/23681949

Question

I have a typical web application, which is automated by Selenium WebDriver. My problem is a particular case of automation in which I have a link, which runs a swing app with Java Web Start, and I would like to transfer the control of the automation to the Swing app. is this possible? What tool can I use to do it? and, how can I do it? Thanks in advance.

Was it helpful?

Solution

  1. Click the jnlp file link in webdriver, save jnlp file to disk;
  2. Run the webstart app from jnlp;
  3. Capture opened app and use it for test.

It can be done by using following libraries:

You can probably do the same trick with other AWT/Swing testing tool, but uispec4j allows to intercept webstart app executed from jnlp, you don't need to run the app by calling main() and you don't need to have your webstart app source code in your testing code repo. I had problems to achieve this with other libs, including Jemmy.

Here is what worked for me:

import java.io.File;    
import javax.swing.JTextField;  
import netx.jnlp.JNLPFile;
import netx.jnlp.Launcher;
import org.junit.Assert;
import org.junit.Test;
import org.uispec4j.Trigger;
import org.uispec4j.UISpecAdapter;
import org.uispec4j.UISpecTestCase;
import org.uispec4j.Window;
import org.uispec4j.interception.WindowInterceptor;

public class WebstartTest extends UISpecTestCase {

    @Test
    public void test() throws Exception {
        // click your webdriver link, save the jnlp file to disk
        final File file = new File("file.jnlp");
        final JNLPFile jnlp = new JNLPFile(file.toURI().toURL());

        // adapter is a UISpec4j way to allow capturing windows created in 
        // non-standard way, exactly what we need.
        this.setAdapter(new UISpecAdapter() {
            @Override
            public Window getMainWindow() {
                return WindowInterceptor.run(new Trigger() {
                    @Override
                    public void run() throws Exception {
                        // running jnlp by netx launcher 
                        Launcher launcher = new Launcher();
                        launcher.setCreateAppContext(false);
                        launcher.launch(jnlp);
                    }
                });
            }
        });

        Window w = this.getMainWindow();

        // verify if window's components are there
        Assert.assertEquals("text", ((JTextField) w.getSwingComponents(JTextField.class)[0]).getText());

        // manipulate window components...
    }
}

Note: uispec4j will intercept the window, so it won't become visible. It wasn't a problem for me, so I didn't investigate if making it visible is possible.

OTHER TIPS

Since Java web start applications are basically regular Java applications and are not appearing in the DOM, you can't access them using WebDriver.

The best tool I know for testing AWT/Swing application is Jemmy. I usually use it for testing standalone Swing applications, but I am sure you can use it also for applications that are launched using the web start mechanism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top