方法一:使用TestNG进行数据驱动
package xxx
import java.util.List;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import util.AdminUtil;
import util.ElementInspector;
import util.Log;
/**
*
* @author xxx
* Objective: Import all card types which are need on client test.
*/
public class importCardTypeOnCF {
private WebDriver driver;
private StringBuffer verificationErrors = new StringBuffer();
@DataProvider(name = "data1")
public Object[][] createdata() {
return new Object[][] {
{ "Auto.piat.aic.sampleodata.action", "\"C:/automation/script/CF/upfileActionCF.exe\"" },
{ "Auto.piat.aic.sampleodata.createItem", "\"C:/automation/script/CF/upfileCreateItemCF.exe\"" },
};
}
@BeforeClass(alwaysRun = true)
@Parameters({"landscape","userName","passWord"})
public void setUp(String landscape, String userName, String passWord) throws Exception {
AdminUtil adminutil = new AdminUtil();
adminutil.loginOnCF(landscape, userName, passWord);
driver = adminutil.getDriver();
}
@Test(dataProvider = "data1")
public void importCardTypes(String CardTypeName, String uploadPath) throws Exception {
Thread.sleep(3000);
System.out.println("Start to import " + CardTypeName);
Log.getLogger().info("Start to import " + CardTypeName);
// Search by card type name, if exist, do nothing, else create the
// cardtype
WebElement searchInputbox = driver.findElement(By.xpath(ElementInspector.Search_Inputbox));
searchInputbox.clear();
Thread.sleep(3000);
searchInputbox.sendKeys(CardTypeName);
Thread.sleep(3000);
WebElement tbody = driver
.findElement(By.xpath(ElementInspector.Table_CardType));
List<WebElement> tr = tbody.findElements(By.tagName("tr"));
WebElement tr1 = tr.get(0);
if (tr1.findElement(By.className("xxxMListTblCell")).getText().equalsIgnoreCase(CardTypeName)) {
System.out.println(CardTypeName + " is already exist on the landscape.");
Log.getLogger().info(CardTypeName + " is already exist on the landscape.");
return;
} else {
// create new card type button
WebElement createCardType = driver
.findElement(By.xpath(ElementInspector.BUTTON_Create_CardType));
createCardType.click();
Thread.sleep(4000);
// input card type name
WebElement cardTypeName = driver
.findElement(By.xpath(ElementInspector.Name_CardType));
cardTypeName.sendKeys(CardTypeName);
Thread.sleep(4000);
WebElement fileUploadPath = driver.findElement(By.xpath(Elemen