Note| Text in Blue [#PYTHON] and Orange [#JAVA] can be edited or mentioned important for the entire blog. All the posts are practically done by me.
Text File | @DataProvider
The below example illustrates on how to work with @dataprovider in-order to fetch values from a text file.
1| Create a Test class file similar to the one given below:
package packagename;
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Google {
private WebDriver driver;
@DataProvider(name="keywords")
public Object[][] data() throws Exception {
HashMap<String,String[]> dataSet= new Text2TestData(System.getProperty("user.dir")+"\\config.txt").getData();
String search1Strings[]=dataSet.get("search1");
String search2Strings[]=dataSet.get("search2");
int size =search1Strings.length;
// modify 2 upon the no. of rows; Here, I used two rows, 'search1' & 'search2'
Object[][] creds = new Object[size][2];
for(int i=0;i<size;i++)
{
creds[i][0]=search1Strings[i];
creds[i][1]=search2Strings[i];
}
return creds;
}
@BeforeTest
public void setUp() throws Exception {
driver = new ChromeDriver();
}
@Test(dataProvider = "keywords", description = "Google_Test")
public void search(String search1, String search2) throws Exception {
driver.get("http://www.google.co.in");
// search google via keyword 1
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys("" + search1);
driver.findElement(By.name("q")).submit();
Thread.sleep(4000);
// search google via keyword 1
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys("" + search2);
driver.findElement(By.name("q")).submit();
Thread.sleep(4000);
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}
Text2TestData
2| Create another java class file, 'Text2TestData' under the same package.
package packagename;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class Text2TestData {
private String fileLocation;
public Text2TestData(String fileLocation) {
this.fileLocation = fileLocation;
}
public HashMap<String,String[]> getData(){
FileInputStream fs;
HashMap<String,String[]> keyValuePair=new HashMap<String,String[]>();
try (BufferedReader br = new BufferedReader(new FileReader(fileLocation))){
String stringLine;
//Read File Line By Line
while ((stringLine = br.readLine()) != null) {
// Print the content on the console
String[] keyValue=stringLine.split("=");
keyValuePair.put(keyValue[0],keyValue[1].split(","));
}
} catch (Exception e) {
e.printStackTrace();
}
return keyValuePair;
}
}
3| Create a text data-source file similar to the contents given below separated with comma (,)
search1=Prashanth Sams,bypasshacker
search2=seleniumworks.com,bypasshacker.blogspot.com
4| Now, place the config.txt file inside your project
5| Save the file and run test :)
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Google {
private WebDriver driver;
@DataProvider(name="keywords")
public Object[][] data() throws Exception {
HashMap<String,String[]> dataSet= new Text2TestData(System.getProperty("user.dir")+"\\config.txt").getData();
String search1Strings[]=dataSet.get("search1");
String search2Strings[]=dataSet.get("search2");
int size =search1Strings.length;
// modify 2 upon the no. of rows; Here, I used two rows, 'search1' & 'search2'
Object[][] creds = new Object[size][2];
for(int i=0;i<size;i++)
{
creds[i][0]=search1Strings[i];
creds[i][1]=search2Strings[i];
}
return creds;
}
@BeforeTest
public void setUp() throws Exception {
driver = new ChromeDriver();
}
@Test(dataProvider = "keywords", description = "Google_Test")
public void search(String search1, String search2) throws Exception {
driver.get("http://www.google.co.in");
// search google via keyword 1
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys("" + search1);
driver.findElement(By.name("q")).submit();
Thread.sleep(4000);
// search google via keyword 1
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys("" + search2);
driver.findElement(By.name("q")).submit();
Thread.sleep(4000);
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}
Text2TestData
2| Create another java class file, 'Text2TestData' under the same package.
package packagename;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class Text2TestData {
private String fileLocation;
public Text2TestData(String fileLocation) {
this.fileLocation = fileLocation;
}
public HashMap<String,String[]> getData(){
FileInputStream fs;
HashMap<String,String[]> keyValuePair=new HashMap<String,String[]>();
try (BufferedReader br = new BufferedReader(new FileReader(fileLocation))){
String stringLine;
//Read File Line By Line
while ((stringLine = br.readLine()) != null) {
// Print the content on the console
String[] keyValue=stringLine.split("=");
keyValuePair.put(keyValue[0],keyValue[1].split(","));
}
} catch (Exception e) {
e.printStackTrace();
}
return keyValuePair;
}
}
3| Create a text data-source file similar to the contents given below separated with comma (,)
search1=Prashanth Sams,bypasshacker
search2=seleniumworks.com,bypasshacker.blogspot.com
4| Now, place the config.txt file inside your project
5| Save the file and run test :)
This comment has been removed by the author.
ReplyDeleteIt was very nice article and it is very useful to Selenium learners.We also provide Cub training software online training.
ReplyDeleteHi Prasanth,
ReplyDeleteHow can i use @DataProvider(name="keywords") in another class @Test annotation(Class in same project but present other Package)
for POM framework
Thanks for sharing this nice blog.
ReplyDeleteRegards..
Selenium Training in Chennai
Thanks for the great information in your blog Selenium Training in Chennai
ReplyDeleteThanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about selenium training in chennai
ReplyDeletehttp://cheyat.com/qa/selenium-online-training-tutorials
ReplyDeletenice about selenium...
ReplyDeleteSAS Institute introduced the SAS Certified Professional Program.
SAS online training in hyderabad
seleni
ReplyDeleteinformatica online training
um data driven text file..
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteJava Training in Chennai Java Training in Chennai | Core Java Training in Chennai
Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes
Java Training Institutes in Chennai Java Training in Chennai
Well explained. Got to learn new things from your Blog on Appium.
ReplyDeleteAppium training in chennai
In some places i can't understand your concept like driver.findElement(By.name("q")).sendKeys("" + search2)can you explain this code.
ReplyDeleteSelenium Training in Chennai
Valuable information thanks for sharing Selenium Online Training
ReplyDeleteThanks for sharing great information.
ReplyDeleteselenium training in chennai
very helpful for my site. I always follow your tips....
ReplyDeletemsbi training in chennai
Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.selenium training in chennai
ReplyDelete
ReplyDeleteThanks for posting this useful content, Good to know about new things here, Let me share this, . cisco training in pune
Advanced Selenium Framework Training in Chennai by Vishwa
ReplyDeleteHi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.
I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
selenium training in chennai
It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai
ReplyDeleteThis is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
ReplyDeleteSelenium Training in Chennai
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteAndroid Training in Chennai
Ios Training in Chennai
The Information which you provided is very much useful for Testing Training Learners Thank You for Sharing Valuable Information. I like this blog and this is very informative.
ReplyDeleteSelenium Training in Bangalore
Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating. Software Testing Training in Chennai | Selenium Training in Chennai
ReplyDeleteAt Coepd - (Center of Excellence for Professional Development) Manual & Selenium testing training program is designed to give participants the skills & knowledge to gain a competitive advantage in starting/enhancing a career in software testing. We provide the attendee's software testing service which is required to ensure that tested applications meet all application requirements. Participants receive up-to-date training in multiple areas in Software Testing and a thorough understanding of real-world projects. Our collaborative ecosystem comprising of Partnerships with Software Companies enables real time software test life cycle experience.
ReplyDeletehttp://www.coepd.com/TestingTraining.aspx
Thanks For Posting A Great Article On Selenium Testing Tutorial
ReplyDelete