Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Sunday 9 April 2017

Why & How WebDriver (Selenium) coding?

The limitations of Selenium IDE are:

1) Selenium IDE uses only HTML language.
2) Conditional or branching statements execution like using of if, select statements is not possible.
3) Looping statements using is not possible directly in Selenium HTML language in ide.
4) Reading from external files like .txt, .xls is not possible.
5) Reading from the external databases is not possible with ide.
6) Exceptional handling is not there.
7) A neat formatted Reporting is not possible with IDE.


Selenium IDE is very useful in learning stage of Selenium as it has feature of record actions. We can export recorded test case to any of these formats.
























Sunday 27 December 2015

Locators In Selenium

 Locators :

Selenium webdriver uses 8 locators to find the elements on web page. The following are the list of object identifier or locators supported by selenium.

We have prioritized the list of locators to be used when scripting.

id - >

Select element with the specified @id attribute.
Name

Select first element with the specified @name attribute.
Linktext

Select link (anchor tag) element which contains text matching the specified link text
Partial Linktext

Select link (anchor tag) element which contains text matching the specified partial link text
Tag Name

Locate Element using a Tag Name .
Class name

Locate Element using a Tag Name ..
Css

Select the element using css selectors. You can check here for Css examples and You can also refer W3C CSS Locatros
Xpath

Locate an element using an XPath expression.

Locating an Element By ID:
The most efficient way and preferred way to locate an element on a web page is By ID. ID will be the unique on web page which can be easily identified.
IDs are the safest and fastest locator option and should always be the first choice even when there are multiple choices, It is like an Employee Number or Account which will be unique.
Example 1:

<div id="toolbar">.....</div>

Example 2:

<input id="email" class="required" type="text"/>

We can write the scripts as

WebElement Ele = driver.findElement(By.id("toolbar"));

Unfortunately there are many cases where an element does not have a unique id (or the ids are dynamically generated and unpredictable like GWT). In these cases we need to choose an alternative locator strategy, however if possible we should ask development team of the web application to add few ids to a page specifically for (any) automation testing.

Locating an Element By Name:
When there is no Id to use, the next worth seeing if the desired element has a name attribute. But make sure there the name cannot be unique all the times. If there are multiple names, Selenium will always perform action on the first matching element
Example:

<input name="register" class="required" type="text"/>
WebElement register= driver.findElement(By.name("register"));


Locating an Element By LinkText:
Finding an element with link text is very simple. But make sure, there is only one unique link on the web page. If there are multiple links with the same link text (such as repeated header and footer menu links), in such cases Selenium will perform action on the first matching element with link.

Example:

<a href="http://www.seleniumhq.org">Downloads</a>
WebElement download = driver.findElement(By.linkText("Downloads"));


Locating an Element By Partial LinkText:
In the same way as LinkText, PartialLinkText also works in the same pattern.

User can provide partial link text to locate the element.
Example:

<a href="seleniumhq.org">Download selenium server</a>
WebElement download = driver.findElement(By.PartialLinkText("Download"));


Locating an Element By TagName:
TagName can be used with Group elements like , Select and check-boxes / dropdowns.
below is the example code:

Select select = new Select(driver.findElement(By.tagName("select")));
select.selectByVisibleText("Nov");
or
select.selectByValue("11");

Locating an Element By Class Name:
There may be multiple elements with the same name, if we just use findElementByClassName,m make sure it is only one. If not the you need to extend using the classname and its sub elements.
Example:

WebElement classtest =driver.findElement(By.className(“sample”));

CSS Selector:
CSS mainly used to provide style rules for the web pages and we can use for identifying one or more elements in the web page using css.
If you start using css selectors to identify elements, you will love the speed when compared with XPath. Check this for more details on Css selectors examples

We can you use Css Selectors to make sure scripts run with the same speed in IE browser. CSS selector is always the best possible way to locate complex elements in the page.

Example:

WebElement CheckElements = driver.findElements(By.cssSelector("input[id=email']"));

XPath Selector:
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

1. Native 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.

2. 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.
Example:
//table/tr/td

Example Syntax to work with Image

    xpath=//img[@alt='image alt text goes here']

Example syntax to work with table

    xpath=//table[@id='table1']//tr[4]/td[2]
    xpath=(//table[@class='nice'])//th[text()='headertext']/

Example syntax to work with anchor tag

    xpath=//a[contains(@href,'href goes here')]
    xpath=//a[contains(@href,'#id1')]/@class

Example syntax to work with input tags

    xpath=//input[@name='name2' and @value='yes']

We will take and sample XML document and we will explain different methods available to locate an element using Xpath

Tuesday 15 December 2015

Introducing WebDriver

Selenium

Selenium runs in many browsers and operating systems
can be controlled by many programming languages and testing frameworks.

What is Selenium?

Selenium automates browsers. That's it. What you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should) also be automated as well.

Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.
If you want to
  •   Create robust, browser-based regression automation
  •    Scale and distribute scripts across many environments

Then you want to use Selenium WebDriver; a collection of language specific bindings to drive a browser -- the way it is meant to be driven.
Selenium WebDriver is the successor of Selenium Remote Control which has been officially deprecated.

Introducing WebDriver

The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to providing a simpler, more concise programming interface along with addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.

How Does WebDriver ‘Drive’ the Browser Compared to Selenium-RC?

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using. Information on each ‘browser driver’ is provided later in this chapter.
For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ JavaScript functions into the browser when the browser was loaded and then used its JavaScript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation.


Versions of Selenium

Different versions of Selenium

Selenium IDE
Firefox add-on Selenium IDE allows users to record and re-play user actions in Firefox. It supports exporting the recorded scripts into Selenium RC or Selenium WebDriver code.

Selenium 1 / Selenium RC
Also known as Selenium 1 incorrectly, Selenium Remote Control is the first version of Selenium API, which was generally known as "Selenium" without any version suffixes at the time. After the release of second generation of Selenium, it started to be called with version number or name in order to be distinguishable from the new API. It is now officially deprecated but still released within Selenium WebDriver library for backward compatibility purpose.

Selenium 2 / Selenium WebDriver
Selenium 2, a.k.a. Selenium WebDriver, is the latest API in Selenium project, which replaces Selenium RC with fundamentally different mechanisms and dominates web UI automation market right now.

Selenium 3
The next release of Selenium project, which is only in staging at the time of writing. One possible major change would be breaking the backward compatibility, i.e. Selenium RC will be no longer a part of Selenium release. More details can be followed and discussed in this post on Selenium Developers' forum.

WebDriver
The term "WebDriver" might have different meanings in various contexts.
Synonym for Selenium WebDriver / Selenium 2.
A tool called "WebDriver" which was created independently then got merged into Selenium.

Comparison of Selenium versions

Version
Version
Comparison
Selenium 1
Selenium RC
Essentially the same thing.
Selenium 1 has never been an official name, but is commonly used in order to distinguish between versions.
Selenium 2
Selenium WebDriver
Essentially the same thing.
The term "Selenium WebDriver" is now more commonly used.
Selenium RC
Selenium WebDriver
Selenium RC is the predecessor of Selenium WebDriver.
It has been deprecated and now released inside Selenium WebDriver for backward compatibility.
Selenium IDE
Selenium RC/WebDriver
Selenium IDE is a recording tool for automating Firefox, with the ability to generate simple RC/WebDriver code.
Selenium RC/WebDriver are frameworks to automate browsers diagrammatically.
Selenium Grid
Selenium WebDriver
Selenium Grid is a tool to execute Selenium tests in parallel on different machines.
Selenium WebDriver is the core library to drive web browsers on a single machine.

Selenium Relationships

Selenium RC Server / Selenium Server
Selenium RC Server was the Java-based package to run Selenium RC tests. With the release of Selenium WebDriver, Selenium (Standalone) Server was introduced as the super-set of the previous version, so that tests can be executed remotely in Selenium Grid mode. For Selenium WebDriver tests that are running locally, Selenium Server is not required.