JAVA 中的 DDT Json 与 Selenium

问题描述 投票:0回答:1

我正在尝试使用 Json 来参数化程序。

数据以简单文本格式存储在data.JSON中

我已经添加了所有依赖项。

当我尝试运行时,下面的代码给了我失败。请让我知道为什么这段代码可能是错误的。

JSONDataClass


public class JsonDataClass {

    @Test
         public List<HashMap<String, String>> getJsonDataToMap() throws IOException
        {
        String FileDir = System.getProperty("user.dir")+"//src//DDT//data.json";
        System.out.println(FileDir);
        
        String jsonContent = FileUtils.readFileToString(new File(System.getProperty("user.dir")+"//src//DDT//data.json"),StandardCharsets.UTF_8);
        System.out.println(jsonContent);
        //List<HashMap<String, String>> data = mapper.readValue(jsonContent, new TypeReference<List<HashMap<String, String>>>());
         List<HashMap<String, String>> dataHashMap = new Gson()
                    .fromJson(jsonContent, 
                        new TypeToken<List<HashMap<String, 
                            String>>>() {}.getType());
                return dataHashMap; 
        }

        
   
    
    @DataProvider
    public Object[][] getData() throws IOException{
        List<HashMap<String, String>> pdata =getJsonDataToMap();
    
        return new Object[][]{{pdata.get(0)},{pdata.get(1)}};
    }
    
    
    
    
    
    @Test(dataProvider="getData")

    public void fetchingdata(HashMap<String, String> input) 
    {
        System.out.println(input.get("email"));
        System.out.println(input.get("username"));
        System.out.println(input.get("password"));
    }
    
}

数据.JSON


[
    {
        "email":"[email protected]",
        "username":"[email protected]",
        "password":"pass123"
    }

    {
        "email":"[email protected]",
        "username":"[email protected]",
        "password":"pass2222"
    }
]

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
        <class name="DDT.JsonDataClass"></class>
    </classes>  
  </test> <!-- Test -->
</suite> <!-- Suite -->
selenium-webdriver testng
1个回答
0
投票

JsonDataDrivenTest.java

package DDT;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JsonDataDrivenTest {


     
    @Test(dataProvider = "dataProvider")
        public void loginTest(String username, String password) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Satyam\\OneDrive\\Desktop\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("link");

        WebElement usernameField = driver.findElement(By.xpath("//input[@id='form-username']"));
        WebElement passwordField = driver.findElement(By.xpath("//input[@id='form-password']"));
        WebElement loginButton = driver.findElement(By.xpath("//button[@id='load1']"));

        usernameField.sendKeys(username);
        passwordField.sendKeys(password);
        loginButton.click();

        // Add your assertion logic here

        driver.quit();
    }

    @DataProvider(name = "dataProvider")
    public Iterator<Object[]> testData() throws Exception {
        List<Object[]> data = new ArrayList<>();

        JsonParser jsonParser = new JsonParser();
        JsonArray jsonArray = jsonParser.parse(new FileReader("C:\\Users\\satyam\\eclipse-workspace\\stupid\\src\\stupid\\testdata.json")).getAsJsonArray();

        for (JsonElement element : jsonArray) {
            JsonObject jsonObject = element.getAsJsonObject();
            String username = jsonObject.get("username").getAsString();
            String password = jsonObject.get("password").getAsString();
            data.add(new Object[]{username, password});
        }

        return data.iterator();
    }
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
      <classes><class name="JsonDataDrivenTest"></class></classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

测试数据.json

[
    {
        "username": "abc",
        "password": "pass"
    },
    {
        "username": "abc",
        "password": "pass"
    }
]
© www.soinside.com 2019 - 2024. All rights reserved.