Issue
I want to test the e-commerce site. If you run this block of code, find the Search Box on the automation website, then type "novel" and click the button. But I want to do this using csvfiles. Csvfiles contain 1 row/column (novel). And after reading this word, he should write a novel in the search box and click the search button.
But I don't know how to read and integrate csvfiles. Could you help?
public void TestSecond(){
WebElement searchBar = driver.findElement(By.id("search-input"));
searchBar.click();
searchBar.sendKeys("novel");
driver.findElement(By.className("button-search")).click();
}
Solution
You can make use of Scanner class
to read the CSV file. You can create a method which reads the csv files and returns the proviced value which you can use within your test method
Your solution would look like
Method to read csv
public List<String> readCsv(String path)
{
List<String> novels = new ArrayList<String>();
try
{
Scanner scanner = new Scanner(new FileInputStream(path));
while(scanner.hasNext())
{
novels.add(scanner.next());
}
}
catch (FileNotFoundException e)
{
// If required you can print the complete error trace on console
e.printStackTrace();
//message indicating file was not found
System.out.println("File not found or is deleted");
}
return novels;
}
Test Method
public void TestSecond(){
List<String> novels = readCsv("path where your csv file is stored");
for(String novel: novels)
{
WebElement searchBar = driver.findElement(By.id("search-input"));
searchBar.click();
searchBar.sendKeys(novel);
driver.findElement(By.className("button-search")).click();
}
}
Answered By - Darshan Shah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.