0
0
Selenium Javatesting~10 mins

DataProvider with external data in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AgetData
BdataProvider
CfetchData
DloadData
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[][].
2fill in blank
medium

Complete 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'
AloginData
BuserData
CtestData
DdataSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong DataProvider name that does not exist.
Omitting the dataProvider attribute.
3fill in blank
hard

Fix 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'
AloadCsv
BcsvData
CreadCsv
DgetCsvData
Attempts:
3 left
💡 Hint
Common Mistakes
Method name not matching the DataProvider name.
Incorrect return type or array conversion.
4fill in blank
hard

Fill 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'
AgetCsvData
BIOException
CSQLException
DInterruptedException
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong DataProvider name.
Not declaring the required exception in the test method.
5fill in blank
hard

Fill 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'
AgetExcelData
B0
Cdata.size()
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong sheet index.
Incorrect array size in toArray method.
Method name not matching DataProvider name.