Examples:
Working with Flat Files:
The format for Excel Data is shown below :
Working with Flat Files:
The format for Excel Data is shown below :
The Following Example Demonstrates how to access data from excel
Selenium Code along with TestNG Framework:
For Reading Flat Files : Excel and CSV
package sample;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import atu.ddf.exceptions.DataDrivenFrameworkException;
import atu.ddf.file.CSVFile;
import atu.ddf.file.ExcelFile;
import atu.ddf.selenium.SeleniumTestNGHelper;
/**
*
* @author Automation Tester http://automationtestingutilities.blogspot.in/
*/
public class ReadingFromFaltFile {
WebDriver driver;
@BeforeClass
public void init() {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
// login into WordPress web site and publish a new Post
// Another Example of Data Driven while working with Excel files having .xls extension
@Test(dataProvider = "iteratorTestData")
public void addPost(String username, String password, String title,
String content) throws InterruptedException {
// Open App
driver.get("http://demo.opensourcecms.com/wordpress/wp-login.php");
// Enter UserName
Thread.sleep(3000);
driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys(username);
// Enter Password
driver.findElement(By.id("user_pass")).clear();
driver.findElement(By.id("user_pass")).sendKeys(password);
// Click on Submit button
driver.findElement(By.id("wp-submit")).submit();
// Enter Title
driver.findElement(By.id("title")).sendKeys(title);
// Enter Content
driver.findElement(By.id("content")).sendKeys(content);
// Click on Publish Button
driver.findElement(By.id("publish")).click();
}
//Another Example of Data Driven Using CSV File
@Test(dataProvider = "csvData")
public void csvDataExample(String username, String password, String title)
throws InterruptedException {
System.out.println("Username: " + username + ": password: " + password
+ ": title: " + title);
}
// Another Example of Data Driven while working with Excel files having .xlsx extension
@Test(dataProvider = "objectTestData")
public void registerUser(String username, String gender, String location,
String language, String age, String email) {
System.out.println("Username: " + username + ": Gender: " + gender
+ ": Location: " + location + ": Language: " + language
+ ": Age: " + age + ": Email: " + email);
}
//Example of 2-Dimensional Object Array DataProvider
@DataProvider(name = "objectTestData")
public Object[][] data() throws DataDrivenFrameworkException {
String excelResource = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "resources"
+ System.getProperty("file.separator") +"ExcelTestData.xlsx";
// provide your excel file path here
ExcelFile excelFile = new ExcelFile(excelResource);
// provide the Sheet name
excelFile.setSheetName("MyScenario");
// provide the Column Name where Test Case names will be given
excelFile.setTestCaseHeaderName("TestCaseNameColumn");
// provide the Test Case Name
List<List<String>> data = excelFile
.getDataUsingTestCaseName("RegisterUser");
// SeleniumTestNGHelper is a utility class that will help in converting
// the Test data into TestNG Data Provider supported format
// One such format here is a 2-Dimensional Object Array
return SeleniumTestNGHelper.toObjectArray(data);
}
//Example of DataProvider whose return type is Object Array Iterator
@DataProvider(name = "iteratorTestData")
public Iterator<Object[]> data1() throws DataDrivenFrameworkException {
String excelResource = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "resources"
+ System.getProperty("file.separator") +"ExcelTestData.xls";
ExcelFile excelFile = new ExcelFile(excelResource);
excelFile.setSheetName("MyScenario");
excelFile.setTestCaseHeaderName("TestCaseNameColumn");
List<List<String>> data = excelFile.getDataUsingTestCaseName("AddPost");
return SeleniumTestNGHelper.toObjectArrayIterator(data);
}
@DataProvider(name = "csvData")
public Object[][] csvData() throws DataDrivenFrameworkException {
String csvResource = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "resources"
+ System.getProperty("file.separator") +"CSVTestData.csv";
CSVFile csvFile = new CSVFile();
List<List<String>> data = csvFile.getTestData(csvResource);
return SeleniumTestNGHelper.toObjectArray(data);
}
}
Go To: DataDrivenFramework Part 3 - Contains Details regarding Upcoming Releases and Features
can you please provide the SeleniumTestNGHelper class
ReplyDeleteHi,
DeleteThe download links are provided in the main page of Data Driven Framework.
Regards
AT
I needed to thank you for this fantastic read!! I
ReplyDeletedefinitely enjoyed every little bit of it. I have got you book marked to check out
new stuff you post…
Also visit my website - how to open csv file in excel, ,
Hi,
ReplyDeleteI used your code for CSV DDF testing, but failed with error:
java.lang.RuntimeException: java.lang.NoSuchMethodError: org.apache.commons.csv.CSVFormat.parse(Ljava/io/Reader;)Ljava/lang/Iterable;
I've downloaded commons-csv-1.0-20140721.202737-298.jar and added it in Build path. Not clear if this is right one. Could you please advise? Thanks.
Freddy
Hi,
DeleteThe initial version was commons-csv-1.0-SNAPSHOT , which is a snapshot ! I suppose the method is no more available in the new release. we will fix the issue. Thank you :)
#AT
this is "The Best" data driven tutorial which i have seen so far:)
DeleteCongratz.. Can we have same kind for keyword driven as well :)
Hi,
ReplyDeleteSeleniumTestNGHelper class is missing from ATU_DDF_Example. Can you please provide a download link for it?
Thanks
Ronen
Hello Admin, thank you for the informative post. At Fita academy, we provide the best IT trainings for various courses. You can check our Fita Chennai review page to know more about our quality and standards.
ReplyDeleteHI I have just tried to use the above code to execute reading data from xl file like (.xlsx) but it is showing an error as
ReplyDelete*DataDrivenFrameworkException cannot be resolved to a type* and
*CSVFile cannot be resolved to a type* *SeleniumTestNGHelper cannot be resolved*. I can not able to resolve it. could you please help me out to resolve and execute it smoothly.
Thank you.
MGS
Also the error *ExcelFile cannot be resolved to a type*
ReplyDeleteweb design training in chennai
ReplyDeletephp training in chennai
dot net training in chennai
java training in chennai
embedded training in chennai
Nice post!thank you for the informative post.
ReplyDelete