2015-01-07 42 views
0
package com.xchanging.selenium.testcases.testng; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap; 

import org.testng.annotations.DataProvider; 
import org.testng.annotations.Test; 

import com.xchanging.selenium.utility.CaptureScreenShot; 
import com.xchanging.selenium.utility.ClearText; 
import com.xchanging.selenium.utility.ClickEvent; 
import com.xchanging.selenium.utility.GlobalVariables; 
import com.xchanging.selenium.utility.ReadRows; 
import com.xchanging.selenium.utility.SelectCheckBox; 
import com.xchanging.selenium.utility.SelectDropDown; 
import com.xchanging.selenium.utility.Text; 
import com.xchanging.selenium.utility.VerifyText; 

public class RegisterAccount extends GlobalVariables { 

    @Test(dataProvider = "getData") 
    public static void register() throws IOException { 
     ClickEvent.clickAt("createAccount_xpath"); 
     Text.enterText("username_name", "username"); 
     Text.enterText("password_name", "machans"); 
     Text.enterText("confirmPassword_name", "machans"); 
     ClickEvent.clickAt("securityquestion_name"); 
     SelectDropDown.select("securityquestion_name", "petname"); 
     Text.enterText("securityanswer_xpath", "vsbhss"); 
     Text.enterText("fullName_name", "Chandrasekaran"); 
     Text.enterText("email_name", "[email protected]"); 
     ClearText.clear("dob_name"); 
     Text.enterText("dob_name", "11/11/1982"); 
     SelectDropDown.select("gender_name", 1); 
     SelectDropDown.select("marital_name", 1); 
     SelectDropDown.select("country_name", "India"); 
     SelectCheckBox.selectchkbox("checkbox_xpath"); 
     ClickEvent.clickAt("register_xpath"); 
     VerifyText.verify("Congratulations.. You have registered successfully"); 
     VerifyText.verify("Login now"); 
     CaptureScreenShot.screenshot("Registration_Successful"); 
     ClickEvent.clickAt("closebutton_xpath"); 
    } 

    @DataProvider 
    public ArrayList<HashMap> getData() throws IOException { 
     ArrayList<HashMap> table = ReadRows.readExcel("Sheet1"); 
     return table; 
    } 
} 

現在我想使用此DataProvider並從xls獲取值,並且必須在我的@Test部分中使用它。如何在TestNG中使用@DataProvider

任何人都可以幫忙嗎???

如果我使用,這樣一來它工作正常..

ArrayList<HashMap> table = ReadRows.readExcel("Sheet1"); 
table.get(0).get("email") 

但我想用@dataProvider ..

回答

-1

如果你想使用dataProvider註解。帶註釋的方法必須返回Object[][],其中每個Object []可以被分配測試方法的參數列表。

你可以嘗試這樣的事情:

@DataProvider 
public Object[][] getData() throws IOException { 
    Object[][] data = new Object[3][2] // based on the size of your excel rows & cols. 
    // Write the code to read data from excel and store. 
    data[][] = //your Code. 
    return data; 
} 

而且你的測試方法可以使用這些數據。

//Lets say your Object[][] data returns 2 arguments. 
@Test(dataProvider="getData") 
public void testMethod(firstArgument, secondArgument){ 
    // your code to use the arguments supplied by data. 
} 
+0

我有這個方法ReadRows.readExcel(「Sheet1」);以檢索Excel表格..我可以採取一些邏輯從這個值? – ChanGan

+0

請參閱下面的URL。 http://poi.apache.org/spreadsheet/quick-guide.html – Paras

+0

我已經使用這個邏輯,我能夠使用ReadRows.readExcel(「Sheet1」)檢索數據。唯一的事情是如何定製在DataProvider中使用它 – ChanGan

0

一些如何管理..

這解決了我的問題。