3 回答

TA貢獻1806條經驗 獲得超5個贊
JUnit 4 解決方案。這個會很大。。。
首先,讓我們從CSVReader一些好的實踐和代碼可讀性開始。在您的測試中,您讀取 CSV 數據并在測試中使用它們。讀取數據不是測試的責任。測試應該已經提供給它的所有數據。它被稱為DataProvider。這個術語實際上是在TestNG測試框架中使用的,就像@user861594 建議的那樣。
因此,您應該有一些東西可以為您的測試提供數據。但這已經是第 2 步了。由于您知道您將從 CSV 文件中逐行讀取數據,因此您應該創建一個適當的類來從 CSV 中讀取數據。
這是一個例子:
public class CSVReader {
private static final String DEFAULT_SEPARATOR = ",";
private BufferedReader reader;
private List<String> lines;
public CSVReader(File file) throws FileNotFoundException {
this.reader = new BufferedReader(new FileReader(file));
lines = this.reader.lines().collect(Collectors.toList());
}
public String[] getRow(int rowNumber) {
return lines.get(rowNumber).split(DEFAULT_SEPARATOR);
}
public int getRowCount() {
return lines.size();
}
}
構造CSVReader函數接受 aFile作為參數并創建適當的對象以以特定方式讀取數據(例如: read as String)。然后,讀取 CSV 文件中的數據,就像在普通 TXT 文件中一樣,將行保存在內存中以備后用。
然后我們創建2個方法。首先是getRowCount它給了我們行/數據集的總數。
其次是getRow從列表中收集特定行并將其保存到String[]數組中以備后用。
String[]數組有一個類似 1 Excel 行的演示文稿:
data index 0 | data index 1 | data index 2 | data index 3
我們有一個類可以讓我們輕松讀取文件。讓我們創建DataProvider
為了向測試提供數據,我們需要使用@Parameters注解并返回Collection<Object[]>到我們的測試。我稍后會談到這一點。
所以,讓我們在我們的DataProvider
public class CSVDataProvider {
public Collection<Object[]> getData() throws FileNotFoundException {
CSVReader reader = new CSVReader(new File("C:\\Users\\xxxxxxxxxxx\\Desktop\\TestData.csv"));
int rowCount = reader.getRowCount();
Object[][] data = new Object[rowCount][2];
for (int i = 0; i < rowCount; i++) {
Object[] singleRow = reader.getRow(i);
data[i][0] = singleRow[0];
data[i][1] = singleRow[1];
}
return Arrays.asList(data);
}
}
我假設您在 CSV 文件中只有登錄名和密碼。這就是我創建二維數組的原因new Object[rowCount][2]。rowCount我們通過提供它必須存儲的元素數量來創建數組,并且我們知道變量中有多少行。2 表示我們每行只有 2 個數據。登錄名和密碼。如果你想使用額外的元素,例如 - 用戶的角色,你可以修改為[3]
在for循環中,我們將數據從 CSV 文件轉換為數組并將其返回以供以后使用。
現在,讓我們談談我們的測試類。
@RunWith(Parameterized.class)
public class OurTest {
private String login, password;
public OurTest(String login, String password) {
this.login = login;
this.password = password;
}
@Parameterized.Parameters(name = "{index}: Login: ({0}) Password: ({1})")
public static Collection<Object[]> data() throws FileNotFoundException {
return new CSVDataProvider().getData();
}
@Test
public void test() {
System.out.println(String.format("login : %s | Password: %s", login, password));
}
}
為了將參數傳遞DataProvider給我們的測試,我們需要 1. 用注釋類@RunWith(Parameterized.class) 2. 創建一個返回 @Parameters 的方法Collection<Object[]> with annotation3. 創建一個構造函數來反映我們接受什么樣的數據。
關于第 3 點,這就是為什么我用String loginand創建了一個 2 參數構造函數String password。我們正在傳遞這兩個參數。OurTestJUnit 將為每個測試創建一個新實例并傳遞不同的行。
在test我剛剛打印的方法中,我們從DataProvider
我沒有提供一個完全有效的解決方案,因為我希望你嘗試調整你的測試來學習這種特定的方法。它也被稱為Data-driven Testing。
我們只有一種測試方法,但 CSV 文件中的每一行都將作為單獨的測試運行。
希望能幫助到你!

TA貢獻1883條經驗 獲得超3個贊
看起來你想用一組測試數據迭代你的測試。在這種情況下,您應該使用 TestNG數據提供程序功能。
public class CSVdataread {
private WebDriver driver;
String baseUrl = "URL";
String CSV_file = "C:\\Users\\xxxxxxxxxxx\\Desktop\\TestData.csv";
@BeforeMethod
public void openBrowser() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\xxxxxxxxxxxx\\Desktop\\webdriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider="users-data")
public void verify_Search(String name, String email) throws InterruptedException, IOException {
String baseUrl = "http://xxxxx.xxx/xxxx/";
driver.navigate().to(baseUrl);
driver.findElement(By.xpath("//input[@id='useridFormField-inputEl']")).sendKeys(name);
driver.findElement(By.xpath("//input[@id='userpasswordFormField-inputEl']")).sendKeys(email);
}
//This method will provide data to any test method that declares that its Data Provider
@DataProvider(name = "users-data")
public Iterator<Object[]> createDataFromCSV() {
CSVReader reader = new CSVReader(new FileReader(CSV_file));
List<Object[]> data = new ArrayList<Object[]>();
//read csv data to list
return data.iterator();
}
@AfterMethod
public void closeBrowser() {
driver.quit();
}
}
您還可以利用可用的 data-provider-extension。例如,使用 qaf 您無需為驅動程序管理或數據提供者編寫代碼。您的測試類將如下所示:
public class CSVdataread extends WebDriverTestCase{
@QAFDataProvider(dataFile="resources/user-data.csv")
@Test()
public void verify_Search(String name, String email) throws InterruptedException, IOException {
String baseUrl = "http://xxxxx.xxx/xxxx/";
getDriver().navigate().to(baseUrl);
getDriver().findElement(By.xpath("//input[@id='useridFormField-inputEl']")).sendKeys(name);
//another way of finding element...
getDriver().findElement("xpath=//input[@id='userpasswordFormField-inputEl']").sendKeys(email);
}
}

TA貢獻1836條經驗 獲得超4個贊
你的 while 循環看起來壞了。while 循環中的 for 循環似乎弄亂了您的登錄過程。
while((cell = reader.readNext())!=null) { // iterate through csv file
String name = cell[0]; // cell is current row, you need first column for name
String email = cell[1]; // second column for email (as password?)
// what do you want to do with baseUrl here?
driver.findElement(By.xpath("//input[@id='useridFormField-inputEl']")).sendKeys(name);
driver.findElement(By.xpath("//input[@id='userpasswordFormField-inputEl']")).sendKeys(email);
// you need to check the successful login here
// then logout and open main page
// do not quit before you are finished
}
// quit after the loop is finished
driver.quit();
如果不了解網站,就不可能告訴您如何檢查成功登錄和執行注銷。
我可以建議您花一些精力來學習不太復雜的任務嗎?您似乎在使用基本 Java 元素時遇到了很多麻煩。從未停止學習。
添加回答
舉報