FireFinder does exactly what you are looking for. You can evaluate either CSS, or XPath expressions, it will list the matching elements, and also draw a red border around them.
teach yourself straight forward concepts to be advance level Utility Creation
Thursday, 11 December 2014
Xpath How to create
Using Xpaths we can identify element location through nodes and attributes in XML document. There are basically 2 types of Xpaths:
1. Absolute Xpath: It starts from the root path.
For e.g. the absolute path for the element highlighted below in the screenshot is: /html/body/div[1]/header/nav/ul/li[1]/a
2. Relative Xpath: It can start from anywhere in the XML document.
For the same element highlighted above, we can write the relative xpath as:
//ul[@id=’jsddm’]/li/a
By looking at the examples above, we can observe that absolute xpath uses single slash (/) at the start which indicates that Xpath engine will look for element starting from root node. While relative xpath uses double slash (//) at the start which indicates that Xpath engine will look for element from anywhere in the XML.
Creating Xpaths using different attributes
We can create xpaths from any attribute using the syntax:
//TagName[@AttributeName=’AttributeValue’]
Referring the screenshot below, we can create the xpath for the same in different ways:
- //div[@id=’main’]
- //div[@class=’content’]
- //div[@data-jiis=’cc’]
Suppose we want to create xpath using element text, we can follow the syntax as:
//TagName[text()=’ElementText’]
Referring the screenshot below, we can create the xpath as:
//a[text()=’Mail’]
Match by Sub-string
We can use the following methods to handle dynamically generated values or if we want to locate elements with sub-strings:
- contains keyword:
The syntax for the same is:
- //TagName[contains(@AttributeName, ‘AttributeValue’)] : We can use this if we want to use attribute value. For e.g. //div[contains(@class, ‘proid’)]
- //TagName[contains(text(), ‘partialtext’)] : We can use this if we want to use partial text. For e.g. //div[contains(text(), ‘logica’)]
- starts-with keyword:
The syntax for the same is:
- //TagName[starts-with(@AttributeName, ‘AttributeValue’)] : We can use this if we want to use starting text of attribute value. For e.g. //div[starts-with(@class, ‘pro’)]
- //TagName[starts-with(text(), ‘startingtext’)] : We can use this if we want to use partial starting text. For e.g. //div[starts-with(text(), ‘log’)]
Creating Xpaths using multiple attributes
We can create a xpath using multiple attributes within that element. For e.g.
Here we can write the xpath as: //input[@id=’gbqfq’ and @autocomplete=’off’]
Using Xpaths to select parent node
Now refer the image below:
From the highlighted element path i.e. //div[@id=’hdtb_msb’], if we want to go to the parent node, then we can write the xpath expression as:
//div[@id=’hdtb_msb’]/..
This will select the parent node having the path: //div[@id=’hdtb_s’]
If we want to select the 2nd child node i.e. the ol element, we can write the xpath expression as:
//div[@id=’hdtb_msb’]/../ol
Wednesday, 10 December 2014
how to do dragAndDrop
We have Actions class in Selenium Webdriver
Actions class have the method dragAndDrop(source,target) for this purpose.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class DragNdrop {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("http://yuiblog.com/sandbox/yui/3.2.0pr1/examples/dd/groups-drag_clean.html");
Actions act = new Actions(driver);
WebElement source1 = driver.findElement(By.id("pt1"));
WebElement target1 = driver.findElement(By.id("t2"));
act.dragAndDrop(source1,target1).perform();
}
}
Actions class have the method dragAndDrop(source,target) for this purpose.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class DragNdrop {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("http://yuiblog.com/sandbox/yui/3.2.0pr1/examples/dd/groups-drag_clean.html");
Actions act = new Actions(driver);
WebElement source1 = driver.findElement(By.id("pt1"));
WebElement target1 = driver.findElement(By.id("t2"));
act.dragAndDrop(source1,target1).perform();
}
}
How to handle alert pop-up.
Pop up are mainly Javascript alerts.
We need to identify as an first step whether its a Java script alert or a new window.
Usually as an thumb rule,
1.Javascript Alert didn't not have any url.
2. It may have a message Text
3. Have only two buttons :: Accept (Ok) and dismiss(Cancel).
4. Firepath will not able to identify these OK and cancel Button.
First switch the control to JS alert then either accept or dismiss the alert according to your
requirement.
steps::
WebDriver driver = new FirefoxDriver();
Alert alt = driver.switchTo().alert(); //this will switch the control to alert pop-up
alt.accept(); // to click on OK or to accept the alert pop-up
//alt.dismiss(); // to click on CANCEL
We need to identify as an first step whether its a Java script alert or a new window.
Usually as an thumb rule,
1.Javascript Alert didn't not have any url.
2. It may have a message Text
3. Have only two buttons :: Accept (Ok) and dismiss(Cancel).
4. Firepath will not able to identify these OK and cancel Button.
First switch the control to JS alert then either accept or dismiss the alert according to your
requirement.
steps::
WebDriver driver = new FirefoxDriver();
Alert alt = driver.switchTo().alert(); //this will switch the control to alert pop-up
alt.accept(); // to click on OK or to accept the alert pop-up
//alt.dismiss(); // to click on CANCEL
Common scenarios where exceptions may occur
There are given some scenarios where unchecked exceptions can occur. They are as follows:
1) Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;//ArithmeticException
2) Scenario where NullPointerException occurs
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s=null;
System.out.println(s.length());//NullPointerException
3) Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.
String s="abc";
int i=Integer.parseInt(s);//NumberFormatException
4) Scenario where ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Difference between checked and unchecked exceptions
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
How to handle https sites in selenium ?
We need to create the Firefox Profile in order to handle this kind of https sites.
And it is possible only for Firefox. We can not create Profile for Chrome and IE.
And it is possible only for Firefox. We can not create Profile for Chrome and IE.
public class myHTTPSConnectionClass {
public static void main(String[] args){
FirefoxProfile profile = newFirefoxProfile();
profile.setAcceptUntrustedCertificates(false);
WebDriver driver = newFirefoxDriver(profile);
driver.get("https://172.64.26.16");
}
}
How to switch inside a Frame
we have the statement
driver.switchTo().frame();
for switching the driver control inside a frame in AUT.
The frame method is overloaded i.e. 3 varients are available .
We have to use one which best fits the scenario.
1. Using the Xpath/Css Selector
driver.switchTo().frame(iframe_element)
ex:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='signupsso']")));
2.Using the frame id or name
driver.switchTo().frame(name_or_id)
3.
driver.switchTo().frame(index)
This is the last option to choose, because using index is not stable enough as you could imagine. If this is your only iframe in the page, try driver.switchTo().frame(0)
example:
<iframe frameborder="0" style="border: 0px none; width: 100%; height: 356px; min-width: 0px; min-height: 0px; overflow: auto;" dojoattachpoint="frame" title="Fill Quote" src="https://tssstrpms501.corp.trelleborg.com:12001/teamworks/process.lsw?zWorkflowState=1&zTaskId=4581&zResetContext=true&coachDebugTrace=none">
---------------------above is the DOM-------------------
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='Fill Quote']")
Cookies Handling in Selenium
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Cookies {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
System.out.println("cookies before delete: "+driver.manage().getCookies());
driver.manage().deleteAllCookies(); //
System.out.println("cookies after delete: "+driver.manage().getCookies());
}
}
import org.openqa.selenium.firefox.FirefoxDriver;
public class Cookies {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
System.out.println("cookies before delete: "+driver.manage().getCookies());
driver.manage().deleteAllCookies(); //
System.out.println("cookies after delete: "+driver.manage().getCookies());
}
}
Difference Between == and equals method || Java
Since Strings are objects, the equals(Object) method will return true if two Strings have the same objects. The == operator will only be true if two String references point to the same underlying String object. Hence two Strings representing the same content will be equal when tested by the equals(Object) method, but will only by equal when tested with the == operator if they are actually the same object.
In other words ,
Example #1:
Integer i = new Integer(10);
Integer j = i;
in the above code. i == j is true because both i and j refer to the same object.
a
Example #2:
Integer i = new Integer(10);
Integer j = new Integer(10);
In the above code, i == j is false because, although they both have the value 10, they are two different objects.
However, to be safe, most of the time, programmers should use ".equals" rather than "==" to avoid unexpected outcome. It simply because ".equals" will compare 2 strings character by character to determine equality while "==" checks whether 2 string objects are identical. Try to do this experiment and you will understand:
String a = new String ("a");
String b = new String ("a");
System.out.println (a == b);
It returns false, while the following code returns true.
String a = new String ("a");
String b = new String ("a");
System.out.println (a.equals(b));
Using equals runs faster than performing (String.compareTo(String) == 0) because of the extra cycles needed to make the == 0 comparison.
two strings are equal in case of equals method if both strings that you are comparing have the same characters and have the same case such as "hello".equals("hello") will return true as both the strings have the same characters h,e,l,l,o and they do have the same length and also have the same case.
Now change the case of h to H i.e write "Hello".equals("hello") this will return false which shows that the strings are not equal.
understood.
Use ( ! "hello".equals(someString) )
This is just a reminder to Java programmers, also to me.
String a = new String ("a");
String b = new String ("a");
System.out.println (a == b);
It returns false, while the following code returns true.
String a = new String ("a");
String b = new String ("a");
System.out.println (a.equals(b));
Using equals runs faster than performing (String.compareTo(String) == 0) because of the extra cycles needed to make the == 0 comparison.
two strings are equal in case of equals method if both strings that you are comparing have the same characters and have the same case such as "hello".equals("hello") will return true as both the strings have the same characters h,e,l,l,o and they do have the same length and also have the same case.
Now change the case of h to H i.e write "Hello".equals("hello") this will return false which shows that the strings are not equal.
understood.
Use ( ! "hello".equals(someString) )
This is just a reminder to Java programmers, also to me.
In other words ,
Example #1:
Integer i = new Integer(10);
Integer j = i;
in the above code. i == j is true because both i and j refer to the same object.
a
Example #2:
Integer i = new Integer(10);
Integer j = new Integer(10);
In the above code, i == j is false because, although they both have the value 10, they are two different objects.
Tuesday, 9 December 2014
Difference :: HashMap vs HashTable
HashMap | HashTable | |
---|---|---|
Synchronized | No | Yes |
Thread-Safe | No | Yes |
Null Keys and Null values | One null key ,Any null values | Not permit null keys and values |
Iterator type | Fail fast iterator | Fail safe iterator |
Performance | Fast | Slow in comparison |
Superclass and Legacy | AbstractMap , No | Dictionary , Yes |
How to handle multiples windows || Using an Utility
/**
* This method will toggle the driver handle from main window to popup
* window.
*
* @param driver
*/
public static void handlePopupWindow(WebDriver driver)
{
(new WebDriverWait(driver, 90)).until(new ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver d)
{
return d.getWindowHandles().size() > 1;
}
});
for (String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
}
Call this Utility method, whenever need to switch to a new window.
* This method will toggle the driver handle from main window to popup
* window.
*
* @param driver
*/
public static void handlePopupWindow(WebDriver driver)
{
(new WebDriverWait(driver, 90)).until(new ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver d)
{
return d.getWindowHandles().size() > 1;
}
});
for (String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
}
Call this Utility method, whenever need to switch to a new window.
Subscribe to:
Posts (Atom)