我想使用包含多组数据的CSV文件执行数据驱动测试。我使用下面的代码,只读取一行而剩下它不读,请任何人建议或提供确切的代码,如何使用selenium从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();
}
}
}
我的CSV文件:
SearchKeyword,Email,Message
Selenium Automation,rjeshqa19@gmail.com,testing message
Selenium WebDriver,,rajeshqa19@gmail.com,testingsecond message
答案 0 :(得分:0)
试试这个。另外,我不确定你为什么要一个for循环。我假设你没有点击&#34;发送&#34;页面中的按钮。我从你的代码编辑了几行。另外,请确保从0开始索引。
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) {
int i = 0;
String keyword = cell[i];
String emails = cell[i + 1];
String message = cell[i + 2];
driver.findElement(By.cssSelector("div.sumome-welcomemat-action-close:nth-child(6)"));
driver.findElement(By.id("ContactForm1_contact-form-name")).clear();
driver.findElement(By.id("ContactForm1_contact-form-name")).sendKeys(keyword);
driver.findElement(By.id("ContactForm1_contact-form-email")).clear();
driver.findElement(By.id("ContactForm1_contact-form-email")).sendKeys(emails);
driver.findElement(By.id("ContactForm1_contact-form-email-message")).clear();
driver.findElement(By.id("ContactForm1_contact-form-email-message")).sendKeys(message);
}
} catch (IOException e) {
e.printStackTrace();
}