• Selenium Video Tutorials

Selenium WebDriver - CSV 資料檔案



Selenium Webdriver 可用於與 csv 資料檔案互動。在自動化測試中,通常會透過 csv 檔案傳遞大量資料以支援資料驅動框架。csv 檔案看起來類似於 Excel 檔案,並且副檔名為 .csv。

Java 提供了一些類和方法,可以使用 OpenCSV 庫對 csv 檔案執行讀寫資料操作。OpenCSV 擁有兩個最重要的類 - CSVReader 和 CSVWriter,用於對 csv 檔案執行讀寫操作。

如何安裝 OpenCSV?

步驟 1 - 從連結 OpenCSV 將 OpenCSV 依賴項新增到 pom.xml 檔案中。

步驟 2 - 儲存包含所有依賴項的 pom.xml 並更新 Maven 專案。

讀取 CSV 中的所有值

讓我們以下面名為 Details1.csv 的 csv 檔案為例,我們將讀取整個 csv 檔案並使用 CSVReader 類及其方法 readNext() 檢索其所有值。

Selenium CSV File 1

請注意 - Details1.csv 檔案放置在專案下的 Resources 資料夾中,如下面的影像所示。

Selenium CSV File 2

示例

package org.example;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import java.io.FileReader;
import java.io.IOException;

public class CSVRead {
   public static void main(String[] args) throws InterruptedException,
   IOException, CsvValidationException {

      // object of CSVReader class
      CSVReader r = new CSVReader(new FileReader("./Resources/Details1.CSV"));

      // store csv data in string array
      String [] csvValues;

      // iterate through csv till the end of the values
      while ((csvValues = r.readNext())!= null){
      
         // iterate through rows
         for (String csvValue : csvValues){
            System.out.println(csvValue);
         }
      }
   }
}

新增到 pom.xml 的依賴項。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>
   
   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
      <dependency>
         <groupId>com.opencsv</groupId>
         <artifactId>opencsv</artifactId>
         <version>5.9</version>
      </dependency>
   </dependencies>
</project>

輸出

Name
Street
Ram
Street 12
Rohan
Street 110

Process finished with exit code 0

在上面的示例中,我們讀取了整個 csv 檔案並在控制檯中獲取了其所有值。

最後,收到訊息 Process finished with exit code 0,表示程式碼已成功執行。

在 CSV 中寫入和讀取值

讓我們再舉一個例子,我們將建立一個名為 Details2.csv 的 csv 檔案,該檔案位於專案中的 Resources 資料夾下,並使用 CSVWriter 類及其方法:writeNext() 或 writeAll() 以及 flush() 寫入一些值。最後,使用 CSVReader 類及其方法 readNext() 讀取這些值。

示例

package org.example;

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import com.opencsv.exceptions.CsvValidationException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVReadWrite {
   public static void main(String[] args) throws InterruptedException,
   IOException, CsvValidationException {

      // object of CSVWriter class
      CSVWriter w = new CSVWriter(new FileWriter("./Resources/Details2.CSV"));

      // stores values in csv
      String [] rows1 = {"Name", "Street"};
      String [] rows2 = {"Ram", "Street 12"};
      String [] rows3 = {"Rohan", "Street 110"};

      // add values to be written to list
      List<String[]> write = new ArrayList<>();
      write.add(rows1);
      write.add(rows2);
      write.add(rows3);

      // write and flush all values
      w.writeAll(write);
      w.flush();

      CSVReader r = new CSVReader(new FileReader("./Resources/Details2.CSV"));

      // store csv data in string array
      String [] csvValues;

      // iterate through csv till the end of the values
      while ((csvValues = r.readNext())!= null){
         // iterate through rows
         for (String csvValue : csvValues){
            System.out.println(csvValue);
         }
      }
   }
}

新增到 pom.xml 的依賴項。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>
   
   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
      <dependency>
         <groupId>com.opencsv</groupId>
         <artifactId>opencsv</artifactId>
         <version>5.9</version>
      </dependency>
   </dependencies>
</project>

輸出

Name
Street
Ram
Street 12
Rohan
Street 110

Process finished with exit code 0

在上面的示例中,我們在專案內的 Resources 資料夾下建立了一個名為 Details2.csv 的 csv 檔案,並在其中寫入了一些值。然後我們讀取所有這些值,最後在控制檯中獲取它們。

最後,收到訊息 Process finished with exit code 0,表示程式碼已成功執行。

此外,名為 Details2.csv 的 csv 檔案已在專案目錄中建立。單擊它,我們將獲得透過上述程式碼寫入的值。

Selenium CSV File 3

結論

這總結了我們關於 Selenium Webdriver CSV 資料檔案的教程的全面內容。我們首先描述了什麼是 OpenCSV 庫,如何安裝 OpenCSV,並逐步演示瞭如何在 csv 中讀取和寫入值,藉助 OpenCSV 以及 Selenium Webdriver。這使您能夠深入瞭解 Selenium Webdriver 中的 CSV 資料檔案。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並擴充套件您的視野。

廣告