如何使用testng将多个测试数据发送到以下

时间:2014-08-05 10:43:11

标签: java selenium selenium-webdriver testng selenium-grid

请建议我如何在3个浏览器中为此字段发送3个用户

driver.findElement(By.id(PropertiesConfiguration.getMyProperty("PinTextfield"))).sendKeys(PropertiesConfiguration.getMyProperty("user")); 

// main class

public  class TestHRWW {`main class`

protected WebDriver driver;
@Parameters("browser")`parameter for browser initialization`
@BeforeClass
public  void setup(String browser) {
    if (browser.equalsIgnoreCase("firefox")) {
        driver = new FirefoxDriver();
    } else if (browser.equalsIgnoreCase("iexplorer")) {
        System.setProperty("webdriver.ie.driver","Drivers\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
    } else if (browser.equalsIgnoreCase("chrome")) {
        System.setProperty("webdriver.chrome.driver","Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    else {
        throw new IllegalArgumentException("The Browser Type is Undefined");
    }
    driver.manage().window().maximize();
}

@BeforeMethod
public void open() {
    driver.get("http://www.myhrasiapacific.com/");
}
@AfterClass
public void teardown() {
    try {
        driver.quit();
    } catch (Exception e) {
        driver = null;
    }

}

// sub class
public class Login extends TestHRWW {`sub class`
    String x;
    static Logger logger = Logger.getLogger(Login.class.getName());
@Test   
    public void ClickLogon(WebDriver driver) throws InterruptedException, IOException {
        driver.findElement(`enter code here`
                By.id(PropertiesConfiguration.getMyProperty("logonbutton")))
                .click();
        Thread.sleep(2000);
       // here i need to sent 3 differt users credentials
        driver.findElement(
                By.id(PropertiesConfiguration.getMyProperty("PinTextfield")))
                .sendKeys(PropertiesConfiguration.getMyProperty("user"));
        Thread.sleep(2000);
        driver.findElement(
                By.id(PropertiesConfiguration
                        .getMyProperty("passwordTextfield"))).sendKeys(
                                PropertiesConfiguration.getMyProperty("password"));
        Thread.sleep(2000);
        driver.findElement(
                By.id(PropertiesConfiguration.getMyProperty("loginbutton")))
                .click();
        driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);

    }

}

2 个答案:

答案 0 :(得分:1)

您应使用TestNG @DataProvider传递用户名&密码

您的登录方式就像

@Test(dataProvider="dataForLogin")
public void ClickLogon(String userName,String password) {
}

@DataProvider(name="dataForLogin")
public static Object[][] prepareDataForLogin(){
        // preparation of login data
}

答案 1 :(得分:1)

如果您正在使用QAF,那么您不需要手动解析您的json数据并摆脱数据提供者类。

public class tDataHelper {
    private static List<String> studentsToCreate = new ArrayList<String>();

    static void parseData() throws Exception {
        studentsToCreate.add("user1");
        studentsToCreate.add("user2");
        studentsToCreate.add("user3");
    }

    @DataProvider
    public static Object[][] createStudents() {
        Object[][] objArray = new Object[studentsToCreate.size()][];
        for (int i = 0; i < studentsToCreate.size(); i++) {
            objArray[i] = new Object[1];
            objArray[i][0] = studentsToCreate.get(i);
        }
        return objArray;
    }
}

public class testStudents {
    private static tDataHelper helper = new tDataHelper();

    @BeforeClass
    public void setup() throws Exception {
        tDataHelper.parseData();
    }
    @Test(dataProvider = "createStudents", dataProviderClass = tDataHelper.class)
    public void testCreateStudents(String studentsToCreate) {
        System.out.println(studentsToCreate);
    }
}

QAF Test Data Driven