Jan 13, 2017

Case Insensitive Xpath

It has been observed that for many a times we would need to play with XPaths while working on web based automation using Selenium Web Driver. It was one of the needs to handle the case insensitive xpath, and looking at the xpath functions I found out lower-case and translate functions, using which we can work case insensitivity.

You can get more details here for these two functions.

Jan 3, 2017

C# Selenium Webdriver - Code to take browser parameter from config file

App.Config::

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 
    <configSections>
 
    </configSections>
 
  <appSettings>
    <add key="browser" value="ff" />
    <add key ="url" value="YOUR APPLICATION URL"/>
    <add key ="userName" value="YOUR APPLICATION USERNAME"/>
    <add key ="password" value ="YOUR APPLICATION PASSWORD"/>
  </appSettings>

</configuration>
       

Class File code::


IWebDriver driver;
        /// <summary>
        /// This method is used to start the browser instance depending on the browser parameter from configuration file.
        /// </summary>
        [TestInitialize]
        public void Start()
        {
            switch (ConfigurationManager.AppSettings["browser"])
            {
                case "ff":
                    {
                        driver = new FirefoxDriver();
                        break;
                    }
                case "ch":
                    {
                        driver = new ChromeDriver();
                        break;
                    }
                case "ie":
                    {
                        driver = new InternetExplorerDriver();
                        break;
                    }
                default:
                    {
                        Console.WriteLine("Something is wrong.. Please check.");
                        break;
                    }
            }
        }

Selenium Webdriver window handling with C#

/// <summary>
/// Wrapper to switch to new window
/// </summary>
public void SwitchToNewWindow()
{          
     driver.SwitchTo().Window(driver.WindowHandles.Last());
}

/// <summary>
/// Wrapper to switch to original window
/// </summary>
public void SwitchToOriginalWindow()
{
     driver.SwitchTo().Window(driver.WindowHandles.First());
}