0
0
Selenium Javatesting~20 mins

DataProvider with external data in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DataProvider Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
AJohn-30\nJane-25\nBob-40
BJohn-30\nJane-25\nBob-40\nnull-0
CCompilation error due to missing throws declaration
DRuntime error: NumberFormatException
Attempts:
2 left
💡 Hint
Think about how the CSV lines are read and parsed into the DataProvider array.
assertion
intermediate
1: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
}
AAssert.assertTrue(username.length() > 0);
BAssert.assertFalse(username.isEmpty());
CAssert.assertNotNull(username);
DAssert.assertEquals(username, "");
Attempts:
2 left
💡 Hint
Check which assertion ensures the username string is not empty.
🔧 Debug
advanced
2: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;
}
AArrayIndexOutOfBoundsException at runtime
BNullPointerException at runtime
CCompilation error due to missing return type
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the for loop boundary condition carefully.
framework
advanced
1: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?
AIn a temporary folder outside the project
BIn the project root directory
CInside the src/main/java package folder
DInside src/test/resources folder
Attempts:
2 left
💡 Hint
Think about Maven/Gradle standard project structure and resource loading.
🧠 Conceptual
expert
1: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?
AIt reduces test execution time significantly
BIt guarantees zero test failures
CIt allows easy maintenance and scalability of test data without code changes
DIt automatically generates test reports
Attempts:
2 left
💡 Hint
Think about how test data changes affect test code.