Unit Testing With Selenium Web Driver - Part Two

In my previous article, Unit Testing With Selenium Web Driver-Part One:
I have explained what Selenium is, the latest version of Selenium (that is Selenium 3.0), how to identify a control by its control properties, and how to work with Selenium by creating Unit 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, Firefox etc.

Each browser has its own drivers, like to execute Selenium scripts in chrome we have hrome drivers, to execute Selenium scripts in Internet Explorer we have Internet Explorer drivers, to execute Selenium scripts in Edge browsers we have Edge drivers, to execute Selenium scripts in Firefox we have Gecko drivers etc.

In order to download Chrome Driver we have to navigate to this Url https://www.seleniumhq.org/download/ and scroll down to find various browser's driver download options.There you will find Google Chrome Driver, click on the version provided for example : 2.36 version.


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


For executing 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 fFrefox browser, it have 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 inbuit support for Firefox browser, we don't need to download any driver for fFrefox in selenium 2.0. If we are using Selenium 3.0 we must download a Firefox driver called Mozilla Geckodriver, click on the version provided for example : 0.20.0 like below.

After clicking on the version it will navigate to another screen with various download options based on our system requirement.If we are using 32bit windows or 64 bit windows then we can download any of these like below.
If we are using Mac O.S or Linux O.S then we have other options to download as well which we can see in the above image.

For executing selenium scripts on opera browser we have to use OperaDriver class and its methods which are part of OpenQA.Selenium.Opera dll. For downloading the opera driver we need to 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.

If we are using Windows 32 bit or 64 bit, we can download based on our system requirement; selenium supports execution of scripts in opera browser for other operating systems like Mac OS, Linux OSetc.

If we want to execute a Selenium script on Internet Explorer we have seperate class called InternetExplorerDriver which has various Methods which is part of OpenQA.Selenium.IE namespace like below, 

For downloading Internet Eplorer driver we have "The Internet Explorer Driver Server" on the download page screen like below.

From the above image we have two different options available like a 32 bit version and a 64 bit version, we can download this based on our system requirement.

After downloading all the drivers like Opera, Firefox, Chrome, Safari, Internet Explorer Driver etc., we can unzip the rar file it and place it in some folder like D:\SeleniumDrivers etc.

For executing Selenium scripts on different browsers like Opera, Chrome, Firefox etc we need to access those downloaded drivers which we have placed in D:\SeleniumDrivers path like below. 
  1. 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.
  1. IWebDriver driver= new FirefoxDriver("D:\SeleniumDrivers\geckodriver-v0.20.0-win32");  
  2. (or)  
  3. IWebDriver driver= new FirefoxDriver("D:\SeleniumDrivers\geckodriver-v0.20.0-win64");  
Here 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.
  1. IWebDriver driver= new InternetExplorerDriver("D:\SeleniumDrivers\internetExplorerDriver_win32");  
  2. (or)  
  3. 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.
  1. IWebDriver driver= new OperaDriver("D:\SeleniumDrivers\operadriver_win32");  
  2. //(or)  
  3. 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.

Example to understand the Selenium Scripts:
  1. using System;  
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  3. using OpenQA.Selenium;  
  4. using OpenQA.Selenium.IE;  
  5. using OpenQA.Selenium.Chrome;  
  6. using System.Threading;  
  7. using OpenQA.Selenium.Support.UI; //SelectElement  
  8. using OpenQA.Selenium.Interactions; //Actions  
  9. using OpenQA.Selenium.Firefox;  
  10. using OpenQA.Selenium.Opera;  
  11. using OpenQA.Selenium.Support;  
  12. using MbUnit.Framework;  
  13. namespace selenium {  
  14.     [TestClass]  
  15.     public class UnitTest1 {  
  16.         [TestMethod]  
  17.         public void TestMethod1() {  
  18.                 ChromeOptions cop = new ChromeOptions();  
  19.                 cop.AddArgument("disable-infobars");  
  20.                 IWebDriver driver = new ChromeDriver(@ "D:\Selenium_Drivers\chromedriver_win32New", cop);  
The above lines of code are used for removing the popup called "Chrome is being controlled by automation test software", while in the automation of any web application there maybe a chance of getting an error or ElementNotfoundException when control is hidden under the popup or when popup overlaps on any control, then we get controls displaying property as false, so Selenium may not be able to find the control and it throws an ElementNotFoundException.

In order to over come the above problem we need to remove the popup while execution is performed.

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

In ChromeDriver() method, the first parameter accepts the path of the chromedriver.exe, where we have installed the drivers and second parameter accepts the Chromeoptions.

With this we can launch the Chrome browser and remove or disable the popup.
  1. driver.Manage().Window.Maximize();  
  2. driver.Navigate().GoToUrl("https://www.google.co.in/");  
  3. Thread.Sleep(3000); 
Once the browser is launched, the above lines of code are used for maximizing the browser with Maximize() method, initially when browser was launched it will be in a minimized state or minimized window, and we need to maximize it.

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

GoToUrl() method accepts one parameter, here we need to provide the Url like https://www.google.co.in. It will navigate to that particular Url and wait for 3 seconds as provided with Thread.Sleep Method. We can also provide Wait Time for a control using WebDriverWait Class like: 
  1. WebDriverWait waitTime= new WebDriverWait(driver, TimeSpan.FromSeconds(3)); 
With this it will remove the popup and navigate to the provided Url.
  1. IWebElement googleText = driver.FindElement(By.Id("lst-ib"));  
  2. highlightElement(driver, googleText);  
  3. googleText.SendKeys("C# Corner");  
  4. Thread.Sleep(2000);  
  5. 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, XPath etc.

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

We are calling HighlightElement() method by passing driver and element's variable, with this it will highlight the control with red and enter the given text that is C# Corner in the textbox control and wait for 2 seconds as provided with Thread.Sleep method like below.
  1. Actions action = new Actions(driver);  
  2. action.SendKeys(Keys.Enter).Build().Perform();  
  3. Thread.Sleep(2000);  
Once the given text that is C# Corner is entered in the textbox, the above code is for entering by using, "Keys.Enter", which performs the click on the keyboard's Enter key.
  1. IWebElement linkclickCsharp = driver.FindElement(By.XPath("//*[@id='rso']/div[1]/div/div/div/div/h3/a"));  
  2. highlightElement(driver, linkclickCsharp);  
  3. linkclickCsharp.Click();  
  4. Thread.Sleep(2000);  

From the above code, we can identify the hyperlink by using its control property like XPath, from the above image, once the control is identified we can click on the hyperlink by using Click() method.
  1. IWebElement signup = driver.FindElement(By.CssSelector(".icons.signUp"));   
  2. HighlightElement(driver, signup);  
  3. signup.Click();  
  4. Thread.Sleep(2000);  
From the above image we can identify the signup control by its ClassName control property, if the value of the className property has spaces then we need to replace the space with dots(.) and need to use CssSelector property, otherwise we will get exception as ElementNotFoundException. If there is no space in className property then we can access the control by using ClassName.

Once the control is identified then it will click on the signup by using Click() method.
  1. IWebElement email = driver.FindElement(By.Name("ctl00$ContentMain$TextBoxEmail"));  
  2. highlightElement(driver, email);  
  3. email.SendKeys("Khaja.moiz@gmail.com");  
  4. Thread.Sleep(2000);  
From the above image we can identify the Enter Email textbox by its Name property. Once the control is identified we can enter the value to the textbox by using SendKeys method.

  1. IWebElement fname = driver.FindElement(By.Id("ctl00_ContentMain_TextBoxFirstName"));  
  2. highlightElement(driver, fname);  
  3. fname.SendKeys("Khaja");  
  4. Thread.Sleep(2000);   

Here with firstName control we are identifying with Id property as shown in the above image. Once the control is identified we can enter the given value using SendKeys method.
  1. IWebElement lname = driver.FindElement(By.XPath("//*[@id='ctl00_ContentMain_TextBoxLastName']"));  
  2. highlightElement(driver, lname);  
  3. lname.SendKeys("Moizuddin");  
  4. Thread.Sleep(2000);  
From the above code we are identifying a control "LastName" by its control properties like XPath="//[@id='ctl00_ContentMain_TextBoxLastName']".

Once the control is identified we are highlighting that control and entering the name by using SendKeys method.
  1. IWebElement enterpassword = driver.FindElement(By.Name("ctl00$ContentMain$TextBoxPassword"));  
  2. highlightElement(driver, enterpassword);  
  3. enterpassword.SendKeys("1234567890");  
  4. Thread.Sleep(2000);  
From the above code, we are identifying a field "Password" by its control properties Name.

Once the control is identified, we are highlighting the control and entering the password 1234567890 to the Password field.
  1. IWebElement confirmPassword = driver.FindElement(By.Id("ctl00_ContentMain_TextBoxPasswordConfirm"));  
  2. highlightElement(driver, confirmPassword);  
  3. confirmPassword.SendKeys("1234567890");  
  4. Thread.Sleep(3000);  
The above lines of code are used for identifying the control "Confirm Password" by its control property like Id="ctl00_ContentMain_TextBoxPasswordConfirm".

Once the control or element that is "Confirm Password" is identified then we will be entering the value 1234567890 to Confirm Password element.

Similarly for other controls we are identifying it by its control properties. Once the element or control is identified then we are performing actions on it.
  1. IWebElement dropcountry = driver.FindElement(By.Name("ctl00$ContentMain$DropdownListCountry"));  
  2. highlightElement(driver, dropcountry);  
  3. dropcountry.Click();  
  4. SelectElement select = new SelectElement(dropcountry);  
  5. select.SelectByValue("India");  
  6. Thread.Sleep(3000);  
  7. IWebElement enterzip = driver.FindElement(By.Name("ctl00$ContentMain$TextBoxZip"));  
  8. highlightElement(driver, enterzip);  
  9. enterzip.SendKeys("500076");  
  10. Thread.Sleep(3000);  
  11. IWebElement entercity = driver.FindElement(By.XPath("//*[@id='TextBoxCity']"));  
  12. highlightElement(driver, entercity);  
  13. entercity.SendKeys("Hyderabad");  
  14. Thread.Sleep(3000);  
  15. IWebElement selectsecurity = driver.FindElement(By.Id("ctl00_ContentMain_DropdownListSecurityQuesion"));  
  16. highlightElement(driver, selectsecurity);  
  17. SelectElement se = new SelectElement(selectsecurity);  
  18. se.SelectByIndex(2);  
  19. Thread.Sleep(2000);  
  20. IWebElement answer = driver.FindElement(By.Id("ctl00_ContentMain_TextBoxAnswer"));  
  21. highlightElement(driver, answer);  
  22. answer.SendKeys("XYZ Technologies");  
  23. Thread.Sleep(2000);  
  24. //IWebElement registerMe = driver.FindElement(By.Name("ctl00$ContentMain$ButtonSave"));  
  25. //highlightElement(driver, registerMe);  
  26. //registerMe.Click();  
  27. Thread.Sleep(2000);  
  28. IWebElement clickhome = driver.FindElement(By.XPath("//*[@id='ctl00_HeaderNewDesign1_HeaderMenu']/div/div/ul/li[1]/a"));  
  29. highlightElement(driver, clickhome);  
  30. clickhome.Click();  
  31. Thread.Sleep(2000);  
  32. IWebElement searchClick = driver.FindElement(By.Name("ctl00$HeaderHomeNewDesign$searchImageButton"));  
  33. highlightElement(driver, searchClick);  
  34. searchClick.Click();  
  35. Thread.Sleep(2000);  
  36. IWebElement peopleClick = driver.FindElement(By.Id("tabAuthorSearch"));  
  37. highlightElement(driver, peopleClick);  
  38. peopleClick.Click();  
  39. Thread.Sleep(2000);  
  40. IWebElement firstNamePeople = driver.FindElement(By.Id("TextBoxFirstName"));  
  41. highlightElement(driver, firstNamePeople);  
  42. firstNamePeople.SendKeys("khaja");  
  43. Thread.Sleep(2000);  
  44. IWebElement clickgo = driver.FindElement(By.XPath("//*[@id='ctl00_ContentMain_PanelAuthorSearch']/input[3]"));  
  45. highlightElement(driver, clickgo);  
  46. clickgo.Click();  
  47. Thread.Sleep(5000);  
  48. IWebElement profileClick = driver.FindElement(By.XPath("//*[@id='authorSearchResult']/div/div/ul/li[1]/a"));  
  49. highlightElement(driver, profileClick);  
  50. profileClick.Click();  
  51. Thread.Sleep(2000);  
  52. IWebElement articlesClick = driver.FindElement(By.XPath("//*[@id='divContributes']/ul/li[1]/a"));  
  53. highlightElement(driver, articlesClick);  
  54. articlesClick.Click();  
  55. Thread.Sleep(2000);  
  56. IWebElement articleClick = driver.FindElement(By.XPath("//*[@id='ctl00_ContentMain_contentBoxUL']/li[1]/div[2]/h3/a"));  
  57. highlightElement(driver, articleClick);  
  58. articleClick.Click();  
  59. }  
  60. public void highlightElement(IWebDriver driver, IWebElement element) {  
  61.     try {  
  62.         var jsDriver = (IJavaScriptExecutor) driver;  
  63.         string highlightWhite = @ "arguments[0].style.cssText = "  
  64.         "border-width: 2px; border-style: solid; border-color:"  
  65.         ";";  
  66.         string highlightJavascript = @ "arguments[0].style.cssText = "  
  67.         "border-width: 2px; border-style: solid; border-color: red"  
  68.         ";";  
  69.         jsDriver.ExecuteScript(highlightJavascript, new object[] {  
  70.             element  
  71.         });  
  72.         Thread.Sleep(2000);  
  73.         // jsDriver.ExecuteScript(highlightWhite, new object[] { element });  
  74.         //OpenQA.Selenium.IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;  
  75.         //jse.ExecuteScript("arguments[0].setAttribute('style,'border: solid 2px red'');", element);  
  76.         //for (int i = 0; i < 2; i++)  
  77.         //{  
  78.         //  
  79.         // jse.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);",  
  80.         // element, "color: yellow; border: 2px solid yellow;");  
  81.         // //// jse.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);",  
  82.         // // element, "");  
  83.         //}  
  84.     } catch (Exception ex) {  
  85.         throw ex;  
  86.     }  
  87. }  
  88. }  
  89. }  
Thanks & I hope this article helps you.