Driver Initilization
WebDriver driver = new ChromeDriver();
WebDriver driver = new FirefoxDriver();
WebDriver driver = new InternetExplorerDriver();
WebDriver driver = new HtmlUnitDriver();
Element Locators
[Link]([Link]("Id Value"));
[Link]([Link]("Name Value"));
[Link]([Link]("Class Name Value"));
[Link]([Link]("Link text Value"));
[Link]([Link]("Partial Text Constant
Value"));
[Link]([Link]("Tag Name Value"));
[Link]([Link]("CSS Value"));
[Link]([Link]("Xpath Value"));
[Link](new ByAll([Link]("ElementClass
Name"), [Link]("Element Id"), [Link]("Element Name")))
Elements Operations
WebElement element =
[Link]([Link]("Value of Element
Locator"));
[Link]();
[Link]("Input Text");
[Link]();
[Link]();
[Link](“type”);
String innerText = [Link]();
boolean enabledstatus = [Link]();
boolean displayedstatus = [Link]();
boolean selectedstatus = [Link]();
//Operation on drop down
Select select = new Select(element);
[Link](Integer Index);
[Link]("Text");
[Link]("Value");
[Link]();
[Link](Integer Index);
[Link]("Text");
[Link]("Value");
WebElement selectedOptions = [Link]();
Browser Operations
String pageTitle = [Link]();
String currentURL = getCurrentUrl();
String currentPageSource = [Link]();
// Navigation history
[Link]("[Link]
[Link]().window().maximize();
[Link]().to("[Link]
[Link]().back();
[Link]().forward();
[Link]().refresh();
[Link]();
[Link]();
// Handle Alert
Alert alert = [Link]().alert();
[Link]();
[Link]();
[Link]();
[Link](“Input Data");
//Handle Cookies
Cookie cookie = new Cookie(“cookieName”, “cookieValue”);
[Link]().addCookie(cookie);
[Link]().getCookies();
[Link]().getCookieNamed(arg0);
[Link]().deleteAllCookies();
[Link]().deleteCookieNamed(arg0);
// Handle frames
[Link]().frame(int Frame Index);
[Link]().frame("frameName");
WebElement element =
[Link]([Link]("Value of Element
Locator"));
[Link]().frame(element);
[Link]().defaultContent();
Screenshots Capture
TakesScreenshot screenshot =((TakesScreenshot)driver);
File srcFile= [Link]([Link]);
[Link](srcFile, destFile);
Manage Timeouts
[Link]().timeouts().implicitlyWait(10,
[Link]);
welement = [Link](Syntax: WebDriverWait wait = new
WebDriverWait(driver, timeout);
[Link](locator));
[Link]();
[Link](Long milli-seconds)
[Link]().timeouts().pageLoadTimeout(30,
[Link]);
Scroll Down or Up Web Page
JavascriptExecutor js = (JavascriptExecutor)driver;
[Link]("[Link](0,100)");
[Link]("[Link](0,
[Link])");
WebElement element =
[Link]([Link]("Value of Element
Locator"));
js. executeScript("arguments[0].scrollIntoView()", element);
Different approach to create XPath and CSS Selector
Description XPath CSS Selector
Whole WebPage /html html
Whole WebPage body /html/body body
image element //img img
Link //a[@href = 'url'] a[href = 'url']
Direct Child //div/a div > a
Id //tagName[@id=’idValue’] tagName#idValue
Class //tagName[@class=’classValu [Link] of Class
e’] attribute
Attribute //tagname[@attribute- tagName[attribute=Valu
name=’value1′] e of attribute]
Multiple Attributes //input[@type='submit' and tagname[attribute1='val
@name='btnLogin'] ue1'][attribute2='value2'
]
Contains //*[contains(@type,'sub')] <HTML
tag><[attribute*=sub
String]>
Starts with //tagname[starts- <HTML
with(@attribute, ‘Start tag><[attribute^=prefix
value’)] of the String]>
Ends with //tagname[ends- <HTML
with(@attribute, ‘End value’)] tag><[attribute$=suffix
of the String]>
Matches //*[matches(@type,'value')] N/A
First Child //ul[@id=’list’]/li[1] ul#list li:first-child
Last Child //ul[@id=’list’]/li[last()] ul#list li:last-child
nth Child //ul[@id=’list’]/li[3] ul#list li:nth-child(3)
Text Value //td[text()=‘textname'] N/A
Element preceding some sibling //E2/preceding-sibling::E1 N/A
Sibling element immediately preceding //E/preceding-sibling::*[1] N/A
User interface element that is disabled //E[@disabled] E:disabled
Checkbox (or radio button) that is checked //*[@checked] *:checked
Text Value //td[text()=‘textname'] N/A
Important Tagname and their Description
Tag Name Specification
<ul> Defines an unordered list of
items.
<tr> Defines a row of cells in a
table.
<title> Represents title to an HTML
document.
<th> Used for creates header of a
group of cell in HTML table.
<table> Used to defines a table in an
HTML document.
<tbody> Used for grouping table rows.
<td> Used for creates standard
data cell in HTML table.
<span> Used to grouping and
applying styles to inline
elements.
<section> Used to divide a document
into number of different
generic section.
<select> Used to create a drop-down
list.
<menu> Used to display a unordered
list of items/menu of
commands.
<li> Define a list item either
ordered list or unordered list.
<iframe> Defines a inline frame that
embedded external content
into current web document.
<img> Used to insert image into a
web document.
<input> Define a get information in
selected input
<div> Define a division part
<body> Defines a main section(body)
part in HTML document
<br /> Specific a single line break
<button> Specifies a press/push button
<a> Specific a anchor (Hyperlink)