Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Learn How to Automate Browser Testing With Selenium WebDriver — Part 2

DZone's Guide to

Learn How to Automate Browser Testing With Selenium WebDriver — Part 2

This tutorial continues explaining the use of Selenium 2.0 for automated browser testing of your application with Selenium WebDriver.

· DevOps Zone ·
Free Resource

Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market. 

In my previous article, I explained the use of Selenium, the latest version of Selenium (Selenium 3.0), how can we identify a control by its control properties, and how to work with Selenium by creating a sample test project and downloading Selenium supportive dll's from NuGet packages. In this article, I am going to explain how to download Selenium drivers from Selenium's official website.

Selenium supports execution with various browsers, such as Google Chrome, Internet Explorer, Edge browser (Windows 10 Operating system), Safari, Opera, and Firefox.

Each browser has its own drivers; if we want to execute Selenium scripts in Chrome, we have Chrome drivers, etc.

In order to download the Chrome Driver, we have to navigate here and scroll down to find various browsers' driver download options. There, you will find Google Chrome Driver. Click on the version provided for the example, 2.36.

Image title

With this click, it will navigate to another screen with various options to download based on our system specifications. If you are using a Windows operating system, we can download chromedriver_win32.zip. If you are using other operating systems like Mac or Linux, we can download chromedriver_Mac64.zip or chromedriver_Linux64.zip etc.

Image title

To execute Selenium scripts on Firefox, we need to use FirefoxDriver Class and its methods, which are part of OpenQA.Selenium.Firefox Dll.

Similarly, if we want to execute our Selenium scripts on Firefox, it has its own driver called GeckoDriver, which is included as part of Selenium 3.0. In earlier versions of Selenium, like Selenium 2.0, Selenium has inbuilt support for the Firefox browser, so we don't need to download any driver. If we are using Selenium 3.0, we must download a Firefox driver called Mozilla's Gecko driver. Click on the version provided for the example, 0.20.0.

Image title

After clicking on the version, it will navigate to another screen with various download options based on our system requirements. If we are using 32bit Windows or 64bit Windows, we can download any of these.

Image title

If we are using Mac OS or Linux OS, then we have other options to download as well, which we can see in the above image.

To execute Selenium scripts on the Opera browser, we have to use the OperaDriver class and its methods which are part of OpenQA.Selenium.Opera dll. Click on the version, for example, 2.29, which will navigate to the download screen, and there you can choose based on your system requirements and the operating system which you are using.

Image title

Image title

If we are using Windows 32 bit or 64 bit, we can download that based on our system requirement. Selenium supports execution of scripts in Opera for other operating systems like Mac OS and Linux OS.

If we want to execute Selenium scripts on Internet Explorer, we have a separate class called InternetExplorerDriver, which has various methods and is part of the OpenQA.Selenium.IE namespace.

Image title

Image title

We have two different options available: 32bit and 64bit. We can download this based on our system requirements.

After downloading all the drivers, we can unzip the .rar file it and place it in a folder like D:\SeleniumDrivers etc.

To execute Selenium scripts on different browsers, we need to access those downloaded drivers which we have placed in D:\SeleniumDrivers path, like below.

IWebDriver driver= new ChromeDriver("D:\SeleniumDrivers\chromedriver_win32"); 

Here in the above code, we need to specify the path where the chromedriver.exe exists, like D:\SeleniumDrivers\chromedriver_win32. With this, it will launch the Chrome browser.

IWebDriver driver= new FirefoxDriver("D:\SeleniumDrivers\geckodriver-v0.20.0-win32");  
(or)  
IWebDriver driver= new FirefoxDriver("D:\SeleniumDrivers\geckodriver-v0.20.0-win64"); 

In the above code, we need to specify the path where the geckodriver.exe exists, like D:\SeleniumDrivers\geckodriver-v0.20.0-win32 (or) D:\SeleniumDrivers\geckodriver-v0.20.0-win64. With this, it will launch the Firefox browser.

IWebDriver driver= new InternetExplorerDriver("D:\SeleniumDrivers\internetExplorerDriver_win32");  
(or)  
IWebDriver driver= new InternetExplorerDriver("D:\SeleniumDrivers\internetExplorerDriver_win64");  

Here in the above code, we need to specify the path where the IEdriver.exe exists, like D:\SeleniumDrivers\internetExplorerDriver_win32 (or) D:\SeleniumDrivers\internetExplorerDriver_win64. With this, it will launch the Internet Explorer browser.

IWebDriver driver= new OperaDriver("D:\SeleniumDrivers\operadriver_win32");  
//(or)  
IWebDriver driver= new OperaDriver("D:\SeleniumDrivers\operadriver_win64");  

Here in the above code, we need to specify the path where the operadriver.exe exists, like D:\SeleniumDrivers\operadriver_win32 (or) D:\SeleniumDrivers\operadriver_win64. With this, it will launch the Opera browser.

Here's an example to understand the Selenium scripts:

using System;  
using Microsoft.VisualStudio.TestTools.UnitTesting;  
using OpenQA.Selenium;  
using OpenQA.Selenium.IE;  
using OpenQA.Selenium.Chrome;  
using System.Threading;  
using OpenQA.Selenium.Support.UI; //SelectElement  
using OpenQA.Selenium.Interactions; //Actions  
using OpenQA.Selenium.Firefox;  
using OpenQA.Selenium.Opera;  
using OpenQA.Selenium.Support;  
using MbUnit.Framework;  
namespace selenium {  
    [TestClass]  
    public class UnitTest1 {  
        [TestMethod]  
        public void TestMethod1() {  
                ChromeOptions cop = new ChromeOptions();  
                cop.AddArgument("disable-infobars");  
                IWebDriver driver = new ChromeDriver(@ "D:\Selenium_Drivers\chromedriver_win32New", cop);

The above lines of code are used for removing the popup "Chrome is being controlled by automation test software." In the automation of any web application, there is a chance of getting an error or ElementNotfoundException when control is hidden under the popup or when the popup overlaps on any control — then we get controls displaying a property as false, so Selenium may not be able to find the control and it throws an ElementNotFoundException. In order to overcome this problem, we need to remove the popup while execution is performed.

The ChromeOptions class contains a method called AddArgument(), with one parameter that is cop.AddArgument("disable-infobars"). "disable-infobars" is used for removing the popup at execution level. The ChromeOptions class is part of the OpenQA.Selenium.Chrome namespace.

In the ChromeDriver() method, the first parameter accepts the path of the chromedriver.exe, where we have installed the drivers, and the second parameter accepts the Chromeoptions. With this, we can launch the Chrome browser and remove or disable the popup.

Image title

driver.Manage().Window.Maximize();  
driver.Navigate().GoToUrl("https://www.google.co.in/");  
Thread.Sleep(3000);

Once the browser is launched, the above lines of code are used to maximize the browser with the Maximize() method. Initially, when the browser is launched, it will be in a minimized state, and we need to maximize it.

Once the browser is maximized, we need to navigate to the URL where we need to perform the automation by using the GoToUrl() method.

GoToUrl() accepts one parameter. Here, we need to provide the URL, like https://www.google.co.in. It will navigate to that UWL and wait 3 seconds as provided with the Thread.Sleep() method. We can also provide the wait time for a control using the WebDriverWait class:

WebDriverWait waitTime= new WebDriverWait(driver, TimeSpan.FromSeconds(3)); 
IWebElement googleText = driver.FindElement(By.Id("lst-ib"));  
highlightElement(driver, googleText);  
googleText.SendKeys("C# Corner");  
Thread.Sleep(2000);  
WebDriverWait waitTime= new WebDriverWait(driver, TimeSpan.FromSeconds(2));

Once the browser is launched by navigating to the www.google.com, we can identify the control by its control properties, like Id, ClassName, Name, and XPath.

In the above lines of code, we can find the control by using the FindElement() method and providing the Id property of the control. When we inspect the HTML code, each HTML element will have the various control properties mentioned above; if the control doesn't have any control properties, then we need to use its XPath property.

Here is the sample code for automating a C# Corner registration form:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using System.Threading;
using OpenQA.Selenium.Support.UI; //SelectElement  
using OpenQA.Selenium.Interactions; //Actions  
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Opera;
using OpenQA.Selenium.Support;
using MbUnit.Framework;
namespace selenium {
 [TestClass]
 public class UnitTest1 {
  [TestMethod]
  public void TestMethod1() {
   ChromeOptions cop = new ChromeOptions();
   cop.AddArgument("disable-infobars");
   IWebDriver driver = new ChromeDriver(@ "D:\Selenium_Drivers\chromedriver_win32New", cop);
   IWebElement googleText = driver.FindElement(By.Id("lst-ib"));
   highlightElement(driver, googleText);
   googleText.SendKeys("C# Corner");
   Thread.Sleep(2000);
   WebDriverWait waitTime = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
   IWebElement dropcountry = driver.FindElement(By.Name("ctl00$ContentMain$DropdownListCountry"));
   highlightElement(driver, dropcountry);
   dropcountry.Click();
   SelectElement select = new SelectElement(dropcountry);
   select.SelectByValue("India");
   Thread.Sleep(3000);
   IWebElement enterzip = driver.FindElement(By.Name("ctl00$ContentMain$TextBoxZip"));
   highlightElement(driver, enterzip);
   enterzip.SendKeys("500076");
   Thread.Sleep(3000);
   IWebElement entercity = driver.FindElement(By.XPath("//*[@id='TextBoxCity']"));
   highlightElement(driver, entercity);
   entercity.SendKeys("Hyderabad");
   Thread.Sleep(3000);
   IWebElement selectsecurity = driver.FindElement(By.Id("ctl00_ContentMain_DropdownListSecurityQuesion"));
   highlightElement(driver, selectsecurity);
   SelectElement se = new SelectElement(selectsecurity);
   se.SelectByIndex(2);
   Thread.Sleep(2000);
   IWebElement answer = driver.FindElement(By.Id("ctl00_ContentMain_TextBoxAnswer"));
   highlightElement(driver, answer);
   answer.SendKeys("XYZ Technologies");
   Thread.Sleep(2000);
   //IWebElement registerMe = driver.FindElement(By.Name("ctl00$ContentMain$ButtonSave"));  
   //highlightElement(driver, registerMe);  
   //registerMe.Click();  
   Thread.Sleep(2000);
   IWebElement clickhome = driver.FindElement(By.XPath("//*[@id='ctl00_HeaderNewDesign1_HeaderMenu']/div/div/ul/li[1]/a"));
   highlightElement(driver, clickhome);
   clickhome.Click();
   Thread.Sleep(2000);
   IWebElement searchClick = driver.FindElement(By.Name("ctl00$HeaderHomeNewDesign$searchImageButton"));
   highlightElement(driver, searchClick);
   searchClick.Click();
   Thread.Sleep(2000);
   IWebElement peopleClick = driver.FindElement(By.Id("tabAuthorSearch"));
   highlightElement(driver, peopleClick);
   peopleClick.Click();
   Thread.Sleep(2000);
   IWebElement firstNamePeople = driver.FindElement(By.Id("TextBoxFirstName"));
   highlightElement(driver, firstNamePeople);
   firstNamePeople.SendKeys("khaja");
   Thread.Sleep(2000);
   IWebElement clickgo = driver.FindElement(By.XPath("//*[@id='ctl00_ContentMain_PanelAuthorSearch']/input[3]"));
   highlightElement(driver, clickgo);
   clickgo.Click();
   Thread.Sleep(5000);
   IWebElement profileClick = driver.FindElement(By.XPath("//*[@id='authorSearchResult']/div/div/ul/li[1]/a"));
   highlightElement(driver, profileClick);
   profileClick.Click();
   Thread.Sleep(2000);
   IWebElement articlesClick = driver.FindElement(By.XPath("//*[@id='divContributes']/ul/li[1]/a"));
   highlightElement(driver, articlesClick);
   articlesClick.Click();
   Thread.Sleep(2000);
   IWebElement articleClick = driver.FindElement(By.XPath("//*[@id='ctl00_ContentMain_contentBoxUL']/li[1]/div[2]/h3/a"));
   highlightElement(driver, articleClick);
   articleClick.Click();
  }
  public void highlightElement(IWebDriver driver, IWebElement element) {
   try {
    var jsDriver = (IJavaScriptExecutor) driver;
    string highlightWhite = @ "arguments[0].style.cssText = "
    "border-width: 2px; border-style: solid; border-color:"
    ";";
    string highlightJavascript = @ "arguments[0].style.cssText = "
    "border-width: 2px; border-style: solid; border-color: red"
    ";";
    jsDriver.ExecuteScript(highlightJavascript, new object[] {
     element
    });
    Thread.Sleep(2000);
    // jsDriver.ExecuteScript(highlightWhite, new object[] { element });  
    //OpenQA.Selenium.IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;  
    //jse.ExecuteScript("arguments[0].setAttribute('style,'border: solid 2px red'');", element);  
    //for (int i = 0; i < 2; i++)  
    //{  
    //  
    // jse.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);",  
    // element, "color: yellow; border: 2px solid yellow;");  
    // //// jse.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);",  
    // // element, "");  
    //}  
   } catch (Exception ex) {
    throw ex;
   }
  }
 }
}

Thanks, and I hope this article helps you.

Find out more about how Scalyr built a proprietary database that does not use text indexing for their log management tool.

Topics:
selenium 2.0 ,devops ,selenium ,tutorial ,automated testing

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}