Print

How to Run Selenium Tests with C# and NUnit

Selenium automation tests can be run as a Console Application, Selenium Service, and NUnit Framework. Since NUnit is used by developers for unit testing, QA engineers can also develop and run Selenium tests with NUnit. Here are the steps for using Nunit with Selenium tests. This example uses Selenium Web Driver and NUnit for Windows.

1. Launch Visual Studio 2010 and create a new class library project and name it as descriptive as possible. In most cases, include test application and component name in the project name.

2. Add the nunit.framework.dll to the reference.

3. Add Selenium .Net Web Driver dlls to the reference.

4. Add the following code to your class library project and compile it.

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;

namespaceSeleniumUnitTest
{
    [TestFixture]
    public class SeleniumTest
    {
        private IWebDriver driver;

Print

Ajax Call Synchronization in Selenium Web Driver

Recently many web applications are using Ajax technology to render information on the web page. When using Selenium Web Driver to automate test cases, Web Driver should wait until the active Ajax calls in the system complete. The WaitForAjaxCallsToComplete() method below will dynamically wait until there is no active Ajax calls in the system. Depending on your application, you can set the timeout in the App.Config or just give a hardcoded timeout.

 public void WaitForAjaxCallsToComplete()
        {
            try
            {
var wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["TimeOut"].ToString())));
            for (int i = 0; i < 30; i++)
                wait.Until(x => (bool)((IJavaScriptExecutor)x).ExecuteScript("return jQuery.active==0"));
            //wait.Until(driver => ((IJavaScriptExecutor)_webDriver).ExecuteScript("return document.readyState").Equals("complete"));
            }
            catch (Exception)
            {

                Console.WriteLine("No Ajax call is detected");
            }
            
            }

Print

Write test result to a specific cell in Excel spreadsheet

In some cases, automation engineers need to write test results to a specific cell in an Excel spreadsheet. The code snippet below is for your reference. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop.Excel;

namespace com.seleniummaster
{
//this method will write test result to a specific cell in Excel spreadsheet
public static void WriteToExcelFile(string fpath, string fname, string resutlToWrite, int cellrow, int cellcolumn)
{

Print

Drop Down Menu Test Selenium C# Code

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class DropDownMenuTest
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
       
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "http://seleniummaster.com";
            verificationErrors = new StringBuilder();
        }
   

Print

Drop Down Menu Test Selenium Java Code

Drop Down Menu Test in Java

package com.example.seleniummaster;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class DropDownMenuTest {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://seleniummaster.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
Print

Drop Down Menu Test Selenium IDE Steps

This screenshot below teaches you how to test drop down menu on the web with Selenium IDE. The steps you need to follow:

Testing Page: http://seleniummaster.com/sitecontent/seleniummenutest/seleniummenutest.html

(1) Mouse over on the root of the menu (purpose is to open the menu items underneath)

(2) Click the menu item one at a time

(3) Assert the menu item link page

(4) Go back and wait

(5) Mouse over on the root of the menu again

(6) Click another menu item

(7) Asset the menu item link page

Print

Selenium Ajax Synchronization

This method below applies to Selenium .Net

When you test web applications, sometimes you need to wait an active AJAX process is completed. You can achieve a WaitForAjax method with few lines of code as listed below.

In this method, when you call the WaitForAjax method, you can define maximum timeout time for the AJAX to be completed. For example, if the active AJAX should be completed within 40 seconds, you can call the method : WaitForAjax(40);

This method below tells you how much actual time the AJAX synchronization took by looking at the message on the console.

public void WaitForAjax(int timeoutInSeconds)
        {
            TimeSpan duration;
            DateTime currentTime = DateTime.Now;
            Console.WriteLine("Ajax is synchronizing  ...............");
            while (true)
            {

                bool complete = (bool)((IJavaScriptExecutor)_webDriver).ExecuteScript("return jQuery.active==0");
                DateTime newCurrentTime = DateTime.Now;
                duration = newCurrentTime - currentTime;
                if (complete || duration.Seconds >= timeoutInSeconds)
                    break;
                Thread.Sleep(1000);

            }
            Console.WriteLine("Total elapsed time is : " + duration.Seconds);
        }