I am using cucumber , Junit , Selenium Web driver and have JDBC connection
I have a feature with below scenario Scenario Outline: Fill out the ATS Registration Form
When Click on SignUp And first name is '' And last name is '' And username is '' And phone is '' And Email is '' And companyname is '' And companytype is '' And address is '' And country is '' And province is '' And city is '' And Postal code is '' And reference is '' And ecommunications is Yes And termsandcondition is enabled And click on submit Then verify the successmessage And check the new user in database
Examples:
|firstname | lastname | username | phonenumber | emailaddress | companyname |companytype | address |country | province | city | postalcode | reference |
| Ram | Sita | kaka1123456 | 64189523 |[email protected] | AAAACCCC | Architect | AAAAA | Canada | Ontario | Toronto | M9V3G2 | Other |
I have a step definition which map to this feature file and page object model.
I have one class for jdbc connection.
package ca.test.utils;
import java.sql.*;
public class Jdbcdemo {
ConfigReader reader = new ConfigReader();
public void jdbcdemo(){
try {
// Get a connection to database
Connection myConn = DriverManager.getConnection("NA" , "NA", "NA");
// Create a statement
Statement mystatement = myConn.createStatement();
//Execute sql
ResultSet myRs = mystatement.executeQuery("select * from `user` u where u.username = ('kaka1123456')");
//process the result
while (myRs.next()){
String actual = myRs.getString("username");
System.out.println("username" + actual );
}
myRs.close();
mystatement.close();
myConn.close();
}
catch (Exception exc){
exc.printStackTrace();
}
}
}
The result is printing on console
Is there any way to compare the feature file data ( username ) with sql data ? if not then how can i compare the data?
2 Answers 2
It is obviously possible. I won't give you the code but I'll give you the direction:
- First you should rework
public void jdbcdemo()
. Addstatic
modifier to the method. Also add the parameters definitions to the method. The parameter set is the same as yourthen
step definition accepts. - Rework
jdbcdemo()
method code. Where you create a statement, you should create prepared statement. - Bind the parameters values (which the method accepts) to your query through prepared statement mechanism.
- In the method use the query that selects the users from the database using the parameter values provided as the filter (
where
clause) - In the method instead of
while
useif
where you checkmyRs.next()
. If it isfalse
, set the test failed, otherwise set the test passed. - In your
then
step definition callJdbcdemo.jdbcDemo(parameters)
, whereparameters
are the values that are propagated to yourthen
step definition.
P.S. - I recommend you to rename the class and the method so that they reflect their purpose in the better way.
This is purely can be done on language level by writing utility custom methods to establish jdbc connections and validating the recordsets against feature file data inside step defs.
Instead of relying on any tool/frame work's out of the box feature , it is more flexible solution.