Monday 30 January 2012

Dropdown box in selenium web driver in .net

To set the desired value into Dropdown box, the below piece of would work fine!
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
//add this name space to access WebDriverWait
using OpenQA.Selenium.Support.UI;
namespace AutomationUsingSelenium
{
    class Program
    {
        public static InternetExplorerDriver WebDriver;
        static void Main(string[] args)
        {                     
        var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
        capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
        WebDriver = new InternetExplorerDriver(capabilitiesInternet);
        WebDriver.Navigate().GoToUrl("http://www.x-rates.com/calculator.html");

 //Get the hold of the dropdown box by Name
 IWebElement setValueConvert = WebDriver.FindElement(By.Name("from"));
//Place the drop down into selectElemnet
 SelectElement clickThisitem=new SelectElement(setValueConvert);
 Thread.Sleep(600);
//Select the Item from dropdown by Index
 clickThisitem.SelectByIndex(12);
//Select the Item from dropdown by Text
 clickThisitem.SelectByText("EUR");


                                                                                                         

         }
    }
}
Wesite Under Test:
Thanks,
Md. Jawed

Wednesday 25 January 2012

Get screenshot of the web page in selenium using C# .net

            var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
            capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
          
            IWebDriver webDriver = new InternetExplorerDriver(capabilitiesInternet);
                       webDriver.Navigate().GoToUrl("http://www.google.com");
Screenshot screenshot = ((ITakesScreenshot) webDriver).GetScreenshot();
            screenshot.SaveAsFile("d:\\ScreenShot.png",System.Drawing.Imaging.ImageFormat.Png);

Wait for an element to load in selenium using C#

Sometimes while performing automation we have to wait for an element to load then only we can proceed to the next steps.
To achieve this selenium has provided explicit wait and implicit wait.
Here we will discuss about use of explicit wait .net using C#.
Using lambda Expression:
 Name space need to use to access WebDriverWait
Using OpenQA.Selenium.Support.UI

using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
//add this name space to access WebDriverWait
using OpenQA.Selenium.Support.UI;

namespace AutomationUsingSelenium
{
    class Program
    {
        static void Main(string[] args)
        {
            var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
            capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
          
            IWebDriver webDriver = new InternetExplorerDriver(capabilitiesInternet);
            webDriver.Navigate().GoToUrl("http://www.google.com");
            IWebElement query = webDriver.FindElement(By.Name("q"));
            query.SendKeys("http://seleniumdotnet.blogspot.com");

            OpenQA.Selenium.Support.UI.WebDriverWait wait = new OpenQA.Selenium.Support.UI.WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
            Console.WriteLine("Page title is: "+webDriver.Title);
            //using lambda expression
            wait.Until(d => d.FindElement(By.Id("ab_name")));//make sure that Search div appear on google

            Screenshot screenshot = ((ITakesScreenshot) webDriver).GetScreenshot();
        screenshot.SaveAsFile("d:\\ScreenShot.png",System.Drawing.Imaging.ImageFormat.Png);

            webDriver.Quit();
            Console.ReadLine();
           
        }
    }
}


Using Anonymous Method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
//add this name space to access WebDriverWait
using OpenQA.Selenium.Support.UI;

namespace AutomationUsingSelenium
{
    class Program
    {
        static void Main(string[] args)
        {
            var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
            capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
          
            IWebDriver webDriver = new InternetExplorerDriver(capabilitiesInternet);
            webDriver.Navigate().GoToUrl("http://www.google.com");
            IWebElement query = webDriver.FindElement(By.Name("q"));
            query.SendKeys("http://seleniumdotnet.blogspot.com");

            OpenQA.Selenium.Support.UI.WebDriverWait wait = new OpenQA.Selenium.Support.UI.WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
            Console.WriteLine("Page title is: "+webDriver.Title);
            //using Anonymous method
             wait.Until(delegate(IWebDriver d) { return d.FindElement(By.Id("ab_name")); });
            
            Screenshot screenshot = ((ITakesScreenshot) webDriver).GetScreenshot();
            screenshot.SaveAsFile("d:\\ScreenShot.png",System.Drawing.Imaging.ImageFormat.Png);

            webDriver.Quit();
            Console.ReadLine();
           
        }
    }
}

InternetExplorerDriver() issue by selenium in IE

While running your automation code in selenium using C#.
Then, most probably chances are that you will encounter the exception as Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones. (NoSuchDriver)”. And below is the screen sot for the same.


InvalidOperationException in IE

The cause of this issue is due to different security level and the ability to enable or disable Protected Mode. The error message is trying to tell you that Protected Mode must either be disabled or enabled for all zones because of a limitation in Selenium's InternetExplorerDriver.
To resolve this issue you need to just add 2 extra lines of code.
            var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
            capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);

The Whole Code would look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
namespace AutomationUsingSelenium
{
    class Program
    {
        static void Main(string[] args)
        {
            var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
            capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
            IWebDriver webDriver = new InternetExplorerDriver(capabilitiesInternet);
            webDriver.Navigate().GoToUrl("http://www.google.com");
            IWebElement query = webDriver.FindElement(By.Name("q"));
            query.SendKeys("http://jawedm.blogspot.com");
            Console.WriteLine("Page title is: "+webDriver.Title);
            webDriver.Quit();
            Console.ReadLine();

        }
    }
}


Hope this would be useful for you.
Thanks,
Md. jawed
Keep looking for this space to get different types of automation solutions using Selenium in .Net.