selenium2.0集成测试案例
webDriver模拟点击对web工程测试还是挺方便的.
package suite; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.NoAlertPresentException; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class WebTest { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { // System.setProperty( // "webdriver.chrome.driver", // "C:\\Program Files (x86)\\Google\\Chrome\\Application\\48.0.2564.109\\chromedriver_x64.exe"); // driver = new ChromeDriver(); driver = new HtmlUnitDriver(true); baseUrl = "http://10.10.10.10:8080/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testWeb() throws Exception { driver.get(baseUrl + "strutsDemo/index.jsp"); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("receiver")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("sender")).click(); driver.findElement(By.id("receiver")).click(); driver.findElement(By.id("start")).click(); driver.findElement(By.id("add")).click(); driver.findElement(By.id("show")).click(); } @After public void tearDown() throws Exception { driver.quit(); } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } } }
程序将会模拟打开浏览器点击并测试.测试代码是用selenium ide录制过来的,想要自己写也可以,只不过麻烦一些.如果当前工程部署在linux主机上要把chrome改成
HTMLUnitDriver,代码中我注释了,毕竟linux安装不了chrome,这边用chrome主要是为了看得清楚.