如何在TestNG中使用@DataProvider

时间:2015-01-07 07:54:50

标签: selenium-webdriver testng

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", "xx@gmail.com");
        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 Part中使用它。

任何人都可以帮忙吗???

如果我使用,这种方式工作正常..

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

但我想使用@DataProvider ..

2 个答案:

答案 0 :(得分:0)

一些如何管理..

@Test(dataProvider="getData")
    public static void register(ArrayList<HashMap> table) throws IOException {


}

这解决了我的问题。

答案 1 :(得分:-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.
}