Selenium上机实验
- 安装Selenium IDE 附加组件
到firefox应用商店下载Selenium IDE,安装后打开如图所示:
- 配置Selenium WebDriver环境
向工程内导入如下的jar包(可以准备一个javacsv来辅助后面的实验)
- 录制脚本
使用Selenium IDE录制脚本后可以很方便的导出,可以直接在导出的代码上进行修改。
打开Selenium IDE
点击右上角的红点开始录制
进入网页 http://121.193.130.195:8080/ 输入账号密码登陆
点击登录跳转后,选择页面内的github地址,右键选择assertText
再次点击红点完成录制
- 导出脚本
点击Optinons->Options,勾选Enable experimental features
通过Options-->Format选择要导出成的相应格式。
选择Java / JUnit 4/ WebDriver
得到生成的Java文件,复制到工程中完成脚本导出
- 编写完整代码
1 package com.WebDriveTest; 2 3 import java.util.regex.Pattern; 4 import java.io.IOException; 5 import java.nio.charset.Charset; 6 import java.util.Arrays; 7 import java.util.Collection; 8 import java.util.concurrent.TimeUnit; 9 import org.junit.*; 10 import org.junit.runner.RunWith; 11 import org.junit.runners.Parameterized; 12 import org.junit.runners.Parameterized.Parameters; 13 14 import static org.junit.Assert.*; 15 import static org.hamcrest.CoreMatchers.*; 16 import org.openqa.selenium.*; 17 import org.openqa.selenium.firefox.FirefoxDriver; 18 import org.openqa.selenium.support.ui.Select; 19 20 import com.csvreader.CsvReader; 21 22 @RunWith(Parameterized.class) 23 public class WebDriveTest { 24 private WebDriver driver; 25 private String baseUrl; 26 private boolean acceptNextAlert = true; 27 private StringBuffer verificationErrors = new StringBuffer(); 28 private String id, pwd,address; 29 30 public WebDriveTest(String id, String address) 31 { 32 this.id = id; 33 this.pwd = id.substring(4); 34 this.address = address; 35 } 36 37 @Before 38 public void setUp() throws Exception { 39 driver = new FirefoxDriver(); 40 baseUrl = "http://121.193.130.195:8080"; 41 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 42 } 43 44 @Parameters 45 public static Collection<Object[]> getData() throws IOException { 46 Object[][] obj = new Object[118][]; 47 CsvReader r = new CsvReader("D:\\CIA\\大三下\\软测\\inputgit.csv", ',', 48 Charset.forName("GBK")); 49 int count = 0; 50 r.readHeaders(); 51 while(r.readRecord()){ 52 obj[count] = new Object[]{r.get(0), r.get(2)}; 53 count++; 54 } 55 return Arrays.asList(obj); 56 } 57 58 @Test 59 public void testUntitled2() throws Exception { 60 driver.get(baseUrl + "/"); 61 driver.findElement(By.id("name")).clear(); 62 driver.findElement(By.id("name")).sendKeys(this.id); 63 driver.findElement(By.id("pwd")).clear(); 64 driver.findElement(By.id("pwd")).sendKeys(this.pwd); 65 driver.findElement(By.id("submit")).click(); 66 assertEquals(this.address, driver.findElement(By.xpath("//tbody[@id='table-main']/tr[3]/td[2]")).getText()); 67 } 68 69 @After 70 public void tearDown() throws Exception { 71 driver.quit(); 72 String verificationErrorString = verificationErrors.toString(); 73 if (!"".equals(verificationErrorString)) { 74 fail(verificationErrorString); 75 } 76 } 77 78 private boolean isElementPresent(By by) { 79 try { 80 driver.findElement(by); 81 return true; 82 } catch (NoSuchElementException e) { 83 return false; 84 } 85 } 86 87 private boolean isAlertPresent() { 88 try { 89 driver.switchTo().alert(); 90 return true; 91 } catch (NoAlertPresentException e) { 92 return false; 93 } 94 } 95 96 private String closeAlertAndGetItsText() { 97 try { 98 Alert alert = driver.switchTo().alert(); 99 String alertText = alert.getText(); 100 if (acceptNextAlert) { 101 alert.accept(); 102 } else { 103 alert.dismiss(); 104 } 105 return alertText; 106 } finally { 107 acceptNextAlert = true; 108 } 109 } 110 }
- 实验结果