Print

Selenium Database Test In C#

Selenium Database Test

In every web application's back end, there is a database like SQL Server, My SQL, Oracle or other types of database support functions. When using Selenium to automate UI testing, we need to make sure any user data entered from the UI should be captured in the database. We can use database retrieve statement to retrieve data and then use the data to verify what is entered by the user.

Considering the example of a user registration. User registers himself/herself with a username and password. By establishing a DB connection and retrieving data from the DB, we can validate the username entered in the UI should match the username in the database.



Test Case: verify that user login information should be saved in the database when a user successfully registered in a site.

In this screenshot below, first two users were in the database. A new test user is entered from the application UI, so we will verify the username.

Print

Test API With Selenium Web Driver in C#

Many web applications provide API for external clients to use in their internal applications. When testing APIs, QA engineers need to verify that what UI displayed should be equal to the API response for the same request input.  In this article, we use the Open Weather API as an example to test the web response for this url "api.openweathermap.org/data/2.5/weather?q=London,uk"   should be the same as the API response. The code is written in c#. We use C# NUnit framework and Selenium web driver in this code example.

 

Step 1: create a C# NUnit project and add Selenium web driver and NUnit library as references. see the screenshot below. Add a NUnit class item named "WeatherApiTests.cs" and add a class item named "ApiResponse.cs".

Print

Selenium Web Driver Wait For Element Visibility

In Selenium test automation, sometimes, the test application should wait until an element is visible. Here is the code sample for it.  

In this sample, Selenium web driver will wait for an element visibility until the timeout seconds elapse. If timeOutInseconds is up, the code snippet will display a message on the console. 

Code Snippet
  1. private void WaitForElementVisible(By by, int timeOutInSeconds)
  2.         {
  3.             Stopwatch stopwatch = new Stopwatch();
  4.             stopwatch.Start();
  5.             try
  6.             {
  7.                 var wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(timeOutInSeconds));
  8.                 wait.Until(ExpectedConditions.ElementIsVisible(by));
  9.             }
  10.             catch (Exception)
  11.             {
  12.                 Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed.Seconds);
  13.             }
  14.         finally
  15.             {
  16.                 stopwatch.Stop();
  17.             }
  18.         }
Print

Selenium Web Driver Model View Controller Framework With C#

This example uses the console application explained in the article named "Selenium Web Driver with NUnit Console Application"

In this example you will learn how to use Model View Controller Framework (MVC Pattern)  in Selenium .Net test automation. The key feature of the sample is that you define all web elements in the site model class, so that you do not need to do change code in other classes that uses the web elements in your test.  This approach is very efficient when you have a dramatic UI change in your web application. You just need to update the element definition in the site element, so that your test application logic and work flow still work as before. 

Test result will be written as an html file, so that you can check the result on web browser. 

Ignore the TestRunner.cs file in this example's screenshot because it was part of the Selenium Web Driver with NUnit Console Application.

Model your test cases 

Print

Selenium Web Driver with NUnit Console Application

When using Selenium Web Driver with NUnit C#, if tests need to run sequencially,  writing a test automation Console Application is a good approach.  

In this example you will learn how to use Selenium WebDriver with NUnit. The key feature of the sample is that you use Selenium .Net WebDriver that will launch a browser and run each test sequencially in the console application. Test result will be written as an html file, so that you can check the result on web browser.  

Model your test cases 

Before you start automation, you need to know what are the tests and what you are going to validate. Let's break the test suite into 4 test cases. 

Test 1 (launchSiteAndLogin)
1. Go to http://www.seleniummaster.com/seleniummastertestapp/index.php
2. Enter "test" in the Username field
3. Enter "XXXX" in the Password filed
4. Click on the Login button
5. Verify that the text "Selenium Test" is present.

Print

How to use Selenium .NET Client Driver

To automate web tests with Selenium .Net , you need to follow the steps below:

  •  Extract the folder
  • Download and install NUnit ( Note: You can use NUnit as your test engine. Or you can also write a simple main() function to run your tests; however NUnit is very useful as a test engine.)
  •     Open your desired .Net IDE (Visual Studio)
  •     Create a class library (.dll)
  •     Add references to the following DLLs: nmock.dll, nunit.core.dll, nunit. framework.dll, ThoughtWorks.Selenium.Core.dll, ThoughtWorks.Selenium.IntegrationTests.dll and ThoughtWorks.Selenium.UnitTests.dll
Print

WebDriver Select Test Environment C# Code

When writing console application with Selenium WebDriver, sometime, users need to select a test environment, so that Selenium tests can navigate to the selected environment and do the test. In this code snippet below, users have the option to select Production, Staging, or QA environment within the timeout specified in the code. If the user does not press any key within the timeout period, the method will return the QA environment as default test environment.

 

Print

WebDriver Wait For Ajax Completion

Nowadays, most web applications use Ajax Call to retrieve data from the server. When using Selenium WebDriver, WebDriver needs to dynamically wait for Ajax call completion, so that the webpage under test is fully rendered when Ajax call is ended. This code below makes WebDriver wait until Ajax Call is over within the time out period that is defined in the App.Config file of the test application.

 

Print

Create Custom Firefox Profile for Selenium Web Driver

When Firefox is launched with Selenium Web Driver by default, certain plug-ins such as Firebug, Cookie Manager, and Selenium IDE  will not be available. Sometimes, we need to use these plug ins when debugging selenium tests. Therefore, creating custom profile on Firefox and launching web driver with the custom profile is essential for some cases. Here, I will explain how to create Firefox custom profile and use it with Selenium Web Driver. 

Print

Explicit and Implicit Waits in Selenium Web Driver

When implementing time synchronization for waiting with Selenium Web Driver technology, we can use two types of waits:

Explicit Wait

In Explicit Wait, we write a code to define a wait statement for certain condition to be satisfied until the wait reaches its timeout period. If WebDriver can find the element before the defined timeout value, the code execution will continue to next line of code. Therefore, it is important to setup a reasonable timeout seconds according to the system response.

For example: