Challenge - 5 Problems
DataProvider Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of DataProvider with CSV external data
Given the following TestNG test code using a DataProvider that reads from a CSV file, what will be the output when the test runs?
Selenium Java
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.List; public class CsvDataProviderTest { @DataProvider(name = "csvData") public Object[][] readCsv() throws Exception { List<Object[]> data = new ArrayList<>(); try (BufferedReader br = new BufferedReader(new FileReader("testdata.csv"))) { String line; while ((line = br.readLine()) != null) { String[] parts = line.split(","); data.add(new Object[]{parts[0], Integer.parseInt(parts[1])}); } } return data.toArray(new Object[0][]); } @Test(dataProvider = "csvData") public void testWithCsvData(String name, int age) { System.out.println(name + "-" + age); } }
Attempts:
2 left
💡 Hint
Think about how the CSV lines are read and parsed into the DataProvider array.
✗ Incorrect
The DataProvider reads each line from the CSV file, splits by comma, and converts the second value to int. The test prints each pair correctly. No extra null or errors occur if the CSV is well-formed.
❓ assertion
intermediate1:30remaining
Correct assertion for DataProvider test with Excel data
You have a TestNG test using a DataProvider that reads user credentials from an Excel file. Which assertion correctly verifies that the username is not empty in the test method?
Selenium Java
@Test(dataProvider = "excelData")
public void loginTest(String username, String password) {
// assertion here
}Attempts:
2 left
💡 Hint
Check which assertion ensures the username string is not empty.
✗ Incorrect
Assert.assertFalse(username.isEmpty()) correctly asserts that username is not empty. Option B only checks for null, but username could be empty string. Option B works but is less readable. Option B asserts username is empty, which is wrong.
🔧 Debug
advanced2:00remaining
Identify the error in DataProvider reading JSON file
This DataProvider reads test data from a JSON file. What error will occur when running this code?
Selenium Java
import org.testng.annotations.DataProvider; import java.nio.file.Files; import java.nio.file.Paths; import org.json.JSONArray; import org.json.JSONObject; @DataProvider(name = "jsonData") public Object[][] getData() throws Exception { String content = Files.readString(Paths.get("data.json")); JSONArray arr = new JSONArray(content); Object[][] data = new Object[arr.length()][2]; for (int i = 0; i < arr.length(); i++) { JSONObject obj = arr.getJSONObject(i); data[i][0] = obj.getString("username"); data[i][1] = obj.getString("password"); } return data; }
Attempts:
2 left
💡 Hint
Check the for loop boundary condition carefully.
✗ Incorrect
The for loop uses i <= arr.length(), but valid indices are 0 to arr.length() - 1. This causes ArrayIndexOutOfBoundsException when i equals arr.length().
❓ framework
advanced1:30remaining
Best practice for external DataProvider file location
In a Selenium TestNG project, where is the best place to store external test data files (like CSV, Excel) to ensure portability and easy access in DataProviders?
Attempts:
2 left
💡 Hint
Think about Maven/Gradle standard project structure and resource loading.
✗ Incorrect
The src/test/resources folder is the standard location for test resources. Files here are included in the test classpath and easily accessed by DataProviders. Other locations are not standard or portable.
🧠 Conceptual
expert1:30remaining
Why use external data sources with DataProvider?
Which is the main advantage of using external data sources (like CSV, Excel, JSON) with TestNG DataProviders instead of hardcoding test data inside the test class?
Attempts:
2 left
💡 Hint
Think about how test data changes affect test code.
✗ Incorrect
Using external data sources separates test data from code, making it easier to update or add test cases without modifying code. This improves maintainability and scalability. It does not directly affect execution time, failure rates, or reporting.