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

后端 未结 2 474
走了就别回头了
走了就别回头了 2020-12-17 06:49

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

相关标签:
2条回答
  • 2020-12-17 07:20

    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.

    0 讨论(0)
  • 2020-12-17 07:25
    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:

    • netx (http://jnlp.sourceforge.net/netx/) - for running webstart application from jnlp
    • uispec4j (http://www.uispec4j.org/) - for intercepting created webstart window and manipulating window elements

    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.

    0 讨论(0)
提交回复
热议问题