2017-06-30 67 views
0

我使用黃瓜,硒和Excel作爲我的數據文件Excel文件,我的問題是我怎麼可以運行基於我對Excel中的數據我有文件的多個時間。例如,我在Excel中有10行數據,並希望逐一運行它,在第一行數據移動到下一行並執行它之後。黃瓜硒使用數據表

特性文件: 方案:登錄

Given I open the browser and access this URL 
When I enter the "<Username>" and "<Password>" 
Then I am able to login 

步驟定義: 公共類登錄{

WebDriver driver = null; 
String url;   


@Given("^I open the browser and access this URL$") 
public void navigateToUrl() throws Throwable{ 

    System.setProperty("webdriver.chrome.driver", ""); 
    driver = new ChromeDriver(); 
    url = DataTable.getDataTableValue(0, 2, 2); 
    driver.get(url); 
    driver.manage().window().maximize(); 
} 

@When("^I enter the \"([^\"]*)\" and \"([^\"]*)\"$") 
public void enterCredentials(String userName, String password) throws Throwable { 

    userName = DataTable.getDataTableValue(0, 1, 1); 
    password = DataTable.getDataTableValue(0, 1, 2); 

    driver.findElement(By.id("username")).sendKeys(userName); 
    driver.findElement(By.id("password")).sendKeys(password); 
} 

@Then("^I am able to login$") 
public void clickLoginButton() throws Throwable { 
    driver.findElement(By.id("Login")).click(); 
} 

}

這裏是我的數據表(Excel文件)

| ID | UserName |密碼

| ID1 | username1 | password1

| ID2 | username2 |密碼2

| ID3 | username3 |密碼3

| ID4 | username4 | password4

+0

我也在努力學習硒在這些日子裏。你應該搜索「Apache POI」API。我認爲這將解決您的問題 – limonik

+0

感謝您的迴應。我已經使用Excel作爲數據表,但我不知道如何在Cucumber-Selenium上迭代。 基本上問題是不使用功能文件中的示例表,我想使用Excel,因爲我正在處理數千個數據。 – Sachi

+0

@Sachi我不認爲這是可能的。我不確定你想實現的是數據驅動的方法,還是隻想將數據抽象到不同的excel文件中。如果以後你可以用Apache POI來做到這一點,但首先是不可能的。 –

回答

0

如果你想在你需要實現代碼的步驟定義一個Excel工作表來迭代的內容。在Gherkin中沒有支持。

實施迭代時,Apache POI可能是一個選項。

明白,行爲驅動開發,BDD,目的是溝通是很重要的。小黃瓜是溝通的一種方式。幾乎所有理解問題的人都可以閱讀和理解小黃瓜情況。

如果你有一些在小黃瓜的真相和一些在Excel中,你會在一個情況下,你不要用黃瓜和小黃瓜通信而是作爲一個測試工具告終。這可能是好的。但是如果您使用Cucumber作爲測試工具,還有其他工具可能更易於使用。 JUnit就是其中之一。