You are on page 1of 18

Selenium Automation Topics

 Day1
– Introduction to Selenium IWebDriver
– Instantiating Selenium IWebDriver
– IWebDriver methods
– Launch the application
– Control the browser
– Work with browser's Object
– Locating Object with Xpath
– Locating Object with cssSelector

 Day2
– IWebDriver Wait Time
 Implicit Wait
 Explicit Wait
 Fluent Wait
– Action Class
– Handling Ajax Call using WebDriver
– Dynamic WebTable
– Taking Screen shot
– Nunit
 Annotation
 Priorities
Day 3:
 C# Introducation
 C# program window
 C# Terms & Folders
 OOPs (Object Oriented Programming System)
 Object and Class
 Constructor & Desctructor
 Some useful Keywords:
 Access Modifiers
Day 1

Selenium Introduction

 Selenium is an open source tool (no cost involved – free to


download from Seleniumhq)
“https://docs.seleniumhq.org/download/”
 Selenium supports language like HTML, Java, PHP, purl,
Python, Ruby and C# .
 Selenium supports multiple web browsers like, IE, Firefox,
Chrome, Opera
 Selenium supports multiple Operating systems like Windows,
Linux, Mac
 Selenium Components
Selenium IDE

 Firefox extension
https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
 Easy record and replay
 Debug and set breakpoints
 Save tests in HTML, WebDriver
and other formats.
 Selenium saves all information
in an HTML table format
 Each record consists of:
Command – tells Selenium what to do (e.g. “open”, “type”,
“click”, “verifyText”)
Target – tells Selenium which HTML element a command refers
to (e.g. textbox, header, table)
Value – used for any command that might need a value of some
kind (e.g. type something into a textbox)

Selenium IDE Commands and Assertion :

Command Number of Description


Parameters

open 0-2 Opens a page


using a URL.

click/clickAndWait 1 Clicks on a
specified
element.

type/typeKeys 2 Types a sequence


of characters.

verifyTitle/assertTitle 1 Compares the


actual page title
with an expected
value.

verifyTextPresent 1 Checks if a
certain text is
found within the
page.
verifyElementPresent 1 Checks the
presence of a
certain element.

verifyTable 2 Compares the


contents of a
table with
expected values.

waitForPageToLoad 1 Pauses execution


until the page is
loaded
completely.

waitForElementPresent 1 Pauses execution


until the specified
element becomes
present.

There are 3 Types of Assertions


 Assert. When an "assert" command fails, the test is stopped
immediately.
 Verify. When a "verify" command fails, Selenium IDE logs
this failure and continues with the test execution.
 WaitFor. Before proceeding to the next command, "waitFor"
commands will first wait for a certain condition to become
true
Selenium Webdriver
 It is an API that’s easy to explore and understand, which
help us to make our tests easier to read and maintain
 Supports dynamic web pages where element of a Page may
change without the Page itself being reloaded
 Web Driver is a compact Object Oriented API which can
directly interacts with the Application under tests.
 WebDriver utilizes the browser native compatibility to
automation without using any peripheral entity.
 Selenium-WebDriver supports multiple browsers in multiple
platforms
– Google Chrome 12.0.712.0+
– Internet Explorer 6+
– Firefox 3.0+
– Opera 11.5+
– Android – 2.3+ for phones and tablets
– iOS 3+ for phones
– iOS 3.2+ for tablets
Selenium WebDriver 3.0 Architecture Diagram

Instantiating Selenium WebDriver object in Java


Firefox Browser
IWebDriver driver = new FirefoxDriver(@"C:\Path for
geckodriver exe\");
Chrome Browser
IWebDriver driver = new ChromeDriver(@"C:\Path for chrome
exe\");
Internet Browser
IWebDriver driver = new InternetExplorerDriver(@"C:\Path for IE
exe\");
Not the Selenium executable for above drivers are available in
seleniumhq website mentioned in next slide. Download the
executable and unzip it.
Instantiating Selenium WebDriver object in Java

Firefox Browser
IWebDriver driver = new FirefoxDriver(@"C:\Path for
geckodriver exe\");
Chrome Browser
IWebDriver driver = new ChromeDriver(@"C:\Path for chrome
exe\");
Internet Browser
IWebDriver driver = new InternetExplorerDriver(@"C:\Path for IE
exe\");
Not the Selenium executable for above drivers are available in
seleniumhq website mentioned in next slide. Download the
executable and unzip it.
Downloading Browser executable
Download browse's executable from below link for Firefox,
Chrome, IE:
Link: http://www.seleniumhq.org/download/
WebDriver methods to -

Launching a Page e.g.


driver.Url = "http://www.google.co.in";
• Locating UI Elements (WebElements)
– FindElement(By arg)
– FineElements(By arg)
– Both methods of WebDriver object are passing By
object as parameter
– FindElement return single WebElement and
FindElements return list of WebElement
– Example
WebDriver driver = new FirefoxDriver();
WebElement field = driver.FindElement(By.Id(“abc”));
List<WebElement> fields =
driver.FindElements(By.TagName(“abc”));
How to locate an element using By class

By Id
driver.FindElement( By.Id("coolestWidgetEvah") );
By Name

WebDriver: driver.FindElement( By.Name("cheese") );

By.LinkText finds a link element by the exact text it displays


driver.FindElement(By.LinkText("REGISTRATION"))

By.ClassName finds elements based on the value of the "class"


attribute
driver.FindElement(By.ClassName("someClassName"))

By.TagName locates elements by their tag name


driver.FindElement(By.TagName("div"))

By.PartialLinkText locates elements that contain the given link


text
driver.FindElement(By.PartialLinkText("REG"))
How to locate an element using By class (cont...)

By Xpath

HTML Page :
<html>
<input type="text" name="example" />
<input type="text" name="other" />
</html>

WebDriver:
driver.FindElements( By.Xpath("//input") );

There are plug-ins for firefox/chrome to automatically display the


Xpath

Syntax for Xpath : Xpath=//tagname[@attribute='value']

By.CssSelector finds elements based on the driver's underlying


CSS Selector engine
driver.FindElement(By.CssSelector("input#email"))
Sample Application
(A sample web application)
http://newtours.demoaut.com/mercurywelcome.php

Selenium-WebDriver Test Script

• Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
namespace SeleniumTesting
{
[TestFixture]
class SeleniumIE
{
IWebDriver driver;
[SetUp]
public void StartBrowser()
{
driver = new
InternetExplorerDriver(@"C:\Users\Hunza\SeleniumCsharp\Seleni
umAutomation\");
}
[Test]
public void Test()
{
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().PageLoad =
TimeSpan.FromSeconds(25);
driver.Url = "http://www.google.co.in";
IWebElement field= driver.FindElement(By.Id("lst-ib"));
field.Clear();
field.SendKeys("Seleniumhq");
IWebElement fieldButton =
driver.FindElement(By.Name("btnK"));
fieldButton.Click();
}
[TearDown]
public void CloseBrowser()
{
driver.Close();
}
}
}

Selenium-WebDriver methods – control browser

 Close() - close the browser window which the driver has


focused
Syntax:
driver.Close();
 Quit() - close all the browser windows open by driver.
Syntax:
driver.Quit();
 Url() retrun type String
Syntax:
String strurl = driver.Url();
 Title() retrun type String
Syntax:
String strTitle =driver.Title();

 CurrentWindowHandle() return the web browser driver's


reference ID
Syntax:
String strDriverReference = driver.CurrentWindowHandle();
 WindowHandles() return the reference ID of all the
browser's window open by the driver.
Syntax:
List<String> strDriverReference = driver.WindowHandles();
 Manage(): There below supporting methos used by the
driver
 driver.Manage().Cookies.AddCookie()
 driver.Manage().Window.Maximize();
 driver.Manage().Timeouts().PageLoad =
TimeSpan.FromSeconds(25);
 driver.Manage().Window.FullScreen();
 Point point= driver.Manage().Window.Position;

• Navigate() is instance variable of Navigation interface


– driver.Navigate().Back();
– driver.Navigate().Forward();
– driver.Navigate().GoToUrl("");
SwitchTo() is instance variable of TargetLocator interface
– driver.SwitchTo().Window("windowRefrence");
– driver.SwitchTo().Alert().Accept();
– driver.SwitchTo().Alert().Dismiss();
– driver.SwitchTo().Alert().SendKeys("values");
– driver.SwitchTo().Frame(i);
– driver.SwitchTo().Frame(“frameName”);
– driver.SwitchTo().ParentFrame();
Selenium-WebDriver methods – work with browser's
Object

 Text () - to read html tag value.


driver.FindElement(By.Id("disp")).Text();
 SendKeys (String values) - input value in text box
driver.FindElement(By.id("inp1")).SendKeys("50000");
 Click() - click a button or webElement
driver.FindElement(By.TagName("button")).Click()
 GetAttribut(String attributname) – get the html tag's
attribute value

driver.FindElement(By.TagName("button")).GetAttribute("cla
ss")
 TagName() - get the html tag name (input/button/select)
driver.FindElement(By.Id("inp1")).TagName()
 Displayed()/ Enabled()/ Selected() - verify the
displayed/enabled/selected status of html tags

You might also like