Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a DataProvider method that reads data from an external source.
Selenium Java
import org.testng.annotations.DataProvider; @DataProvider(name = "userData") public Object[][] [1]() { return new Object[][] { {"user1", "pass1"}, {"user2", "pass2"} }; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name different from the DataProvider name without specifying it in the annotation.
Returning a wrong data type instead of Object[][].
✗ Incorrect
The DataProvider method must have a name. Here, 'getData' is the method name matching the DataProvider annotation.
2fill in blank
mediumComplete the test method signature to use the DataProvider named "userData".
Selenium Java
import org.testng.annotations.Test; @Test(dataProvider = "[1]") public void loginTest(String username, String password) { // test steps }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong DataProvider name that does not exist.
Omitting the dataProvider attribute.
✗ Incorrect
The test method must reference the DataProvider by its name, which is 'userData' here.
3fill in blank
hardFix the error in the DataProvider method to correctly read data from an external CSV file.
Selenium Java
import org.testng.annotations.DataProvider; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.List; @DataProvider(name = "csvData") public Object[][] [1]() throws Exception { List<Object[]> data = new ArrayList<>(); BufferedReader br = new BufferedReader(new FileReader("data.csv")); String line; while ((line = br.readLine()) != null) { String[] parts = line.split(","); data.add(parts); } br.close(); return data.toArray(new Object[0][]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Method name not matching the DataProvider name.
Incorrect return type or array conversion.
✗ Incorrect
The method name must match the DataProvider name or be referenced by it. Here, 'getCsvData' is the method name that should be used.
4fill in blank
hardFill both blanks to correctly use the DataProvider method and handle exceptions in the test method.
Selenium Java
import org.testng.annotations.Test; @Test(dataProvider = "[1]") public void testLogin(String user, String pass) throws [2] { // test logic here }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong DataProvider name.
Not declaring the required exception in the test method.
✗ Incorrect
The test method uses the DataProvider named 'getCsvData' and must declare 'IOException' because the DataProvider method throws it.
5fill in blank
hardFill all three blanks to create a DataProvider that reads from an Excel file and returns data for the test.
Selenium Java
import org.testng.annotations.DataProvider; import java.io.FileInputStream; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.ss.usermodel.*; import java.util.ArrayList; import java.util.List; @DataProvider(name = "excelData") public Object[][] [1]() throws Exception { FileInputStream file = new FileInputStream("data.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt([2]); List<Object[]> data = new ArrayList<>(); for (Row row : sheet) { Cell cell1 = row.getCell(0); Cell cell2 = row.getCell(1); data.add(new Object[] {cell1.getStringCellValue(), cell2.getStringCellValue()}); } workbook.close(); file.close(); return data.toArray(new Object[[3]][]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong sheet index.
Incorrect array size in toArray method.
Method name not matching DataProvider name.
✗ Incorrect
The method name is 'getExcelData', the sheet index is 0 (first sheet), and the array size is data.size() to convert the list to array.