1. http://testingtoolstecniques.blogspot.com/
http://testingtoolstecniques.blogspot.com/
Writing your first MYSQL DB functional Test Case in J unit
In junit you can write test cases for any SQL Queries here I am using MYSQL however if you want to
perform this on some other D.B you will just have to change connection configurations according to that
D.B.
You need to download MY SQL Connector for java and import that in your project.
Steps:
Creating Connection Class
First Step is to create a connection class with data base here I am following singleton pattern for
creating connection with D.B
Example Code is :
import com.mysql.*;
import com.mysql.jdbc.Connection;
public class dbConnectionInstance {
public static Connection conn;
static String url= "jdbc:mysql://localhost:3306/";
static String dbName = "test_data_eoffice";
static String driver = "com.mysql.jdbc.Driver";
static String userName = "root";
static String password = "";
dbConnectionInstance()
{}
public static Connection MysqlConnect() {
if ( conn == null ) {
try {
Class.forName(driver).newInstance();
conn =
(Connection)DriverManager.getConnection(url+dbName,userName,password);
}
catch (Exception sqle) {
sqle.printStackTrace();
}
return conn;
}
else
return conn;
}
}
2. http://testingtoolstecniques.blogspot.com/
http://testingtoolstecniques.blogspot.com/
Benefit of using singleton pattern is that you won’t need to created create a
connection object ever time you will simply call this instance by
dbConnectionInstance. MysqlConnect(); and this method will return you connection.
Creating Functionalities Class
This class will contain execution of all queries which we want to test
First step is to create this class and identify a query here I am writing a test case for query “insert into
testdatacreatefile”
I created a function public void createFileTestDataGenarator()
Then I executed this query with in this function
Now my test functionality is ready to be called in a J-Unit Test case
Example code is :
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDataGenerator {
private Connection connect = dbConnectionInstance.MysqlConnect();
private Statement statement = null;
public void createFileTestDataGenarator()
{
try {
statement=connect.createStatement();
for(int i=1000; i<=2000; i++)
{ String fileRefNum="Test FIle Reference Number"+i;
String Subject="Test FIle Subject Number"+i;
String Keywords ="keyword"+i+"A,keyword"+i+"B";
String Decscription="Test FIle Description Number"+i;
int status=0;
statement.executeUpdate("insert into
testdatacreatefile(fileSubject,fileReferenceNumber,fileKeyword,fileDescription,testDa
taStatus) values
('"+Subject+"','"+fileRefNum+"','"+Keywords+"','"+Decscription+"',"+status+")");
}} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
public void Log(String log)
{
3. http://testingtoolstecniques.blogspot.com/
http://testingtoolstecniques.blogspot.com/
try {
statement=connect.createStatement();
statement.executeUpdate("insert into logger(step)
values ('"+log+"')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createDocumentTestDataGenarator()
{
try {
statement=connect.createStatement();
for(int i=1000; i<=2000; i++)
{
String DocumentRefNum="Test Document Reference Number"+i;
String Subject="Test Doucument subject "+i;
String Decscription="Test Document Description"+i;
String FromMinsitry="Some Test Mionistry"+i;
String FromDesigntation="Some Test Mionistry Designation
ABC"+i;
String FromName="Some Test Misnitry Named ABC:GM"+i;
String FromMedium="Some Test Courior Service"+i;
int status=0;
statement.executeUpdate("insert into
testdatacreatedocument(
DocumentRef,DocumentDesc,FromMinistry,FromDesignation,FromName,FromMedium,Stat
us) values
('"+DocumentRefNum+"','"+Decscription+"','"+FromMinsitry+"','"+FromDesigntation+"','"
+FromName+"','"+FromMedium+"',"+status+")");
}} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
4. http://testingtoolstecniques.blogspot.com/
http://testingtoolstecniques.blogspot.com/
Creating J unit Class
Left Click your project and add a J Unit Test Case
There are three basic attenuation is Junit
@Before: this attenuation is used for initialization
@ Test: with this attenuation you call your actual test or execute with in this
attenuation
@ After: this attenuation is used at the end of test e.g for closing all DB
connections
Next step is now to call you created functionality in @ test Attenuation
Create Object of that class and then call its instance like example below
@Test
public void testCreateFIle() throws Exception {
TestDataGenerator TDG= new TestDataGenarator();
TDG.createDocumentTestDataGenarator();
//FM.addNtoing("fazal.karim", baseUrl, driver, "Abdul Hafeez", "KPO",
"abdul.hafeeez");
// Thread.sleep(5000L);
5. http://testingtoolstecniques.blogspot.com/
http://testingtoolstecniques.blogspot.com/
}
At Last execute your Junit Class as a Junit project not as a Java Application
For knowing more about MY SQL and java refer following links
http://www.tutorialspoint.com/jdbc/
Writing your first Selenium Web Driver functional Test Case in J unit
First of all I scripts all functionalities in a web app in a separate class and I create a separate function for
every functionality like signin(string username, string password). Then I call these functions in sequence
in my Junit Test case
e.g you class contain 3 function sign in (), Register() , View Category(), clickProduct(), addToCart
Now in your junit test case if you need add to cart you need following steps
1. Sign in User by calling signin()
2. Open a category by calling View Category()
3. Click some product by calling clickProduct(),
4. Add product to cart by calling addToCart()
I this approach e.g you need a test case view product or just sign in you don’t need to rewrite test script
you will simply rearrange test steps
Example code is mentioned below: In this code I created a separate function for every functionality and
we can call this functionality a test step benefit of this approach is reusability but in this approach you
have to keep it in mind that every possible parameter must be kept dynamic
import java.awt.AWTException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;
public class FileManagment {
loginManager LM= new loginManager();
TestDataGenerator TDG=new TestDataGenerator();
testDataProvider TP=new testDataProvider();
String subject;
String FileRefNumber;
String Description;
String Keywords;
String fileName;
// private StringBuffer verificationErrors = new StringBuffer();
public void createDocument(WebDriver wd, String uID, String baseUrl) throws
ClassNotFoundException, InterruptedException
9. http://testingtoolstecniques.blogspot.com/
http://testingtoolstecniques.blogspot.com/
verifyNoting(reciverID,baseUrl, browser,fileName);
}
}
Now Add a Junit Test Case
Left Click your project and add a J Unit Test Case
There are three basic attenuation is Junit
@Before: this attenuation is used for initialization
@ Test: with this attenuation you call your actual test or execute with in this
attenuation
@ After: this attenuation is used at the end of test e.g for closing all DB
connections
Next step is now to call you created functionality in @ test Attenuation
Now call your web driver script with in @test