>单击webdriver中的jnlp文件链接,将jnlp文件保存到磁盘;
>从jnlp运行webstart应用程序;
>捕获打开的应用程序并将其用于测试.
可以使用以下库来完成:
> netx(http://jnlp.sourceforge.net/netx/) – 用于从jnlp运行webstart应用程序
> uispec4j(http://www.uispec4j.org/) – 用于拦截创建的webstart窗口和操作窗口元素
您可以使用其他AWT / Swing测试工具执行相同的操作,但是uispec4j允许拦截从jnlp执行的webstart应用程序,您不需要通过调用main()来运行应用程序而您不需要使用您的webstart测试代码库中的应用程序源代码.我在与其他libs(包括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将拦截窗口,因此它不会变得可见.这对我来说不是问题,所以我没有调查是否有可能让它可见.