Reading data from a CSV file

We saw a simple data-driven test TestNG. The test data was hardcoded in the test-script code. This could become difficult to maintain. It is recommended that we store the test data separately from the test scripts. Often, we use data from the production environment for testing. This data can be exported in the CSV format. We can read these CSV files in data-provider methods and pass the data to the test instead of hardcoded object arrays. 

In this example, we will use the OpenCSV library to read a CSV file. OpenCSV is a simple Java library for reading CSV files in Java. You can find more details on OpenCSV at http://opencsv.sourceforge.net/

Let's first create a CSV file, named data.csv, in the src/test/resources/data folder and copy the following combinations of searchWords and items:

searchWord,items
phones,3
music,5
iphone 5s,0

Next, we need to add the OpenCSV dependency to the Maven pom.xml file. For this example, we will use the latest version, 3.4, as shown in the following code snippet:

<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.4</version>
</dependency>

Finally, we need to modify the provider() method in the test class to read the contents of the CSV file and return them as an array of objects, as shown in the following code:

public class SearchTest {

WebDriver driver;

@DataProvider(name = "searchWords")
public Iterator<Object[]> provider() throws Exception {

CSVReader reader = new CSVReader(
new FileReader("./src/test/resources/data/data.csv")
, ',', ''', 1);

List<Object[]> myEntries = new ArrayList<Object[]>();
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {

myEntries.add(nextLine);
}

reader.close();
return myEntries.iterator();
}


@BeforeMethod
public void setup() {

System.setProperty("webdriver.chrome.driver",
"./src/test/resources/drivers/chromedriver");
driver = new ChromeDriver();
driver.get("http://demo-store.seleniumacademy.com/");

}

@Test(dataProvider = "searchWords")
public void searchProduct(String searchWord, String items) {

// find search box and enter search string
WebElement searchBox = driver.findElement(By.name("q"));

searchBox.sendKeys(searchWord);

WebElement searchButton =
driver.findElement(By.className("search-button"));

searchButton.click();

assertThat(driver.getTitle())
.isEqualTo("Search results for: '" + searchWord + "'");

List<WebElement> searchItems = driver
.findElements(By.xpath("//h2[@class='product-name']/a"));

assertThat(searchItems.size())
.isEqualTo(Integer.parseInt(items));
}

@AfterMethod
public void tearDown() {
driver.quit();
}
}

In the provide method, the CSV file will be parsed using the CSVReader class of the OpenCSV library. We need to provide the path of the CSV file, the delimiter character, and the header row number (this will skip while fetching the data), as shown in the following code snippet:

@DataProvider(name = "searchWords")
public Iterator<Object[]> provider() throws Exception {

CSVReader reader = new CSVReader(
new FileReader("./src/test/resources/data/data.csv")
, ',', ''', 1);

List<Object[]> myEntries = new ArrayList<Object[]>();
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
myEntries.add(nextLine);
}
reader.close();
return myEntries.iterator();
}

In the preceding code, we will read each line of the CSV file ,copy it to an array of the object, and return it to the test method. The test method will be executed for each row in the CSV file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.63.136