软件测试第二次实验

1.     安装seleniumIDE:

2.     使用SeleniumIDE录制脚本和导出脚本

3. (1)下载Selenium Java 2.53.0版本,并在Eclipse中导入。

 

下载地址:http://selenium-release.storage.googleapis.com/index.html?path=2.53/。下载之后解压,得到如下目录结构:

然后把上面解压出来的文件拷到新建的project目录下,目录结构如下图:

2)添加build path,项目目录右键-->Build Path--> config build path-->Java Build Path-->Libraries-->Add JARs。把libs文件夹下的jar包全部添加上,再添加selenium-java-2.53.0.jar和selenium-java-2.53.0-srcs.jar。

3)编写代码:

package lab2;

 

import java.nio.charset.Charset;

import java.util.regex.Pattern;

import java.util.concurrent.TimeUnit;

 

import org.junit.*;

 

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 com.csvreader.CsvReader;

 

public class TestGit {

  private WebDriver driver;

  private String baseUrl;

  private boolean acceptNextAlert = true;

  private StringBuffer verificationErrors = new StringBuffer();

  private String id = null;

  private String pwd = null;

  private String gitUrl = null;

 

  @Before

  public void setUp() throws Exception {

   

    driver = new FirefoxDriver();

    baseUrl = "https://psych.liebes.top/st";

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  }

 

  @Test

  public void testGit() throws Exception {

      CsvReader cinnew CsvReader("E:/input.csv", ',',Charset.forName("GBK"));

      cin.readHeaders();

      while(cin.readRecord()){

        id = cin.get(0);

        pwd = id.substring(4, 10);

        gitUrl = cin.get(2);

        driver.get(baseUrl + "/");

        driver.findElement(By.id("Username")).clear();

        driver.findElement(By.id("Username")).sendKeys(id);

        driver.findElement(By.id("Password")).clear();

        driver.findElement(By.id("Password")).sendKeys(pwd);

        driver.findElement(By.id("Submit")).click();

        String gitUrls = driver.findElement(By.xpath("//tbody[@id = 'table-main']/tr[3]/td[2]")).getText();

        if (!gitUrl.equals(gitUrls)){

            System.out.println(id);

            System.out.println(gitUrl);

            System.out.println(gitUrls);

            continue;

           

        }

        assertEquals(gitUrl,gitUrls);

    }

  }

 

  @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;

    }

  }

}

posted @ 2018-04-16 23:28  runtimerrors  阅读(167)  评论(0编辑  收藏  举报