如何從 CSV 讀取測試資料執行自動化測試

如何從 CSV  讀取測試資料執行自動化測試

這篇文章主要用一個網站登入為範例,說明如何用 CSV 建立測試資料,

並且讓Selenium/Java 讀取每一筆測試資料當作每一個測試個案執行,

直到所有的測試資料被讀取完畢為止。這樣的測試模式又稱為 “DDT (Data Driven Testing)” 或是 Parameterized Test

為什麼需要這樣的測試呢? 想想看,如果我們需要測試一個網站的登入。需要測試資料有帳號密碼。

如果有 100組測試帳號資料,也就表示有 100組測試個案,測試程式也就要執行 100 次。

那麼 DDT跟執行 100 次回圈有什麼不同?

因為這樣的測試,

每一個測試個案都是取決於測試資料,

每一次的測試程式的執行都相互獨立,

每一次的測試個案都有各至的測試結果。

DDT concept

 

要怎樣實作呢? 這裡舉Java 為例子

測試情境

我們以登入測試為例子。這個網站有帳號的登入。根據不同的登入資料會有不同的錯誤訊息。

因此,我們要如何將登入的帳號密碼,透過CSV的方式,進行自動化測試?

https://www.directpass.com/signin

Sign-In test

 

CSV 資料檔

首先,定義一個測試資料檔,檔名testingAccount.csv

測試資料檔主要提供帳號與密碼,例如測試資料內容如下:

abc01@yahoo.com,1234
abc02@yahoo.com,1234

Java/Selenium 程式範例

[pastacode lang=”java” message=”Selenium Data Driven Testing” highlight=”” provider=”manual”]

package mySelenium;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;


@RunWith(value = Parameterized.class)
public class JunitCSVSample {
	
	private static WebDriver driver;
	private static StringBuffer verificationErrors = new StringBuffer();

	private String account;
	private String pwd;
	
	@Parameters
    public static Collection testData() throws IOException {
        return  getTestData("d:\\myJava\\testingAccount.csv");
    }
	
	public JunitCSVSample(String account,String pwd) {
		this.account = account;
		this.pwd = pwd;
		
	}
	
	public static Collection<String[]> getTestData(String fileName) throws IOException {
		List<String[]> records = new ArrayList<String[]>();
		String record;

		BufferedReader file = new BufferedReader(new FileReader(fileName));
		while ((record=file.readLine())!=null) {
		 	String fields[] = record.split(",");
			records.add(fields);
		}
		file.close();
		return records;
	}
	
	@BeforeClass
	public static void setUp() throws Exception { 
		// Create a new instance of the Firefox driver
		driver = new FirefoxDriver();
		driver.get("https://www.directpass.com/signin");
	}

	@Test
	public void testingLogin() throws Exception {
		try {
			WebElement accountField = driver.findElement(By.id("account"));
			accountField.clear();
			accountField.sendKeys(account);
			
			WebElement pwdField = driver.findElement(By.id("password"));
			pwdField.clear();
			pwdField.sendKeys(pwd);
	
			WebElement signInButton = driver.findElement(By.id("signin-form-submit"));
			signInButton.click();
		
			WebElement helptips = driver.findElement(By.xpath("//div[@class='help-inline']"));
			assertEquals("Please provide password in the correct format.", helptips.getText());
						
			
		} catch (Error e) {
			//Capture and append Exceptions/Errors
			verificationErrors.append(e.toString());
			System.err.println("Assertion Fail " + verificationErrors.toString());
		}
	}
	
	@AfterClass
	public static void tearDown() throws Exception {
		//Close the browser
		driver.quit();
		
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}

[/pastacode]

程式說明

這段程式主要用來讀取 CSV 變數內容,例如 CSV中的資料為帳號與密碼,

因此,透過 getTestData就可以讀取 CSV 內容,並且將變數存放在account, pwd

如果讀者要參考使用,getTestData 這段程式可以直接 copy 使用。變數的部分僅需依照CSV資料的需要稍加修改即可。如粗體如下:

@Parameters
public static Collection testData() throws IOException {
return getTestData(“d:\\myJava\\testingAccount.csv“);
}public JunitCSVSample(String account,String pwd) {
this.account = account;
this.pwd = pwd;

}

public static Collection<String[]> getTestData(String fileName) throws IOException {
List<String[]> records = new ArrayList<String[]>();
String record;BufferedReader file = new BufferedReader(new FileReader(fileName));
while ((record=file.readLine())!=null) {
String fields[] = record.split(“,”);
records.add(fields);
}
file.close();
return records;
}

如何使用變數呢?

資料由CSV  讀取之後,讀到變數 account與pwd

accountField.sendKeys(account);
pwdField.sendKeys(pwd);

Junit執行

透過 Junit 執行之後,每讀一筆 CSV 的資料,就會視為一個獨立的測試案例。

小結

我們透過 Junit + Selenium/Java + 定義@Parameters與 getTestData()

實作一個如何讀取 CSV自動化測式的案例。

 

如果測試個案的執行會取決輸入資料,那麼我們就會將測試資料定義在 CSV,

這樣一來,未來如果要新增或是修改測試資料,只要直接修改 CSV 即可。

透過修改CSV的修改,也減去程式維護的成本,同時也讓整個自動化測試更有彈性。

 

Leave a Reply

Your email address will not be published. Required fields are marked *