Sunday 10 April 2016

Database Testing with CodedUI


Lets discuss about the Database Testing with CodedUI (C#) with some sample code
Connection String plays a key role in connecting to the database
Which has a different format for different Databases like Oracle,SQL etc
//Below code will login and open the DB connection.
string connetionString = “Data Source=DBSeverName;Initial Catalog=DBName;User ID=UserName;Password=DBPassword”;
SqlConnection connection = new SqlConnection(connetionString);
connection.Open();


Once the DB connection is setup, we need to define a sql Command for executing and Sql Datareader for reading the results of the command
Once the results are read by the data reader, it is loaded into a DataTable

//Below code will help to execute the Select query.
String Selectquery = “Select FirstName from Emp where EmpID in (01)”;
SqlCommand command = new SqlCommand(Selectquery, connection);
SqlDataReader reader = command.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(reader);
Once the data is loaded into the datatable, all we need to do is verify with the expected output

//Below code will help to verify the data which above code fetch from the particular table.
String rowText;
rowText = DB.dataTable.Rows[0][DB.dataTable.Columns[0]].ToString();
if (rowText.Contains(“Mayank”))
{
      Console.WriteLine(“FirstName is macthing with expected result”);
}
else
{
      Console.WriteLine(“Verification is not passed”);
}

After the verification, Close the connection
//It will close the connection.
connection.Close();

Thursday 7 April 2016

Using a App.config file to have different Sections in CodedUI project

To make coded UI tests to work in different environments, or to have different configuration values for different test methods to override the source of data file, a config file can be setup rather than a data source. 

Things to be done to achieve this
  1. You’ll need to add a reference to the System.Configuration assembly for the solution
  2. Then add the custom elements to app.config file (which is added to test project) TestSiteConfigurationData section name in the app.config file
  3. Create configuration data class for a custom section in App.Config TestSiteConfigurationData.cs
  4. Create the Helper class file for reading the data from configuration data class TestSiteConfigurationHelper.cs
  5. Call the helper class in the Codedui Project and read the values from config file under your custom section

You might face an issue with some wrong value entered for the type of section in the app.config file

 <configSections>
    <section name="TestSiteConfigurationData"
             type="Configuring_Sections.TestSiteConfigurationData,Configuring Sections"/>
  </configSections>
   
Configuring_Sections.TestSiteConfigurationData - <ProjectNamespace>.<ConfigurationDataClassName>

Configuring Sections - the solution dll name i.e your CodedUI project name

Find the sample project implementing the above attached


Happy coding :)