Software Testing 5 Using Selenium to automatically test.

本文介绍如何使用Selenium进行自动化测试,包括安装Selenium IDE及Java环境配置,并通过实例演示了如何从CSV文件读取数据并自动验证用户名密码登录后的Git链接。

Using Selenium to automatically test.

1.Install Selenium

Download Selenium IDE for FireFox in this website:  https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/

If succeeded, you can see the selenium IDE like below.

Open this IDE and Click the red button to record.

After rocording, you can export the process into a java program.

2. Install Selenium in eclipse

Download javacsv.jar and selenium-java-2.53.0.zip from the offcial website.

unzip them and import them into your project, just like the way you import Junit in the previous lab.

3. automatically testing 

Write the program and run it. 

The program will open your firefox and test the Username and password automatically.

package P1;
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 Test {
	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 {
		  System.setProperty("webdriver.firefox.bin", "D:/Firefox/firefox.exe");
	    driver = new FirefoxDriver();
	    baseUrl = "http://121.193.130.195:8080";
	    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	  }

	  @org.junit.Test
	  public void testGit() throws Exception {
	      CsvReader cin =  new CsvReader("E:/大三下/软件测试/inputgit.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("name")).clear();
	        driver.findElement(By.id("name")).sendKeys(id);
	        driver.findElement(By.id("pwd")).clear();
	        driver.findElement(By.id("pwd")).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;
	    }
	  }


}

  

 

This green bar shows that the program runs without faults.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值