UI测试自动化可以将繁琐的UI测试持续管理起来,做到测试case和具体case实现相分离,便于case的跟踪和即时反馈。
添加case实现:
一、To give you a little history:
Jenkins是在hudson基础上发展起来的优秀持续集成工具.
Selenium2在selenium1的基础之上增加了强大的webdriver驱动,使得测试case可以完全脱离Selenium-Server.
二、Prerequisites:
Subversion (http://subversion.tigris.org/getting.html)
Selenium2 Webdriver(http://seleniumhq.org/projects/webdriver/)
Jenkins(http://jenkins-ci.org/)
Maven2(http://maven.apache.org/)
Vncserver(http://www.realvnc.com/)
Install vncserver:
vncserver :1
xhost +localhost
Install Jenkins
service jenkins restart
检查jenkins的系统属性里是否有DISPLAY=:1
Install maven2
mvn –version
三、新建maven项目(即新建pom.xml)
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>web_driver</groupId>
<artifactId>web_driver</artifactId>
<version>0.0.2-SNAPSHOT</version>
<name>web_driver</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<scope>compile</scope>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>1.0-alpha-3</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
<xmlOutput>true</xmlOutput>
<findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>1.0-alpha-3</version>
<inherited>true</inherited>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>surefire-report-maven-plugin</artifactId>
<inherited>true</inherited>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
四、Add the webdriverTestsproject into subversion:
添加case:
public class Demo {
public String case10()
{
return "google查询";
}
}
添加case实现:
public class WebTest {
Demo demo = new Demo();
private static WebDriver driver;
private WebElement element;
private String webUrl = "http://www.google.com/";
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass
public static void setUpBeforeClass() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
@Test
public void testCase10() {
// 查询
System.out.println(demo.case10());
driver.get(webUrl)
inputElement = driver.find_element_by_name("q")
inputElement.send_keys("Cheese!")
inputElement.submit()
print driver.title
try:
WebDriverWait(driver, 10).until(lambda driver :
driver.title.lower().startswith("cheese!"))
print driver.title
finally:
driver.quit()
}
}
五、将maven项目上传至svn中,svn地址在下面配置jenkins中会用到。
六、Running the testcases remotely and continuously:
jenkins中新建maven2项目,一路配置下来,在Goals and options中填写:clean -Dtest=com.google.WebTest emma:emma findbugs:findbugs cobertura:cobertura
点击立即构建即可运行。