Top 5 ways to setup Test Data in your Automation Frameworks for SDETs 1) Hard coded data (if data instance is very small) 2) Setup of Excel or CSV files for storing data (Obsolete) 3) Setup of JSON files (Modern) 4) Mocking data using API (Modern) 5 Database integration (Obsolete) Now let's dive into basic setup for each type: 1) Hard Coded: Entering test data directly into our Test Scripts or deriving it through some other function. But its coded directly within our files. Ex: driver.findElement(By.id("username")).sendKeys("testUser"); 2) Using Excel or CSV files: Read test data from CSV or Excel files using libraries like Apache POI or OpenCSV Ex: List<Object[]> data = new ArrayList<>(); String csvFile = "path/to/data.csv"; try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) { String line; while ((line = br.readLine()) != null) { data.add(line.split(",")); } } catch (IOException e) { e.printStackTrace(); } return data.toArray(new Object[0][]); } 3) JSON files: Reading test data using JSON files, use libraries like Jackson, GSON etc. Ex: (Used Jackson) ObjectMapper objectMapper = new ObjectMapper(); try { User user = objectMapper.readValue(new File("path/to/user.json"), User.class); System.out.println(user.getUsername()); System.out.println(user.getPassword()); } catch (IOException e) { e.printStackTrace(); } } 4) Using APIs: Retrieving test data via APIs allows for dynamic and up-to-date test scenarios, particularly useful when dealing with databases Now in this we can use OKHTTP or Rest Assured as per your requirements. As this is little complex to implement as we need to data driver class for APIs & then use it in our Test Classes. I am providing example here: https://shorturl.at/HIGP2 5) Fetching data directly using Database: Useful when data connection is available locally or is not env. dependent. This is usually used in legacy frameworks now, as most of us have migrated to use APIs now. Ex: List<Object[]> data = new ArrayList<>(); String url = "jdbc:mysql://localhost:3306/testdb"; String user = "testuser"; String password = "testpassword"; try (Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT username, password FROM users")) { while (rs.next()) { data.add(new Object[]{rs.getString("username"), rs.getString("password")}); } } catch (SQLException e) { e.printStackTrace(); } return data.toArray(new Object[0][]); } ⭐ Found this useful? Repost for your network. ♻️ -x-x- ⭐ P.S. Which one is the most useful for you? ++++ Get Complete Code here: https://shorturl.at/HIGP2 To learn more about Java for Cracking Test Automation Interviews use: https://lnkd.in/g5hr9bea Become Test Automation Architect with FullStackQALearn Docker, GitHub Actions, Jenkins, Test Automation etc. : https://lnkd.in/dWjpT3-3 #japneetsachdeva
Thanks for breaking down the different ways to set up test data. Very informative. 🚀
Your post is much appreciated!
Thanks for sharing
SDET Engineer 3 @Comcast| Ex- Accenture| BDD | Selenium | Java | TestNG | Maven | POM | Rest API Testing | Postman | RestAssured | Appium | JMeter | Github CI | JIRA | SDET | Healthcare
5moHi Japneet Sachdeva one doubt regarding json file for fetching the data. If I have 10 user name I need to add 10 json data right? In the single json file