Sunday, 5 July 2015

SE - 2 - findElement() and findElements() - difference !!

- findElement() is used to find/locate single targeted element on webpage while findElements is used to locate a list of elements.

- if targeted element is not found on webpage findElement() will return NoSuchElement exception while findElements() will not throw any exception but will show empty list.

- generally the results of findElements() are stored in List while result of findElement can be stored in any webelement variable.

-if there are multiple elements and we use findElement() it will just return the first occurrence of the elements.

-findelements() returns List of webelements from a webpage

SE - 1 - Locators in Selenium !

Locators are used in selenium to locate Web elements on the web page.If we say web elements it refers to the the elements on the web page that is WebButtons, EditBox , CheckBox, etc. Similar to QTP  , we need to locate elements on webpage through Selenium too. After locating that element only we can perform any operation on them.

Selenium has provided different kind of locators :

1) Locating by ID : Id is considered to be the most unique thing in locating an element. Id will always be unique on the webpage which can be easily identified.W3C also approves that ID should be unique on a HTML page.

WebDriver WB = new FirefoxDriver();
WB.findElement(By.id("element ID")

2) Locating by Name : The next preferred locator after ID is Name. Although we can't have Unique name all the times. There may be repetition of names, in that case Selenium will use the first occurrence of it.
WebDriver WB = new FirefoxDriver();
WB.findElement(By.name("element Name")

3) Locating a element by Link Text : This can be used is there is unique link text on the webpage. This locator will work correctly if a web page has single text link.
WebDriver WB = new FirefoxDriver();
WB.findElement(By.linkText("element Linktext")

4) Locating by Partial Link : Similar to Link text partial link text also works.
WebDriver WB = new FirefoxDriver();
WB.findElement(By.PartialLinkText("element Partial Link Text")

5) Locating by TagName : TagName can be used with Group Elements like select , checkboxes , dropdoewns , etc.
WebDriver WB = new FirefoxDriver();
WB.findElement(By.TagName ("element Tag Name")

6) Locating Element by Classname : We can also use classname to uniquly identify the element but it should be unique.
WebDriver WB = new FirefoxDriver();
WB.findElement(By.ClassName("element Class Name")

7) Locating by CSS Selector : CSS is used to uniquely identify element having complex path. If we can't identify the element from any of the given methods we can use CSS. Compared o Xpath , CSS is much faster.
WebDriver WB = new FirefoxDriver();
WB.findElement(By.CssSelector ("CSS path")

8) Locating by Xpath : XPath is designed to allow the navigation of XML documents, with the purpose of selecting individual elements, attributes, or some other part of an XML document for specific processing.
There are two types of xpath :

-> Native(absolute) Xpath, it is like directing the xpath to go in direct way. like
Example:
html/head/body/table/tr/td

Here the advantage of specifying native path is, finding an element is very easy as we are mention the direct path. But if there is any change in the path (if some thing has been added/removed) then that xpath will break.

-> Relative Xpath.
In relative xpath we will provide the relative path, it is like we will tell the xpath to find an element by telling the path in between.
Advantage here is, if at all there is any change in the html that works fine, until unless that particular path has changed. Finding address will be quite difficult as it need to check each and every node to find that path.

WebDriver WB = new FirefoxDriver();
WB.findElement(By.Xpath("Xpath")

Xpath has also different variants which we will discuss seperately.