Monday 30 September 2013

Echo ,Store command and storeVars



Echo ,Store command and storeVars
Store command
Helps in declaring and assigning a value to variable
 Ex:
Command : store
Target :1000
Value : a
This  will create a variable ‘a’ and  assign 1000 as its value
Echo command
Helps in printing a value to the log. It can be character, string, number or value of variable.
To print value of variable,
Command: echo;
Target: ${Variable Name}
Ex:
Command: echo
Target: $(a)
This will print value of variable ‘a’ that is 1000 to the blog
Note:
If you simple give ‘a’ in the target instead of ‘${a}’ then this is treated as a string/Character and ‘a’  is printed to log but not its value.
All the variables declared in a test are stored into an array called ‘storedVars’ .In order to refer value of variable inside storedVars array, you can call Target: JavaScript {storedVars [‘a’]}
Command: echo
Target: JavaScript{storedVars[a]}
Command: echo
Target : storedVars[‘a’] to the log but not value of ‘a’
Assigning value of  variable to another variable:
Command : store
Target :${a}
Value : b
Or
Command : store
Target: JavaScript{storedVars[a]}
Value: b
In both examples mentioned above , value of ‘a’ assigned to variable ‘b’
storeEval Command:
Using  storeEVal  Command  you can directly assign  or print value of a variables without  help of ‘Javascript{}’ in the  target
Ex:
Command : storeEVal
Target : storedVars{a}
Value : c
Observe that  in the above example  we are directly  calling storedVars[‘a’] to assign value to variable ‘a’ to ‘c’

Thursday 19 September 2013

Useful XPATH patterns

starts-with
Many sites use dynamic values for element’s id attributes, which can make them difficult to locate. One simple solution is to use XPath functions and base the location on what you do know about the element. For example, if your dynamic ids have the format <input id="text-12345" /> where 12345 is a dynamic number you could use the following XPath://input[starts-with(@id, 'text-')]
contains
If an element can be located by a value that could be surrounded by other text, the contains function can be used. To demonstrate, the element <span class="top headingbold"> can be located based on the ‘heading’ class without having to couple it with the ‘top’ and ‘bold’ classes using the following XPath: //span[contains(@class, 'heading')]. Incidentally, this would be much neater (and probably faster) using the CSS locator strategycss=span.heading
siblings
Not yet written - locate elements based on their siblings. Useful for forms and tables.
Starting to use CSS instead of XPATH
Locating elements based on class
In order to locate an element based on associated class in XPath you must consider that the element could have multiple classes and defined in any order. However with CSS locators this is much simpler (and faster).
  • XPath: //div[contains(@class, 'article-heading')]
  • CSS: css=div.article-heading

 

How to write XPATH for an element for Selenium IDE?
.The purpose of it in Selenium  is to locate an element which can be used while writing a script in Selenium IDE.The XPATH when written in Selenium is helpful in locating the element without fail.
Importance of XPATH in Selenium IDE
XPATH is useful in identifying and locating the page elements with it's html information.Each and every html tag has  XPATH for it.It can be written for each and every html tag.When writing it you will be using the html tags for reference.To get the XPATH of  an element you should install the Firebug in your Mozilla Firefox browser.It helps in identifying the XPATH by using the HTML tags.As Selenium IDE runs on Mozilla Firefox browser the Firebug is the tool which should be added to your browser to make your work easier while writing XPATH.

How to write XPATH for Selenium IDE ?
It  is written with the tags of the elements.

You can get the basic XPATH of an element by using the Firebug which you have added to the Mozilla Firefox browser as an Add-On. To get an XPATH of an element right click on the element and select "Inspect element with Firebug". And when you happen to visit the element in the Firebug right click on it and select copy XPATH. By this method you can copy the XPATH and paste it in your Selenium IDE tool's Target section.

How does the basic XPATH look like?
/html/body/div[2]/div/div/div/div/div/div/div/h1/span

Is there any other method by which the XPATH looks simple?
The way to write the XPATH for Selenium is as follows

//element[@attribute="attribute value"]

Here are the examples for you.

<div id="y-cols" class="clearfix y-fp-ln-pg">

//div[@id="y-cols" @class="clearfix y-fp-ln-pg"]

<span class="logo">Yahoo! India</span>

//span[@class="logo"]

<img id="p_13872472-header-image" class="" src="http://l1.yimg.com/t/images/paes-and-bhupathi-392-160612.jpg">

//img[@id="p_13872472-header-image" and @class=""]

<input id="p_13838465-p" class="input-query input-long med-large">

//input[@id="p_13838465-p" and @class="input-query input-long med-large"]

<button id="search-submit" class="searchsubmit med-large y-fp-pg-grad">search</button>

//button[@id="search-submit"  @class="searchsubmit med-large y-fp-pg-grad"]

In this  same manner you can write XPATH for all the elements




Wednesday 18 September 2013

WebDriver: Advanced Usage


Explicit and Implicit Waits
Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step.
Explicit Waits
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.
This example is also functionally equivalent to the first Implicit Waits example.
Expected Conditions
There are some common conditions that are frequently come across when automating web browsers. Listed below are Implementations of each. Java happens to have convienence methods so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them.
  • Element is Clickable - it is Displayed and Enabled.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
The ExpectedConditions package (Java) (Python) (.NET) contains a set of predefined conditions to use with WebDriverWait.
Implicit Waits
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
RemoteWebDriver
Taking a Screenshot
import java.io.File;
import java.net.URL;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Testing {
   
    public void myTest() throws Exception {
        WebDriver driver = new RemoteWebDriver(
                                new URL("http://localhost:4444/wd/hub"),
                                DesiredCapabilities.firefox());
       
        driver.get("http://www.google.com");
       
        // RemoteWebDriver does not implement the TakesScreenshot class
        // if the driver does have the Capabilities to take a screenshot
        // then Augmenter will add the TakesScreenshot methods to the instance
        WebDriver augmentedDriver = new Augmenter().augment(driver);
        File screenshot = ((TakesScreenshot)augmentedDriver).
                            getScreenshotAs(OutputType.FILE);
    }
}
Using a FirefoxProfile
FirefoxProfile fp = new FirefoxProfile();
// set something on the profile...
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, fp);
WebDriver driver = new RemoteWebDriver(dc);
Using ChromeOptions
ChromeOptions options = new ChromeOptions();
// set some options
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(dc);
AdvancedUserInteractions
The Actions class(es) allow you to build a Chain of Actions and perform them. There are too many possible combinations to count. Below are a few of the common interactions that you may want to use. For a full list of actions please refer to the API docs Java C# Ruby Python
The Advanced User Interactions require native events to be enabled. Here’s a table of the current support Matrix for native events:
platform
IE6
IE7
IE8
IE9
FF3.6
FF10+
Chrome stable
Chrome beta
Chrome dev
Opera
Android
iOS
Windows XP
Y
Y
Y
n/a
Y
Y
Y
Y
Y
?
[1]
n/a
Windows 7
n/a
n/a
Y
Y
Y
Y
Y
Y
Y
?
[1]
n/a
Linux (Ubuntu)
n/a
n/a
n/a
n/a
[2]
[2]
Y
Y
Y
?
[1]
n/a
Mac OSX
n/a
n/a
n/a
n/a
N
N
Y
Y
Y
?
[1]
N
Mobile Device
n/a
n/a
n/a
n/a
n/a
?
n/a
n/a
n/a
?
Y
N

[1]
(1234) Using the emulator

[2]
(12) With explicitly enabling native events
Browser Startup Manipulation
Todo
Topics to be included:
  • restoring cookies
  • changing firefox profile
  • running browsers with plugins
Using a Proxy
Internet Explorer
The easiest and recommended way is to manually set the proxy on the machine that will be running the test. If that is not possible or you want your test to run with a different configuration or proxy, then you can use the following technique that uses a Capababilities object. This temporarily changes the system’s proxy settings and changes them back to the original state when done.
String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new InternetExplorerDriver(cap);
Chrome
Is basically the same as internet explorer. It uses the same configuration on the machine as IE does (on windows). On Mac it uses the System Preference -> Network settings. On Linux it uses (on Ubuntu) System > Preferences > Network Proxy Preferences (Alternatively in “/etc/environment” set http_proxy). As of this writing it is unknown how to set the proxy programmatically.
Firefox
Firefox maintains it’s proxy configuration in a profile. You can preset the proxy in a profile and use that Firefox Profile or you can set it on profile that is created on the fly as is shown in the following example.
String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);