(5)在文件夹里找到seleniumSuite.java,其内容如下,稍加修改就可作为JUnit测试类。修改后的代码https://github.com/yongheng20/SeleniumJavaWebDriver
1 package com.example.tests; 2 3 import java.util.regex.Pattern; 4 import java.util.concurrent.TimeUnit; 5 import org.junit.*; 6 import static org.junit.Assert.*; 7 import static org.hamcrest.CoreMatchers.*; 8 import org.openqa.selenium.*; 9 import org.openqa.selenium.firefox.FirefoxDriver; 10 import org.openqa.selenium.support.ui.Select; 11 12 public class SeleniumSuite { 13 private WebDriver driver; 14 private String baseUrl; 15 private boolean acceptNextAlert = true; 16 private StringBuffer verificationErrors = new StringBuffer(); 17 18 @Before 19 public void setUp() throws Exception { 20 driver = new FirefoxDriver(); 21 baseUrl = "http://www.ncfxy.com/"; 22 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 23 } 24 25 @Test 26 public void testSeleniumSuite() throws Exception { 27 driver.get(baseUrl + "/"); 28 driver.findElement(By.id("name")).clear(); 29 driver.findElement(By.id("name")).sendKeys("0123456789"); 30 driver.findElement(By.id("pwd")).clear(); 31 driver.findElement(By.id("pwd")).sendKeys("000000"); 32 driver.findElement(By.id("submit")).click(); 33 } 34 35 @After 36 public void tearDown() throws Exception { 37 driver.quit(); 38 String verificationErrorString = verificationErrors.toString(); 39 if (!"".equals(verificationErrorString)) { 40 fail(verificationErrorString); 41 } 42 } 43 44 private boolean isElementPresent(By by) { 45 try { 46 driver.findElement(by); 47 return true; 48 } catch (NoSuchElementException e) { 49 return false; 50 } 51 } 52 53 private boolean isAlertPresent() { 54 try { 55 driver.switchTo().alert(); 56 return true; 57 } catch (NoAlertPresentException e) { 58 return false; 59 } 60 } 61 62 private String closeAlertAndGetItsText() { 63 try { 64 Alert alert = driver.switchTo().alert(); 65 String alertText = alert.getText(); 66 if (acceptNextAlert) { 67 alert.accept(); 68 } else { 69 alert.dismiss(); 70 } 71 return alertText; 72 } finally { 73 acceptNextAlert = true; 74 } 75 } 76 } |