简体   繁体   English

在 Selenium 中使用 TestNG DataProviders 读取包含“用户名”和“密码”的 JSON 文件时出现错误

[英]Getting an error as while reading a JSON file containing 'Usernames' and 'Passwords' using TestNG DataProviders in Selenium

Getting an error as while reading a JSON file using TestNG DataProviders in Selenium.在 Selenium 中使用 TestNG DataProviders 读取 JSON 文件时出现错误。

Error:
class com.google.gson.JsonObject cannot be cast to class org.json.simple.JSONObject (com.google.gson.JsonObject and org.json.simple.JSONObject are in unnamed module of loader 'app')
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.google.gson.JsonParser;

import io.github.bonigarcia.wdm.WebDriverManager;

public class DataDrivenTest_json {
    WebDriver driver;

    @BeforeClass
    void setUp() { /* to set up chromedriver */
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.MILLISECONDS);
    }

    @Test(dataProvider="dp") /* using dataproviders*/
    void login(String data) {
        String list[] = data.split(",");
  driver.get("some website url");
  driver.findElement(By.cssSelector("[id='Email']")).sendKeys(list[0]);//username
  driver.findElement(By.cssSelector("[id='Password']")).sendKeys(list[1]);//password
  driver.findElement(By.cssSelector("[type='submit']")).click();
    }
    
    @DataProvider(name="dp")
    public String[] readJson() throws IOException{
        @SuppressWarnings("deprecation")
        JsonParser jsonparser = new JsonParser();
        FileReader reader =new FileReader("C:\\Users\\dell\\eclipse-workspace- 
           photon\\ExcelDriven\\src\\test\\java\\Testdata.json");
        @SuppressWarnings("deprecation")
        Object obj = jsonparser.parse(reader); //java object
        JSONObject userLoginsJsonObj = (JSONObject)obj;
        JSONArray userLoginsArray =(JSONArray)userLoginsJsonObj.get("userLogins"); 
        String array[] = new String[userLoginsArray.size()]; 
        for(int i=0; i<userLoginsArray.size();i++) {
            JSONObject users = (JSONObject)userLoginsArray.get(i); 
            String username = (String)users.get("username"); 
           
            String password = (String)users.get("password");
            array[i] = username+","+password;
        }
        return array;
    }
    

    @AfterClass
    void tearDown() { //close driver
        driver.close();
    }
}
/*My MavenProject has testng included and pom.xml file has 'com.googlecode.json-simple' dependency added.Still the above error is visible in console.*/

You import class org.json.simple.JSONObject while you need to import com.google.gson.JsonObject .您导入class org.json.simple.JSONObject而您需要导入com.google.gson.JsonObject You cannot just cast an object to any class even if that class has the same name.即使该类具有相同的名称,您也不能只是将对象强制转换为任何类。 The package of the class also matters.课程的包也很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM