您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium之利用Excel實現(xiàn)參數(shù)化
作者:網(wǎng)絡轉載 發(fā)布時間:[ 2014/11/25 13:14:35 ] 推薦標簽:軟件測試 Selenium 功能測試

  第三步:新建一個TestNg Class,把excel數(shù)據(jù)填寫到測試界面,具體代碼如下:
  CODE:
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import File.ExcelWorkBook;
public class LoginCenter {
private WebDriver driver;
private String url;
String sourceFile="你文件的路徑和文件名稱";
@BeforeClass
public void testBefore(){
//設置firefox瀏覽器
FirefoxProfile file=new FirefoxProfile(new File("C:\Users\qinfei\AppData\Roaming\Mozilla\Firefox\Profiles\t5ourl6s.selenium"));
driver=new FirefoxDriver(file);
url="你的測試地址";
}
@Test
public void login() throws Exception{
//初始化ExcelWorkBook Class
ExcelWorkBook excelbook=new ExcelWorkBook();
//進入到你的測試界面
driver.get(url);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
try{
//把取出的username放在userlist集合里面
List<string> userList=excelbook.readUsername(sourceFile);
//把取出的password放在passlist集合里面
List</string><string> passList=excelbook.readPassword(sourceFile);
//把取出來的值,輸入到界面的輸入框中
int usersize=userList.size();
for(int i=0;i<usersize ;i++){
//通過css定位到username輸入框
WebElement username=driver.findElement(By.cssSelector("input[name="j_username"]"));
//通過css定位到password輸入框
WebElement password=driver.findElement(By.cssSelector("input[name="j_password"]"));
//通過xpath定位登錄按鈕
WebElement submit=driver.findElement(By.xpath("//button//span[contains(text(),'登錄')]"));
//清除username輸入框的內(nèi)容
username.clear();
//把list中數(shù)據(jù)一個一個的取出來
String name=userList.get(i);
//然后填寫到username輸入框
username.sendKeys(name);
for(int j=0;j<passList.size();j++){
password.clear();
String pass=passList.get(j);
password.sendKeys(pass);
}
//點擊登錄按鈕
submit.click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
//通過xpath定位登出按鈕
WebElement logoutButton=driver.findElement(By.xpath("//button//span[contains(text(),'登出')]"));
logoutButton.click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
}catch(Exception e){
e.printStackTrace();
}
}
}

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd