- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 iframe
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援特性
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven & Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - JSON 資料檔案
Selenium WebDriver 可用於與 JSON 資料檔案互動。在自動化測試中,經常需要透過 JSON 檔案為測試用例提供大量資料,以驗證特定場景或建立資料驅動框架。JSON 檔案以鍵值對形式包含資料,其副檔名為 .json。
Java 提供了一些類和方法,可以使用 JSON Simple 庫對 JSON 檔案執行讀寫資料操作。JSON Simple 是一個輕量級的庫,用於操作 JSON 物件。
如何安裝 JSON Simple?
步驟 1 - 從以下連結新增 JSON Simple 依賴項:
https://mvnrepository.com/artifact/.
步驟 2 - 儲存包含所有依賴項的 pom.xml 檔案並更新 Maven 專案。
示例 1 - 從 JSON 檔案讀取所有值
讓我們以名為 Detail.json 的 JSON 檔案為例,我們將讀取整個 JSON 檔案並使用 JSONParser 類及其 parse() 方法檢索其所有值。
Detail.json 檔案的內容如下:
{
"name": "Ram",
"email": "abc@gmail.com",
"home": [
{
"road": "TUY",
"zip": "700008"
},
{
"road": "TUX",
"zip": "700061"
}
]
}
從 Detail.json 檔案檢索值後,我們將把 name 和 email 鍵的值輸入到以下頁面中的 全名 和 電子郵件 欄位中。
請注意:Details.json 檔案放置在專案下的 Resources 資料夾中,如下圖所示。
JSONRead.java 類檔案的程式碼實現。
package org.example;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class JSONRead {
public static void main(String[] args) throws IOException, ParseException {
//object of JSONParser
JSONParser j = new JSONParser();
// load json file to be read
FileReader f = new FileReader("./Resources/Detail.json");
// parse json content
Object o = j.parse(f);
// convert parsing object to JSON object
JSONObject detail = (JSONObject)o;
// get values from JSON file
String name = (String)detail.get("name");
String email = (String)detail.get("email");
System.out.println("First Name: " + name);
System.out.println("Email: " + email);
// get values from JSON array
JSONArray h = (JSONArray)detail.get("home");
// iterate through the JSONArray
for(int i = 0; i < h.size(); i ++){
JSONObject home = (JSONObject) h.get(i);
String road = (String)home.get("road");
String zip = (String)home.get("zip");
System.out.println("Road: " + road);
System.out.println("Zip: " + zip);
}
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://tutorialspoint.tw/selenium/practice/text-box.php");
//Identify elements
WebElement n = driver.findElement(By.xpath("//*[@id='fullname']"));
WebElement m = driver.findElement(By.xpath("//*[@id='email']"));
// enter name and email after reading from JSON file
n.sendKeys(name);
m.sendKeys(email);
// get values entered
String enteredName = n.getAttribute("value");
String enteredEmail = m.getAttribute("value");
System.out.println("Entered Name: " + enteredName);
System.out.println("Entered Email: " + enteredEmail);
// quitting browser
driver.quit();
}
}
新增到 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.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
輸出
First Name: Ram Email: abc@gmail.com Road: TUY Zip: 700008 Road: TUX Zip: 700061 Entered Name: Ram Entered Email: abc@gmail.com Process finished with exit code 0
在上面的示例中,我們讀取了整個 JSON 檔案,並在控制檯中獲得了其所有值,訊息為 - 姓名:Ram,電子郵件:abc@gmail.com,道路:TUY,郵編:700008,道路:TUX 和 郵編:700061。然後,將透過讀取 JSON 檔案獲得的姓名和電子郵件的值輸入到網頁上的輸入框中,並在控制檯中也檢索到輸入的值,訊息為 - 輸入的姓名:Ram 和 輸入的電子郵件:abc@gmail.com。
最後,收到訊息 Process finished with exit code 0,表示程式碼已成功執行。
示例 2 - 在 JSON 檔案中寫入和讀取值
讓我們再舉一個例子,我們將建立一個名為 Detail1.json 的 JSON 檔案,該檔案位於專案中的 Resources 資料夾下,並在其中寫入一些值。
package org.example;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class JSONReadWrite {
public static void main(String[] args) throws IOException, ParseException {
// object of JSONObject
JSONObject j = new JSONObject();
// generate key-value pairs in JSON file
j.put("Name", "Selenium");
j.put("Language", "Java");
// create JSON file with no duplicate values
FileWriter f = new FileWriter("./Resources/Detail1.json", false);
// write on json file
f.write(j.toJSONString());
// close file after write
f.close();
//object of JSONParser
JSONParser j1 = new JSONParser();
// load json file to be read
FileReader f1 = new FileReader("./Resources/Detail1.json");
// parse json content
Object o = j1.parse(f1);
// convert parsing object to JSON object
JSONObject detail = (JSONObject)o;
// get values from JSON file
String name = (String)detail.get("Name");
String language = (String)detail.get("Language");
System.out.println("Name: " + name);
System.out.println("Language: " + language);
}
}
新增到 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.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
輸出
Name: Selenium Language: Java Process finished with exit code 0
在上面的示例中,我們在專案中的 Resources 資料夾下建立了一個名為 Detail1.json 的 JSON 檔案,並在其中寫入了一些值。然後我們讀取所有這些值,最後在控制檯中以訊息的形式獲得它們 - 名稱:Selenium 和 語言:Java。
最後,收到訊息 Process finished with exit code 0,表示程式碼已成功執行。
此外,名為 Detail1.csv 的 JSON 檔案已在專案目錄中建立。單擊它,我們將獲得透過上述程式碼寫入的值。
這總結了我們關於 Selenium WebDriver - JSON 資料檔案的教程的全面內容。我們首先描述了什麼是 JSON Simple 庫,如何安裝 JSON Simple,並逐步講解了如何在使用 JSON Simple 和 Selenium WebDriver 的情況下讀取和寫入 JSON 檔案中的值的示例。這使您能夠深入瞭解 Selenium WebDriver 中的 JSON 資料檔案。最好不斷練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。
