如何自动化运行单击链接到 Web 应用程序的 Swing Java Web 启动应用程序,该应用程序由 Selenium WebDriver 自动化?

2022-01-16 00:00:00 selenium webdriver automation java swing

我有一个典型的 Web 应用程序,它由 Selenium WebDriver 自动化.我的问题是一个特殊的自动化案例,其中我有一个链接,它使用 Java Web Start 运行一个 Swing 应用程序,我想将自动化控制权转移到 Swing 应用程序.这可能吗?我可以使用什么工具来做到这一点?而且,我该怎么做?提前致谢.

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.

推荐答案

  1. 点击webdriver中的jnlp文件链接,将jnlp文件保存到磁盘;
  2. 从 jnlp 运行 webstart 应用;
  3. 捕获打开的应用并将其用于测试.

可以使用以下库来完成:

It can be done by using following libraries:

  • netx (http://jnlp.sourceforge.net/netx/) - 用于运行 webstart来自 jnlp 的应用程序
  • uispec4j (http://www.uispec4j.org/) - 用于拦截创建的 webstart 窗口和操作窗口元素
  • 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

你可能可以用其他 AWT/Swing 测试工具做同样的事情,但是 uispec4j 允许拦截从 jnlp 执行的 webstart 应用程序,你不需要通过调用 main() 来运行应用程序,你不需要在您的测试代码仓库中包含您的 webstart 应用程序源代码.我在使用其他库(包括 Jemmy)时遇到了问题.

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.

这对我有用:

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...
    }
}

注意:uispec4j 会拦截窗口,所以它不会变得可见.这对我来说不是问题,所以我没有调查是否可以让它可见.

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.

相关文章