Selenium With Java and Python For Mobile Apps & Web Apps......!

Wednesday, 31 December 2014

Usage of Single ‘/’ and double ‘//’ in the xpath And Absolute Xpath & Relative Xpath

single slash at the start of Xpath instructs XPath engine to look for element starting from root node.
double slash at the start of Xpath instructs XPath engine to search look for matching elementanywhere in the XML document.
Absolute XPath: The easiest way of finding the xpath is to use the Browser Inspector tool to locate an element and  get the xpath of it:
XPath Generated by the tool is : /html/body/div[2]/div/div/footer/section[3]/div/ul/li[3]/a
Relative XPath: At times XPath generated by Firebug are too lengthy and you see there is a possibility of getting a shorter XPath. Above xpath will technically work, but each of those nested relationships will need to be present 100% of the time, or the locator will not function.  Above choosed xpath is known as Absolute xpath. There is a good chance that your xpath will vary in every release. It is always better to choose Relative xpath, as it helps us to reduce the chance of element not found exception.

How To Get Xpath In Different-2 Browser?
Mozilla FireFox: Used FireBug and FirePath plugins for Mozilla link:
http://getfirebug.com/
https://addons.mozilla.org/en-US/firefox/addon/firepath/
Chrome:Here is the steps to do so:

  1. Navigate to the page I want to verify
  2. Press F12 to bring up Chrome debugger
  3. Press ctrl + f to bring up find
  4. Type or paste in the xpath expression that I want to test
  5. See if the xpath found an element.                                                                                                           OR
  1. Open Developer Tools
  2. Select Console tab.
  3. Use $x token. For example, $x("/html/body") will select the body tag.                                                                                                                                                                                                            Internet Explorerhe tool is the Internet Explorer developer bar. What it does show you is the tree (xpath) for the element you click on when you turn the “select element by click” option.                         

Selenium Locators ..............

Selenium web driver 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.
1. id
2. Name
3. Linktext
4. Partial Linktext
5. Tag Name
6. class name
7. Css
8. xpath
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="abcid">.....</div>
Example 2:
<input id="email" class="required" type="text"/>
We can write the scripts as
WebElement Ele = driver.findElement(By.id("abcid"));
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="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("value of Tag")));
select.selectByVisibleText("November");
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(“value of name”));
CSS Selector:
CSS mainly used to provide style rules for the web pageIs 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.
We can you use css which can also 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 Checkments = driver.findElements(By.cssSelector("input[id="value of id"']"));
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 mantion 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
We will take and sample XML document and we will explain different methods available to locate an element using Xpath

If a simple XPath is not able to find a complicated web element for our test script, we need to use the functions from XPath 1.0 library. With the combination of these functions, we can create more specific XPath. Let's discuss a 3 such functions –
  1. text():It take complete String with space Ex. //*['text()="abc "']
  2. Normalize-space(): It remove before and after string space not between but not work with part of string.
  3. Contains():It can used remove space and take part of string Ex. //*[contains(.,"hello")]
  4. Sibling: Used in case of dynamic Xpath . Ex. //div/tr[contains(,"hello")]/following-sibling::span //*[contains(text(),"hello")]/preceding-sibling::span
  5. Ancestor

Selenium Web Driver Setup With Java.........

To setup selenium webdriver, you should have following…
  1. Selenium webdriver Bindings ( in our case we will required Java bindings)
  2. JRE/JDK – java Runtime/Development environment,
  3. IDE – we will use Eclipse
Following steps will guide you setup selenium webdriver.
1. download all required software and bindings
 Selenium Webdriver bindings : Link : http://docs.seleniumhq.org/download/  : select the latest binding for java in “Selenium Client & WebDriver Language Bindings” section. and extract the zip file
Lesson 2 - selenium webdriver install

Download  and install JDK from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Download the Eclipse from https://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigosr2
you don’t need to install Eclipse as it is dump, you just need to run the executable.
2. Setup the Java in Environmental variable :  follow the steps mentioned below to setup the JRE in environmental variable ( for Win 7).
  1. Click on Start button
  2. Right click on My Computer > select properties
  3. Select Advance System Properties, it will popup ” System properties”
  4. Click on “Environmental Variables…” button. it will pop up, dialog
  5. in “System variables” section, select “path” and click on “Edit” button. if Path variable is not defined then create new variable.
  6. Now in path, you have enter the JRE path. typically the path is “C:\Program Files (x86)\Java\jre7\bin”
  7. Save and Exit.
3. Configuring the Selenium Webdriver: now you have setup the JRE, next step to configure the Selenium Webdriver.
follow the steps:
  1. Run Eclipse
  2. select or put the workspace path ( it is the path where you want to create projects)
  3. Create JAVA project in Eclipse
  4. Go to File menu > Properties, it will launch properties dialog.
  5. Select the “Libraries” tab
  6. Click on “Add external Jar” button
  7. Now select the Selenium webdriver Jar files which you have extracted.
  8. Click on “OK” button
  9. you are done!!!
now you have configured the Selenium webdriver !

What is Selenium Webdriver Architecture?

Before starting the automation using any automation tool, it is very important to know how that tool works and how it is architecture. This will helps to take the good advantage of the tool at the same time it will helps to make right automation framework.
In my further posts I will start explain how to use selenium and how to create selenium framework in details but before that let’s get an overview of Selenium webdriver architecture. Selenium can be a little bit confusing. As a beginner you will find how simply you can record and play the selenium scripts but it is not straight forward to how it’s doing that. At first glance it might appear that Selenium is actually driving the browser directly from your code but there’s actually a little bit more going on here and it’s going to help us understand how we can remote execute our test by looking at this basic architecture. So here’s a picture of the architecture for Selenium WebDriver which is the current version of Selenium which I going to explain here.
selenium webdriver architecture
Selenium webdriver architecture mainly divided into three parts
  1. Language level bindings
  2. Selenium Webdriver API
  3. Drivers
1) Language Level Bindings :
You can see at the Left hand side here we’ve got some bindings and these are language level bindings and with which you can implement the Selenium webdriver code. In simple words these the languages in which are making an framework, will interact with the Selenium Webdriver and work on various browsers and other devices. So we have a common API that we use for Selenium that has a common set of commands and we have various bindings for the different languages. So you can see there’s Java, Java, Python, Ruby, there’s also some other bindings and new bindings can be added very easily.
2) Selenium Webdriver API:
Now these bindings communicate with Selenium Webdriver API and and This API send the commands taken from language level bindings interpret it and sent it to Respective driver. Right now don’t worry about how it works. I will explain them in upcoming posts. In basic term it contains set of common library which allow to send command to respective drivers.
3) Drivers:
Drivers here at the right hand side, you see we have various internet browser specific drivers such as IE driver, a Firefox, Chrome, and other drivers such as HTML unit which is an interesting one. It works in headless mode which make text execution faster. It also contains mobile specific drivers as well. But the basic idea here is that each one of these drivers knows how to drive the browser that it corresponds to. So the Chrome driver knows how to handle the low level details of Chome browser and drive it to do things like clicking button, going into pages, getting data from the browser itself, the same thing for Firefox, IE, and so on.

How all blocks work together?
So what’s happening here is you’re going to write your test in let’s say in Java and you’re going to be using common Selenium API and that Java binding is going to be sending command across this common WebDriver API. Now on the other end is going to be listening a driver, It’s going to interpret those commands and it’s going to execute them on the actual browser and then it’s going to return the result backup using the WebDriver API to your code where you can look at that result.
selenium webdriver
Let’s take more closure look that how exactly that works
Let say you have written test using java (binding code) against Selenium API and that binding code is going to issue commands across WebDriver wire protocol this is a rest-based web service that is able to interpret those commands. The driver server is just a little executable that runs each one of the drivers has this driver server that basically listens on a port on your local machine when you run your tests and it’s waiting for these commands to come in. And when these commands come in it interprets those commands and then automates the browser and then returns those results back.
I hope this will give some clear idea about how Selenium Webriver being architect.

Translate

Popular Posts

Total Pageviews