Friday, 20 March 2015

C#-Selenium Code Snippets

Agenda:
The agenda for this blog is to Discuss various scenario based concept and code used in C# Selenium.

Highlighting Web Element in Script Run-Time:
There are some Scenarios where we need to verify/Identify WebElements in the Page ,We can do them in multiple approaches,We can High Light the Element which we are looking for and set the color with different colors for that element , in the Test Run time so we can Identify/verify that the same:

public void HighlightToVerify(By locator)

        {
        try{
               string AttributeValue = "border:3px solid green;";
               string Attribute = Driver.FindElement(locator).GetAttribute("style");
IJavaScriptExecutor JsDriver = ((IJavaScriptExecutor)Driver);
JsDriver.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);", Driver.FindElement(locator), AttributeValue);
               Thread.Sleep(100);
               JsDriver.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);", Driver.FindElement(locator), Attribute);
           }
        catch(Exception){

             }
        }


SendKeys using JavaScript:

There are scenarios where we need to use JavaScript for Entering Text in the Input field,the same can be achieved in the following way:

public void SendData(By Locator, String Text, int TimePeriod = 30)
        {
            try
            {
                IJavaScriptExecutor js = ((IJavaScriptExecutor)Driver);
                js.ExecuteScript("arguments[0].innerHTML='" + Text + "';", Driver.FindElement(Locator, TimePeriod));
            }
            catch (Exception ex)
            {
                Log.Error("Unable to Enter Text using IJavaScriptExecutor for Element with Locator: "+Locator);
                
            }
        }



Presence Of Certain Text in The Page Source:

In some cases we need to check the presence of some text in the Page Source,Here we can achieve the same:

public bool IsTextPresentOnPage(String Text)

        {
            try
            {
                return Driver.PageSource.Contains(Text);
            }
            catch (Exception)
            {
                Log.Info("The Text "+ Text +"is not Present in the Page Source ");
                return false;
            }

        }



Zoom -IN and Zoom Out :


In Selenium you might face some issues like interacting with elements that goes under task bar due to improper layout. 



In this scenario WebElement will not be visible ,for this scenario you need to zoom out the page again zoom in once action is performed successfully :



class Program

    {
        static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();

            driver.Navigate().GoToUrl("https://www.google.co.in/?gfe_rd=cr&ei=fxQ6VeiZNJTDuASzrYHQAg");
            driver.Manage().Window.Maximize();
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^{ADD}");
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^{SUBTRACT}");
            Thread.Sleep(3000);
            driver.Quit();

            

        }
    }

Download or Save Files :



In selenium sometimes we need to download or save files,we can achieve it by the below approach:


static void Main(string[] args)

        {

            IWebDriver driver = new FirefoxDriver();

            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.seleniumhq.org/projects/webdriver/");
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^s");
            Thread.Sleep(2000);
            String pathToSaveFile="C:\\Users\\Admin\\Desktop\\II";
            System.Windows.Forms.SendKeys.SendWait("^a");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait(pathToSaveFile);
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(3000);
            driver.Quit();

            


        }


Visit for More Automation Related Discussion:


https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos






Gallio-Automation Platform with MbUnit- C#-Selenium

Agenda Of the Blog: Introduction of  Gallio -an open source platform for .NET,  which provides run-time services and tools.

What is Gallio?

The Gallio Automation Platform is an open, extensible, and neutral system for .NET that provides a common object model, run-time services and tools (such as test runners) that may be leveraged by any number of test frameworks.

Features and use of Gallio


·         Tools Support

·         Frameworks supported by Gallio: MbUnit, NUnit, MSTest, NBehave, XUnit
·         Different Gallio Runners: GUI (Icarus), Command-line (Echo) etc.

·         Gallio can be used for the multiple purposes:




Why to use Gallio for Automation Testing with Selenium C#:


To control and manipulate the Test execution approach:    To run the Selenium C# code we need to compile the full project and get the .dll file and we need to use that generated .dll file as a input to any Test Runner (framework) to execute the automation script, and Gallio serves the purpose in robust manner with multiple approaches, like:


(a)    Running all suites as a whole binding:

(i)      Whole automation Project can be run as a whole by using “Gallio Echo” Runner tool with .dll file as input to it. Detailed Reports are generated after the execution of whole Automation project with XML and HTML format.

(b)   Execution of All or Few selected Test Suites and Test cases:

(i)      We can perform selection of Test Suites and Test cases from whole Test Automation suites and execute them. This can be achieved by using “Gallio Icarus” Runner tool which is basically an UI, the generated .dll file has to be added through the UI “Add Files” option to use the Compiled project’s Test Case and, we can select all or few test suites and test cases from the UI tree format to execute. Please refer the below screenshot for the Gallio Icarus UI with Test Report.





(i)       Detailed Reports are generated and visible from the UI (in Execution Log tab) itself and we can see the report after the execution of test case, we don’t need to wait until the Whole suite execution.


(b)     Gallio can be integrated with Build Tool like NAnt to build the full automation process.

(c)     Gallio can be integrated with CI tool like Jenkins. 







What is MbUnit?


The MbUnit is an extensible unit testing framework for the .Net Framework. MbUnit is a part of the Gallio bundle. It provides below rich features apart from the other generic features which is available for any other unit test framework for .Net Framework:

1.       We can perform Parallel Testing with MbUnit for C# Selenium with multiple instances of the browser.

2.       MbUnit Supports Retry option which is basically automatic triggering of failed test cases for the next time.


Visit for More Automation Related Discussion:


https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos