Software Testing(软件测试 实验二)Lab2 Seleium

Selenium Experiment

1、 Install the SeleiumIDE plugin

(1)   Download firefox40.0.2 from

https://ftp.mozilla.org/pub/firefox/releases/40.0.2/win32/zh-CN/

 

 

Add install the firefox browser following the instruction.

(2)   Add Seleium plugin

Search for “Seleium add on” and “Add to Firefox”

 

 

      

Then we can find the Seleium from the bar of firefox.

 

2、Record and export the script.

(1)     Open the SeleiumIDE

 

(2)Click the red record button and begin to record the script.

 

       Add a command

      

       And run the test case.

      

       Export the test case.

   

 

3、 Test whether the github URL from the website is the same as the URL from the csv file for one student.

(1)   New a project

(2)   Add the relative jars(Seleium, Junit, javasav, …)

(3)   Copy the export script to my project

(4)   Code to finish the whole function

Here is the code:

 

package cn.tju.scs.wyh;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
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 org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.Select;

import com.csvreader.CsvReader;

@RunWith(Parameterized.class)
public class GitURL {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  private String sid;
  private String expected;
  private static List<String[]> csvList;

  public GitURL(String sid, String expected){
    this.sid = sid;
    this.expected = expected;
  }
  
  @Before
  public void setUp() throws Exception {
      File pathToBinary = new File("E:\\firefox\\firefox.exe");
      FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
      FirefoxProfile firefoxProfile = new FirefoxProfile();       
      driver = new FirefoxDriver(ffBinary,firefoxProfile);
      baseUrl = "http://121.193.130.195:8080";
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testGitURL() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.id("name")).clear();
        driver.findElement(By.id("name")).sendKeys(this.sid);
        driver.findElement(By.id("pwd")).clear();
        driver.findElement(By.id("pwd")).sendKeys(this.sid.substring(4));
        new Select(driver.findElement(By.id("gender"))).selectByVisibleText("女");
        driver.findElement(By.id("submit")).click();
        assertEquals(this.expected, 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);
    }
  }

  @Parameters
  public static Collection<String[]> testData() throws FileNotFoundException, IOException{
      int length = getGitURLFromCSV();
      String[][] data = new String[length][];
      for(int j = 0; j < length; j++){
          data[j] = new String[]{csvList.get(j)[0], csvList.get(j)[2]};
      }
      return Arrays.asList(data);
  }
  
  private static int getGitURLFromCSV() throws IOException{
      CsvReader reader = new CsvReader("F:\\Material\\inputgit.csv", ',', Charset.forName("GBK"));
      csvList = new ArrayList<String[]>();  
      reader.readHeaders();   
      int csvLength = 0;
      while (reader.readRecord()) {   
          csvList.add(reader.getValues());
          csvLength++;
      }  
      reader.close(); 
      return csvLength;
  }
  
  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;
    }
  }
}

And the result:

 

转载于:https://www.cnblogs.com/wazxser/p/6612361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值