2017-03-10 76 views
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 
+0

如果您認爲它是正確的,您能接受答案嗎? – shank087

回答

0

試試這個。另外,我不確定爲什麼你想要一個for循環。我假設你沒有點擊頁面上的「發送」按鈕。我編輯了幾行代碼。此外,請確保您從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(); 
     }