1、实验代码
1 import java.util.Collection; 2 import java.util.concurrent.TimeUnit; 3 import org.junit.*; 4 import static org.junit.Assert.*; 5 6 import org.junit.runner.RunWith; 7 import org.junit.runners.Parameterized; 8 import org.openqa.selenium.*; 9 import org.openqa.selenium.firefox.FirefoxDriver; 10 import org.openqa.selenium.support.ui.Select; 11 12 import java.util.*; 13 import java.io.*; 14 import java.util.NoSuchElementException; 15 16 @RunWith(Parameterized.class) 17 public class Lab2 { 18 private String input1; 19 private String input2; 20 private String expected; 21 22 private WebDriver driver; 23 private String baseUrl; 24 private boolean acceptNextAlert = true; 25 private StringBuffer verificationErrors = new StringBuffer(); 26 27 public Lab2(String input1, String input2, String expected) { 28 this.input1 = input1; 29 this.input2 = input2; 30 this.expected = expected; 31 } 32 33 //读取csv文件 34 public static List<String> importCsv(File file){ 35 List<String> dataList=new ArrayList<String>(); 36 37 BufferedReader br=null; 38 try { 39 br = new BufferedReader(new FileReader(file)); 40 String line = ""; 41 while ((line = br.readLine()) != null) { 42 dataList.add(line); 43 } 44 }catch (Exception e) { 45 }finally{ 46 if(br!=null){ 47 try { 48 br.close(); 49 br=null; 50 } catch (IOException e) { 51 e.printStackTrace(); 52 } 53 } 54 } 55 56 return dataList; 57 } 58 59 @Before 60 public void setUp() throws Exception { 61 driver = new FirefoxDriver(); 62 baseUrl = "http://121.193.130.195:8080"; 63 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 64 } 65 66 @Parameterized.Parameters 67 public static Collection<Object[]> getData() { 68 List<Object[]> output = new ArrayList<>(); 69 List<String> dataList=importCsv(new File("/Users/wuzhuo/Desktop/inputgit.csv")); 70 for(int i = 1 ; i < dataList.size(); i++) { 71 String[] split = dataList.get(i).split(","); 72 Object[] data = new Object[]{split[0], dataList.get(i).substring(4,10), split[2]}; 73 output.add(data); 74 System.out.println(data); 75 } 76 return output; 77 } 78 79 @Test 80 public void testLab2() throws Exception { 81 driver.get(baseUrl + "/"); 82 driver.findElement(By.id("name")).clear(); 83 driver.findElement(By.id("name")).sendKeys(this.input1); 84 driver.findElement(By.id("pwd")).clear(); 85 driver.findElement(By.id("pwd")).sendKeys(this.input2); 86 //new Select(driver.findElement(By.id("gender"))).selectByVisibleText("男"); 87 driver.findElement(By.id("submit")).click(); 88 Object git1 = driver.findElement(By.xpath("//*[@id=‘table-main‘]/tr[3]/td[2]")).getText(); 89 //System.out.println(git1); 90 //assertEquals(this.expected, git1); 91 System.out.println(git1.toString()); 92 assertEquals(this.expected, git1.toString()); 93 } 94 95 @After 96 public void tearDown() throws Exception { 97 driver.quit(); 98 String verificationErrorString = verificationErrors.toString(); 99 if (!"".equals(verificationErrorString)) { 100 fail(verificationErrorString); 101 } 102 } 103 104 private boolean isElementPresent(By by) { 105 try { 106 driver.findElement(by); 107 return true; 108 } catch (NoSuchElementException e) { 109 return false; 110 } 111 } 112 113 private boolean isAlertPresent() { 114 try { 115 driver.switchTo().alert(); 116 return true; 117 } catch (NoAlertPresentException e) { 118 return false; 119 } 120 } 121 122 private String closeAlertAndGetItsText() { 123 try { 124 Alert alert = driver.switchTo().alert(); 125 String alertText = alert.getText(); 126 if (acceptNextAlert) { 127 alert.accept(); 128 } else { 129 alert.dismiss(); 130 } 131 return alertText; 132 } finally { 133 acceptNextAlert = true; 134 } 135 } 136 }
2、实验结果
时间: 2024-11-08 12:41:16