基于浏览器的Applet使用开源方法进行自动化测试一直是一个难题。希望这篇文章能够为你提供一个开源的参考方案。
如果你使用Java Swing进行Applet或者GUI开发,你就可能使用过fest进行单元测试或者Acceptance测试。fest简单易用,它的用户包括:Google, Square, Eclipse Foundation, Oracle, IBM, Guidewire等等。同时许多公司都在使用Selenium进行网页方面的测试。如何能够把两者结合起来,就是我们讨论的话题。有老外开发了一个.net 的FEST-Selenium,能从一定程度上解决这个问题,他有以下不足之处:
1,不支持java,只能使用.net
2,我们不能重用fest简单的语法。
这个festselenium项目就是能够重用以前fest的测试代码,差不多不要修改就能通过selenium进行远端控制。festselenium自动实现client<->selenium<->fixture<->SUT的最终交互。
这是我以前写的一个自动测试框架,通过http://code.google.com/p/festselenium/ 能访问源代码。
dialog.comboBox("domain").select("Users");
dialog.textBox("username").enterText("alex.ruiz");
dialog.button("ok").click();
除了要新建一个selenium连接,用户几乎感觉不到中间已经多了selenium这一层
// selenium setup
selenium = new DefaultJavaSelenium("localhost",4444, browserString , url);
selenium.start();
selenium.open(url);
// get the appletfixure to control fest JAppletFixture
AppletFixture dialog = selenium.applet(LIST_APPLET_ID)
// fest similar API for autmation testing
dialog.comboBox("domain").select("Users");
dialog.textBox("username").enterText("alex.ruiz");
dialog.button("ok").click();
step1~3和6和以前fest进行applet一样。4除了要新建一个selenium连接之外和fest测试代码是一样的。4到5就是这个项目的核心,完成festselenium测试代码-〉selenium调用-〉fest fixture的自动转换。
package com.nemo.festselenium.demo;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class DemoApplet extends JApplet implements AdjustmentListener{
private static final long serialVersionUID = 1L;
JScrollBar slider;
JTextComponent text;
JPanel panel;
public void adjustmentValueChanged(AdjustmentEvent e) {
text.setText(new Integer(slider.getValue()).toString());
}
public DemoApplet(){
slider = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 100);
text = new JTextArea("0");
panel = new JPanel();
panel.add(text);
panel.add(slider);
add(panel);
slider.addAdjustmentListener(this);
}
}
package com.nemo.festselenium.demo;
import org.fest.swing.fixture.JAppletFixture;
public class JTestFixture extends DemoApplet {
JAppletFixture fixture;
public JTestFixture() {
super();
setName("demo_applet");
fixture=new JAppletFixture(this);
}
public JAppletFixture getTestFixture() {
return fixture;
}
}
<html>
<head>
<title>Test</title>
</head>
<body>
<div>
<object id="demo_applet" width="300" height="150" type="application/x-java-applet">
<param name="java_code" value="com.nemo.festselenium.demo.JTestFixture.class" />
<param name="java_archive" value="fest-swing-1.2a3.jar,fest-assert-1.1.jar,fest-util-1.1.jar,fest-reflect-1.2-SNAPSHOT.jar" />
</object>
</div>
</body>
</html>
package com.nemo.festselenium.demo;
import static org.junit.Assert.*;
import org.junit.*;
import com.nemo.festselenium.fixture.*;
import com.nemo.festselenium.selenium.*;
public class DemoTest {
DefaultJavaSelenium selenium;
@Before
public void setUp() {
selenium = new DefaultJavaSelenium("localhost",4444, "*iexplore" , "http://localhost:8080");
}
@After
public void tearDown() {
selenium.stop();
}
@Test
public void test() {
selenium.start();
selenium.open("demo_applet.html");
selenium.windowMaximize();
AppletFixture fixture = selenium.applet("demo_applet");
ScrollBarFixture scrollbar = fixture.scrollBar();
TextComponentFixture text = fixture.textBox();
scrollbar.scrollUnitUp();
assertEquals(text.text(), "1");
}
}
15:19:56.179 INFO - Command request: getNewBrowserSession[*iexplore, http://loca
15:20:04.659 INFO - Command request: open[demo_applet.html, ] on session a84fc32
15:20:08.223 INFO - Command request: windowMaximize[, ] on session a84fc3216d6c4
1f1865ab7a710189814
15:20:08.847 INFO - Command request: getEval[navigator.userAgent, ] on session a
84fc3216d6c41f1865ab7a710189814
15:20:09.378 INFO - Command request: getEval[window.document.getElementById("dem
o_applet").getTestFixture().scrollBar().scrollUnitUp(), ] on session a84fc3216d6
c41f1865ab7a710189814
15:20:10.205 INFO - Command request: getEval[window.document.getElementById("dem
o_applet").getTestFixture().textBox().text(), ] on session a84fc3216d6c41f1865ab
7a710189814