There are many situation when we get difficulties to find the elements which do not have identification/attribute but their immediate siblings has some identification.
In above example if we want to get the text of H1 we can not use //h1 because there are more than one h1 on the page. If you see a and h1 both are siblings and children of div//[@class='title pad_btm15']
By using a sibling element's property we can find out his sibling. In above example we know id of a which is "mainContent" then we can get its sibling
We choose the method based on which gives the element in fastest way in all the browsers. Finding elements by ID is usually going to be the fastest option, because at its root, it eventually calls down to document.getElementById(), which is optimized by many browsers.
Finding elements by XPath is useful for finding elements using very complex selectors, and is the most flexible selection strategy, but it has the potential to be very slow, particularly in IE. In IE 6, 7, or 8, finding by XPath can be an order of magnitude slower than doing the same in Firefox. IE provides no native XPath-over-HTML solution, so the project must use a JavaScript XPath implementation, and the JavaScript engine in legacy versions of IE really is that much slower.
If you have a need to find an element using a complex selector, I usually recommend using CSS Selectors, if possible. It's not quite as flexible as XPath, but will cover many of the same cases, without exhibiting the extreme performance penalty on IE that XPath can.
/div
it will define an absolute path to node "a" relative to the root. In this case, it will only find "a" nodes at the root of the XML tree.
//
When // is used at the beginning of a path:
//div
It will define a path to node "div" anywhere within the XML document. In this case, it will find "div" nodes located at any depth within the XML tree.
These XPath expressions can also be used in the middle of an XPath value to define ancestor-descendant relationships. When / is used in the middle of a path:
/div/a
Tt will define a path to node "a" that is a direct descendant (ie. a child) of node "div".
When // used in the middle of a path:
/div//a
It will define a path to node "a" that is any descendant (child node) of node "div".
When we don't know the immediate parent of a child then we can use "." dot
will still find any node, "div", located anywhere within the XML tree. But, the XPath:
.//div
will find any node, "div", that is a descendant of the node . " In other words, preceding the "//" expression with a "." tells the XML search engine to execute the search relative to the current node reference.
It is easy and small API.
These API’s are less Object oriented
As compared to RC, it
is bit complex and large API. API’s are entirely Object oriented
Selenium RC is slower
since it uses a JavaScript program called Selenium Core. This Selenium
Core is the one that directly controls the browser, not you.
Web Driver is faster
than Selenium RC since it speaks directly to the browser uses the
browser’s own engine to control it.
Selenium Core, just
like other JavaScript codes, can access disabled elements.
Web Driver interacts
with page elements in a more realistic way. It interacts natively with
browser application
Required to start
server before executing the test script.
Doesn’t required to
start server before executing the test script.
It is standalone java
program which allow you to run Html test suites.
It actual core API
which has binding in a range of languages.
Selenium RC cannot
support the headless HtmlUnit browser. It needs a real, visible browser
to operate on.
Web Driver can support
the headless HtmlUnit browser.
It doesn’t supports of
moving mouse cursors.
It supports of moving
mouse cursors.
Selenium RC Has
Built-In Test Result Generator. Selenium RC automatically generates an HTML
file of test results.
Web Driver has no
built-in command that automatically generates a Test Results File.
It does not supports
listeners
It supports the
implementation of listeners
Selenium RC needs the
help of the RC Server in order to do so.
web Driver directly
talks to the browser
It does not support to
test iphone/Android applications
This post will take you through the way you can select the value in a drop down. Webdriver does not have straight forward command "SELECT" which Selenium does.
In below example i will open home page of eBay India and search ipod in Consumer Electronic category. This page has a category drop down in which i will select
Consumer Electronics.
package com.web;
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DropDownSelection {
private static WebDriver driver=null;
@BeforeTest
@org.testng.annotations.Parameters({"BROWSER"})
public void setup(String BROWSER){
if(BROWSER.equals("FF")){
File profileDir = new File("C:\\nir");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.setPreference("general.useragent.override", "same user agent string as above");
driver= new FirefoxDriver(profile);
System.out.println("Broser Selected " + BROWSER);
}else if(BROWSER.equals("IE")){
System.out.println("Broser Selected " + BROWSER);
driver = new InternetExplorerDriver();
}else if(BROWSER.equals("HU")){
System.out.println("Broser Selected " + BROWSER);
driver =new HtmlUnitDriver();
} else
{
System.out.println("Broser Selected " + BROWSER);
driver = new ChromeDriver();
}
}
@Test
public void testDropDownSelection() throws InterruptedException{
driver.get("http://www.ebay.in");
WebElement search = driver.findElement(By.name("_nkw"));
search.sendKeys("ipod");
List dd = driver.findElement(By.name("_sacat")).findElements(By.tagName("option"));
for (WebElement option : dd) {
System.out.println(option.getText());
if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
option.click();
break;
}
}
driver.findElement(By.xpath("//input[@value='Search']")).submit();
Thread.sleep(5000);
driver.getPageSource().contains("results found");
driver.quit();
}
}
TestNG.xml
Using TestNG suite we will run the test case.
we can run test script in different browsers parallely using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox and chrome parallely.
package com.web;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParallelRunning {
private WebDriver driver=null;
@BeforeTest
@Parameters ({"BROWSER"})
public void setup(String BROWSER){
System.out.println("Browser: " + BROWSER);
if (BROWSER.equals("FF")) {
System.out.println("FF is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("IE is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("HU is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")){
System.out.println("Google chrome is selected");
driver = new ChromeDriver();
}
}
@Test
public void testParallel()throws Exception{
driver.get("http://www.google.com");
Thread.sleep(5000);
WebElement search = driver.findElement(By.name("q"));
search.sendKeys("automation blog by niraj");
search.submit();
Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"));
driver.findElement(By.xpath("//a[contains(@href,'automationtricks.blogspot.com')]")).click();
Thread.sleep(15000);
Assert.assertTrue(driver.getPageSource().contains("working as Associate Manager @CSC"));
driver.quit();
}
}
In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.
TestNG.xml
we can run test script in different browsers using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox chrome.
package com.web;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class WebDriverDemo {
private static WebDriver driver = null;
@Test
@Parameters( {"BROWSER"})
public void testread(String BROWSER)throws Exception{
System.out.println("Browser: " + BROWSER);
if (BROWSER.equals("FF")) {
System.out.println("FF is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("IE is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("HU is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")){
System.out.println("Google chrome is selected");
driver = new ChromeDriver();
}
driver.navigate().to("http://www.yahoo.com");
Thread.sleep(10000);
WebElement search= driver.findElement(By.name("p"));
search.sendKeys("automation blog by niraj");
search.submit();
Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"),"Failed in "+ BROWSER);
driver.close();
}
}
In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.
TestNG.xml