0
我想使用CSV文件進行數據驅動測試,並使用多組數據。我在下面的代碼中只使用了一行,並且沒有閱讀,請任何人提供或提供確切的代碼,如何閱讀多個使用硒的csv文件中的一組行。如何使用硒在CSV文件中讀取多行?
代碼:
public class CSVdataread {
WebDriver driver;
String baseurl = "https://hploadrunnertutorial.blogspot.in/";
String CSV_file = "E:\\AppiumTutorials\\Selenium_Practice\\SeleniumYoutube\\TestData.csv";
@BeforeClass
public void openBrowser(){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void verify_Search() throws InterruptedException{
driver.get(baseurl);
//Read CSV file
CSVReader reader = null;
try{
reader = new CSVReader(new FileReader(CSV_file));
String[] cell=reader.readNext();
while((cell= reader.readNext())!=null){
for(int i=1;i<cell.length;i++){
String keyword = cell[i];
String emails = cell[1+i];
String message=cell[2-i];
driver.findElement(By.id("ContactForm1_contact-form-name")).sendKeys(keyword);
System.out.println("System enters search keyword.");
Thread.sleep(3000);
driver.findElement(By.id("ContactForm1_contact-form-email")).clear();
driver.findElement(By.id("ContactForm1_contact-form-email")).sendKeys(emails);
System.out.println("System clears enter Search Keywords.");
driver.findElement(By.id("ContactForm1_contact-form-email-message")).clear();
driver.findElement(By.id("ContactForm1_contact-form-email-message")).sendKeys(message);
driver.findElement(By.id("ContactForm1_contact-form-submit")).sendKeys(message);
}
}
}catch(IOException e){
e.printStackTrace();
}
}
}
MY CSV文件:
SearchKeyword,Email,Message
Selenium Automation,[email protected],testing message
Selenium WebDriver,,[email protected],testingsecond message
如果您認爲它是正確的,您能接受答案嗎? – shank087