Wednesday

Rally + Custom apps + RestAPI to customize reports / test cases



If you are looking to generate custom reports based on the various fields under requirements/ build / test cases then going the Rest API way is not the right solution. Rally provides something called the custom apps which can help you achieve what you want:

https://help.rallydev.com/custom

If you are not happy with the custom apps that Rally provides then your last resort is the Excel Add-in that rally provides :

https://help.rallydev.com/rally-add-excel-installation-user-guide

The link above has details that provide information about how to use the add-in.If you have troubles during the installation then make sure you have "visual studio tools for office 2010 runtime" installed!

Monday

Testing the Page Layout with Selenium Webdriver


If you are looking to test

  • Overlapping objects with selenium webdriver?
  • page layout of a website?
Easiest way to do that:

webelement.getPosition() and webelement.getDimension().
These two methods are quite good when it comes to testing the layout of a webpage. I did a quick test and found that the location would return the location of the element relative to the left top corner of the page. That means this should always return the same value irrespective of monitor resolution or window size.
If you can record the location and element dimension once and store these values somewhere, You can then run automated tests using selenium to find whether the position or dimension of the element changed. If that change is greater than a delta value, then it means the alignment of the element has changed and the test has Failed!


Tuesday

Book Review - Selenium WebDriver Practical Guide

 


In the past few automation years (years where you have new tools that create a big bang effect) everyone wants to know what is it that the open-source world has to offer via Selenium WebDriver.

Whether you are new to automation or you are someone who has dipped his feet using other tools, want to know more about:
  • How does WebDriver fit into an agile /Continuous Integration environment?
  • How Selenium WebDriver is able to automate the testing on multiple browsers?
  •  Are there common tips that can be used while automating your scenarios?
  • How do you create a test automation framework that has a collection of libraries and answers to all other business needs?

How the book is laid out:

It starts with the evolution of selenium and how it got its name, touches upon the bare minimum basics of the tool using Java. This is great, since the hardest thing about starting an automation project is often getting your basics right. The examples in the support files and downloads help you to quickly get into the meat of how you can achieve the same in a real-time project.

Since it’s basically a collection of topics, one doesn’t have to read the book in order. Each chapter starts with a background to what you will grasp by the end of your reading.

What I enjoyed:

I liked most of the chapters that cover advanced topics dwelling into problem solving patterns like the page object model. Since I have been using WebDriver for quite some time now, they really helped me to understand refresh using test automation in a fast paced agile/CI environment. Apart from that it has bonus chapters on leveraging the testing of iOS and android applications.


Who this book is for

If you are a quality assurance/testing professional, software developer, or web application developer looking to create automation test scripts for your web applications, this is the perfect guide for you! As a prerequisite, this book expects you to have a basic understanding of Java programming, although any previous knowledge of WebDriver or Selenium is not needed. By the end of this book, you will have acquired a comprehensive knowledge of WebDriver, which will help you in writing your automation tests.


Verdict: A must-buy for any person who wants to learn selenium WebDriver! 

Get the style of a web object using selenium webdriver



There are too many posts on this topic but they are all scattered in bits and pieces, bringing it all to one place to solve how to get the background color or any other css property of an element using webdriver

These days elements are rarely styled with just inline styles. For example:
1
<div style="color:red">text</div>
An element’s style will also be dependent on its parent elements, as well as its class (ie style sheets).
1
2
3
<div style="color:red">
  <span class="bigtext">text</span>
</div>
How do you get the actual style of the element (ie the computed style)?
For example color
WebElement elem = driver.findElement(By.xpath("//a[contains(@class,'test')]")); System.out.println(elem.getCssValue("color")); System.out.println(elem.getCssValue("background-color")); String color = elem.getCssValue("color"); String s1 = color.substring(5); StringTokenizer st = new StringTokenizer(s1); int r = Integer.parseInt(st.nextToken(",").trim()); int g = Integer.parseInt(st.nextToken(",").trim()); int b = Integer.parseInt(st.nextToken(",").trim()); Color c = new Color(r, g, b); String hex = "#"+Integer.toHexString(c.getRGB()).substring(2); System.out.println(Color.decode(hex)); WebElement element = driver.findElement(By.id("color"));
Note that shorthand CSS properties (e.g. background, font, border, border-top, margin, margin-top, padding, padding-top, list-style, outline, pause, cue) are not returned, in accordance with the DOM CSS2 specification- you should directly access the longhand properties (e.g. background-color) to access the desired values.

Friday

Some cool testing tips

This is an old one but i thought it was worth a share!


P.S Click on the image and download to zoom in!

Trim / Remove spaces in Xpath?



If you are wondering how you can trim the spaces in your xpath, this is how:

The normalize-space function strips leading and trailing white-space from a string, replaces sequences of white-space characters by a single space, and returns the resulting string.

Example : Which one is better?
//td[starts-with(normalize-space(),'Text to Trim')]
//td[starts-with(normalize-space(text()),'Text to Trim')]
The second option is better as it targets at the specific nodes


More string functions in XPATH:

FunctionsDescription
starts-with(string1string2)Returns true if the first string starts with the second string.
contains(string1string2)Returns true if the first string contains the second string.
substring(stringoffsetlength)Returns a section of the string. The section starts at offset
(which is a number), and is as long as the value provided at length.
substring-before(string1string2)Returns the part of string1 up until the first occurence of string2.
substring-after(string1string2)Returns the part of string1 after the first occurence of string2.
string-length(string)Returns the length of string (i.e. the number of characters).
normalize-space(string)Trims the leading and trailing space from string.
Also replaces consecutive occurrences of white space with a single space.
translate(string1string2string3)Returns string1 after any matching characters in string2 have been
replaced by the characters in string3.
concat(string1string2...)Concatenates all strings (i.e. joins them together).
format-number(number1string1,string2)Returns a formatted string version of number1 after applying string1 as
a format string. string2 is an optional locale string.


Thursday

Java- code to find the earliest date + Selenium WebDriver to perform actions


 Here is an interesting code that will
  1. Pick elements from a webpage 
  2. Convert the text into time               
  3. Put all the collected time into a sorted date set
  4. Pick the earliest date in the set
  5. Use this date to perform desired action on the webpage 
SortedSet dates = new TreeSet(); String time; List elements = driver.findElements(By.xpath("//a[contains(text(), ':')]")); for (WebElement webElement : elements) { time = webElement.getText(); if (!(time.equals(""))) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); Date date = simpleDateFormat.parse(time); //System.out.println(simpleDateFormat.format(date)); dates.add(date); } }
Date earliest = dates.first(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); String found = sdf.format(earliest); String id = driver.findElement(By.xpath("//a[contains(text(),'"+found+"')]")).getAttribute("title");
driver.findElement(By.xpath(".//*[@title='"+id+"']")).click
           


Tuesday

Partial screenshot / Highlight element before screenshot - Selenium WebDriver

If you are using selenium web-driver and you want to either highlight element while taking screenshots or take the screenshot of a particular element, here you go :

We can get the element screenshot by cropping entire page screenshot as below:

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage  fullImg = ImageIO.read(screenshot);
//Get the location of element on the page
Point point = ele.getLocation();
//Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
    eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
//Copy the element screenshot to disk
FileUtils.copyFile(screenshot, new File("c:\\partial.png"))
Highlight before screenshot:

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].setAttribute('style', arguments[1]);",f, "color: yellow; border: 5px solid red;"); File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

Just Highlight:

for (int i = 0; i < 30; i++) { 
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: orange; border: 5px solid orange;");
javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: pink; border: 5px solid pink;");
javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: yellow; border: 5px solid yellow;");
javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); 

Friday

Find a Frame ID in Firepath / Firebug



If you are into web automation you would always encounter frames that need to be identified and switched to. Here are simple steps that help you do the same:

If you are not able to recognize a web-element on a webpage. Chances are that it is in a frame ,  Just right click on the webpage and you will see that it's a Frame. This can also be seen in firepath's name

Steps to find the frame properties :

  1. Inspect on the element using firepath
  2. Click on the dropdown with the name of the frame
  3. Right click on the name and "inspect in DOM" tab
  4. Maximize the elements and look for an id attribute inside frame elements

Wednesday

Set Cookies in Selenium Webdriver / Run JavaScript


There have been instances where I would run JavaScript snippets from Java's WebDriver

WebDriver driver = new AnyDriverYouWant();
if (driver instanceof JavascriptExecutor) {
    ((JavascriptExecutor)driver).executeScript("yourScript();");
}
Example: ((JavascriptExecutor) driver).executeScript("window.focus()");
You can find more documentation on this in the JavaDocs of JavascriptExecutor.

Now the above example is good with JS being run directly buy what if you want to set a cookie?
If you are new to setting cookies then read this: http://www.quirksmode.org/js/cookies.html

Cookie ck = new Cookie("name", "value");
driver.manage().addCookie(ck);
Cookie cookie = new Cookie(name, value, domain, path, expiry);
Read this for Selenium!