Selenium WebDriver
Selenium WebDriver API Commands and Operations
Find Element
- By ID
Example:
- <span id="spanName">name</span>
- IWebElement element = driver.FindElement(By.Id("spanName"));
- By Class Name
Example:
- <div class="mobile"><span>Nokia</span></div><div class="mobile"><span>IPhone</span></div><div class="mobile"><span>Samsung</span></div>
- IList<IWebElement> mobiles = driver.FindElements(By.ClassName("mobile"));
- By Tag Name
Example:
- <iframe src="..."></iframe>
- IWebElement frame = driver.FindElement(By.TagName("iframe"));
- By Name
Example:
- <input name="fname" type="text"/>
- IWebElement firstname = driver.FindElement(By.Name("fname"));
- By Link Text
Example:
- <a href="http://www.google.com/search?q=india">india</a>
- IWebElement country = driver.FindElement(By.LinkText("india"));
- By Partial Link Text
Example:
- <a href="http://www.google.com/search?q=india">search for india</a>
- IWebElement country = driver.FindElement(By.PartialLinkText("india"));
- By CSS
Example:
- <div id="emp"><span class="admin">sysAdmin</span><span class="user">guest</span></div>
- IWebElement user = driver.FindElement(By.CssSelector("#emp span.admin"));
- By XPATH
Example:
- <input type="text" name="fname" />
- <INPUT type="text" name="lname" />
- IList<IWebElement> inputs = driver.FindElements(By.XPath("//input"));
NUnit
NUnit is the third party tool developed for unit testing in Visual Studio
You can get more information on NUnit.
To integrate Nunit in visual studio first create new project in visual studio.
Also add references using Nuget packages related to NUNIT and WebDriver
Please find the following code to write test cases using NUnit
- using NUnit.Framework;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Chrome;
- using OpenQA.Selenium.Firefox;
-
- namespace NunitSelenium
- {
- [TestFixture]
- public class TestSeleniumClass
- {
-
- IWebDriver driverFF;
- IWebDriver driverGC;
-
-
- [SetUp]
- public void Init()
- {
-
- driverFF = new FirefoxDriver();
- driverGC = new ChromeDriver(@"C:\Downloads\chromedriver_win32");
- }
-
- [Test]
- public void TestFirefoxDriver()
- {
- driverFF.Navigate().GoToUrl("http://www.google.com");
- driverFF.FindElement(By.Id("lst-ib")).SendKeys("Selenium with C#");
- driverFF.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
- }
-
- [Test]
- public void TestChromeDriver()
- {
- driverGC.Navigate().GoToUrl("http://www.google.com");
- driverGC.FindElement(By.Id("lst-ib")).SendKeys("Selenium with C#");
- driverGC.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
- }
- }
- }
Before executing the test application download the browser specific driver.
Please find the following url to download the
driver.
And store the driver at the location
C:\Downloads\chromedriver_win32. NUnit-Gui This is a desktop application tool. This tool is very useful for executing the test cases and then also it provides the status of executed test cases. That how many test cases passed, failed and also give the proper exception.
The input for this tool is the dll or exe of code where the test cases return.
Then this tool extracts the test cases and shows it in the left panel and we can run all or individual test case from this tool. Also this tool provides the test result in xml format so that we can create HTML using this xml and create test report.
Here's the screenshot:
Also the following is the XML data:
- <?xml version="1.0" encoding="utf-8" standalone="no" ?>
- -
- <test-results name="C:\Users\aprabhu\Documents\Visual Studio 2015\Projects\seleniumDemo\NunitSelenium\bin\Debug\NunitSelenium.dll" total="2" errors="0" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2015-10-06" time="16:37:01">
- <environment nunit-version="2.6.4.14350" clr-version="2.0.50727.5420" os-version="Microsoft Windows NT 6.1.7601 Service Pack 1" platform="Win32NT" cwd="C:\Program Files (x86)\NUnit 2.6.4\bin" machine-name="AMITP" user="aprabhu" user-domain="CLARICE" />
- <culture-info current-culture="en-IN" current-uiculture="en-US" /> -
- <test-suite type="Assembly" name="C:\Users\aprabhu\Documents\Visual Studio 2015\Projects\seleniumDemo\NunitSelenium\bin\Debug\NunitSelenium.dll" executed="True" result="Success" success="True" time="33.767" asserts="0">
- -
- <results>
- -
- <test-suite type="Namespace" name="NunitSelenium" executed="True" result="Success" success="True" time="33.756" asserts="0">
- -
- <results>
- -
- <test-suite type="TestFixture" name="TestSeleniumClass" executed="True" result="Success" success="True" time="33.754" asserts="0">
- -
- <results>
- <test-case name="NunitSelenium.TestSeleniumClass.TestChromeDriver" executed="True" result="Success" success="True" time="17.253" asserts="0" />
- <test-case name="NunitSelenium.TestSeleniumClass.TestFirefoxDriver" executed="True" result="Success" success="True" time="16.446" asserts="0" />
- </results>
- </test-suite>
- </results>
- </test-suite>
- </results>
- </test-suite>
- </test-results>
Here's the output