軟體測試之Selenium Java WebDriver

來源:互聯網
上載者:User

標籤:使用   equals   datafile   body   after   static   int   exce   ica   

編寫Selenium Java WebDriver程式,測試inputgit.csv表格中的學號和git地址的對應關係
  package selenium2;import java.util.regex.Pattern;import java.util.concurrent.TimeUnit;import org.junit.*;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters;import static org.junit.Assert.*;import static org.hamcrest.CoreMatchers.*;import org.openqa.selenium.*;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.Select;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Arrays;import java.util.Collection;@RunWith(Parameterized.class)public class selenium2 {  private WebDriver driver;  private String baseUrl;  private boolean acceptNextAlert = true;  private StringBuffer verificationErrors = new StringBuffer();    private String name;  private String password;  private String github;public selenium2(String name, String password, String github)  {      this.name = name;      this.password = password;      this.github = github;  }    @Parameters  public static Collection data()  {      Object[][] oa = new Object[117][3];            File dataFile = new File("D:\\大三下\\軟體測試\\lab2\\inputgit.csv");      if(dataFile.exists() && dataFile.isFile())      {          try          {              FileReader fr = new FileReader(dataFile);              BufferedReader br = new BufferedReader(fr);              String content = br.readLine();              int count = 0;              String[] pieces = new String[3];              while ((content = br.readLine())!=null)              {                  pieces = content.split(",");                  oa[count][0] = pieces[0];                  oa[count][1] = pieces[0].substring(4);                  oa[count][2] = pieces[2];                  count = count + 1;              }              br.close();              fr.close();          }catch(FileNotFoundException e)          {              System.out.println("Cann‘t find file, error info: " + e.getMessage());          }catch(IOException e)          {              e.printStackTrace();          }      }      else      {          System.out.println("Can not find file!");      }      return Arrays.asList(oa);  }  @Before  public void setUp() throws Exception {    System.setProperty("webdriver.gecko.driver", "D:\\大三下\\軟體測試\\lab2\\geckodriver.exe");    driver = new FirefoxDriver();    baseUrl = "http://121.193.130.195:8080";    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);      }  @Test  public void test() throws Exception {    driver.get(baseUrl + "/");    driver.findElement(By.id("name")).clear();    driver.findElement(By.id("name")).sendKeys(name);    driver.findElement(By.id("pwd")).clear();    driver.findElement(By.id("pwd")).sendKeys(password);    driver.findElement(By.id("submit")).click();    assertEquals(github, driver.findElement(By.xpath("//tbody[@id=‘table-main‘]/tr[3]/td[2]")).getText());  }  @After  public void tearDown() throws Exception {    driver.quit();    String verificationErrorString = verificationErrors.toString();    if (!"".equals(verificationErrorString)) {      fail(verificationErrorString);    }  }  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;    }  }}

  注意:

1.所匯入jar包為:

2.使用的FF為版本40

3.當junit測試成功時,117個資料將自動檢驗並成功顯示

 

軟體測試之Selenium Java WebDriver

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.