Saturday, July 6, 2013

Migrating from Selenium To WebDriver

We all know that WebDriver is improved and enhanced version of Selenium commonly known as Webdriver or selenium 2, the Selenium team is now more focused  on webDriver and currently doesn't provide much support for Selenium(1).

So people who are using Selenium from its inception or before webDriver are facing biggest challenge that how to easily migrate from Selenium to webDriver. There is a class named webDriverBackedSelenium  which provides both selenium as well as webDriver functions but its not a clear approach as even if you use this class you have migrate to pure webdriver later, Using WebDriverBacked is a quick Step but it is a temporary Solution, you can also migrate to WebDriver easily.

I will now explain how I performed the migration process

We are using Selenium from last 2 years and we have written all our test cases in Excel sheets and use them for executing test cases using a self written java framework. Selenium functions are executed using JAVA reflection property.

So biggest challenge was how to minimize the effort required in changing existing test cases when we migrate from selenium to Webdriver

I have designed a new class for webDriver functions and defined all selenium functions in that but using webDriver, function name is same as Selenium functions but it uses webdriver classes then defined a new class which executes these functions using reflection. Using this approach, now it requires a very little effort to change existing input sheets and also little training is required for existing users for using the new Suite

Now my new suite can handle both webdriver requests as well as Selenium requests

I have just started using webDriver but i must say it is much better than Selenium.

Sunday, June 30, 2013

Facebook : How to Avoid unwanted tags in Pictures or posts

Facebook : How to Avoid unwanted tags in Pictures or posts

On FB we all are facing problem related to unwanted tags in friends posts.

Your friends without asking you tags in your posts just to get more likes

So how to avoid it, facebook already has a very nice way of avoiding this but it seems very few people are aware of it

Solution
1. Go to account settings
2. Click Timeline and Tagging
3. In who can add things to my time line, Enable "Review posts friends tag you in before they appear on your timeline?"

Now each time some one tags you, facebook first ask you to review it and then adds it your time line

You can also remove multiple tags using this feature

selenium Mosue Over not working if hover property is defined in CSS

Problem

Selenium is not able to perform mouseOver if Hover property is defined in CSS
You can try yourself on this link using selenium ide http://www.w3schools.com/cssref/tryit.asp?filename=trycss_sel_link_more2
Selenium mouse Over has no effect on this link
please visit this link

Solution

According to this question, this is one of those perennial problems Selenium and :hover css
They discuss a couple of solutions, but it looks like the problem is that as Javascript cannot trigger the :hover pseudo class, so Selenium can't either.

You can use webdriver to solve this problem, Webdriver code for mouse over

void mouseOver(String UiLocator) throws Exception
{
element =  driver.findElement(findBy(UiLocator));
Actions builder=new Actions(driver);
builder.moveToElement(element).build().perform();
}


Selenium: Is there any JS (JavaScript) code coverage tool which we can integrate with Selenium Server/RC

Solutions
1.
I don't know what you are trying to achieve, but:
  • Selenium is testing the final output, as seen on the page itself.
So it really does not matter if its PHP, HTML, JSP, ASP or .NET - the Selenium is designed to mimick the end user and click the final application - the final HTML code generated by whatever is under the hood.
Selenium is also not that good for code coverage tests - one piece code can be on many pages - so better approach with selenium is to do the "user" coverage - try to cover all the possible actions which living human could possibly do with your page

2.
Not aware of a tool for Selenium, but JsTestDriver has a design very similar to Selenium RC (can launch tests from the command line and they are run on a server that drives browsers headlessly) and provides code coverage information.
IntelliJ integrates with JsTestDriver and provides a visual display of coverage information.

3.
In theory, you can use any coverage tool that instruments JavaScript code to collect coverage data.
For example, you can use JSCoverage either in proxy mode (for real-time instrumentation) or for static instrumentation to run your Selenium tests off the instrumented code.
One technical hurdle could be to devise a way to accumulate coverage data across multiple pages. The coverage tool of your choice could already have support for accumulation of data from multiple page runs. If not, you might have to handle that bit yourself, but this is probably not something trivial.

4.
There is no particular tool that can integrate with Selenium to do JS coverage. However there are lots of tools which test JS on every page which can tell if the JS that executing on your web page had any errors. This may not ideal solution but on each page you will have the measure of uptil which point JS executed properly on your webpage under test. There are two solutions for that:
1.) JSErrorCollector API: It will integrate directly with Selenium and let you know if there were any error on the page. Can be found at: http://mguillem.wordpress.com/2011/10/11/webdriver-capture-js-errors-while-running-tests/
2.) Full fledged JS coverage tools: There is an excellent list of tools here which will essentially help you in covering JS on on your web pages. Can be found at: Looking for a better JavaScript unit test tool

5.
I've integrated this tool in my Selenium tests a time ago. You need a bit of work to gather coverage info before page changes (in any case js trigger a page reload, link etc...)
Once you set up everything, it will fully coverage any js executed while Selenium load and test your website pages.
PS : Even if it was specially adapted for YUI test, you can use it with selenium.

Ant: copying a directory from one windows system to another

Problem
How to copy a directory from a windows system to a remote windows system using ant
there is one scp command and FTP command, please provide me an example to perform this task using scp and ftp.
Also scp requires SSH which is not common for windows system.
So how to use SCP on windows system using ant
Also if you know any better approach for windows system using ant or java, please share
Also there is one SC command(don't know how to use it)

Solutions
1.
Download:
<scp file="${username}:${password}@${ip}:${path-to-file}" todir="${dir}" trust="true" />
or Upload:
<scp file="${path-to-file}" todir="${username}:${password}@${ip}:${dir}" trust="true" />  
Ant scp task provides attributes such as localFile and remoteFile to replace "file", and localTofile / localTodir and remoteTofile / remoteTodir to replace "dir". Using these attributes can help avoid confusion when you need some scp tasks to get files from the server to local machine, while others to upload files from the local machine to server.
Like this (for uploading):
<scp localFile="${path-to-file}" remoteTodir="${username}:${password}@${ip}:${dir}" trust="true" />
Check the ant manual to see more information: http://ant.apache.org/manual/Tasks/scp.html
Note:
1. Avoid copying multiple files; Copy a zip archive and a ant build file together with it, and unzip on target machine.
2. Using scp, you need to setup ssh server on the target machine; you also need to put jsch.jar in your ANT_HOME/lib. The jsch.jar can be downloaded from

2.
You should go for using FTP to copy files between the windows machines. There are other protocal too, but you should go with FTP.
A sample for using FTP.
<ftp server="${server.location}"
   remotedir="${directory.to.copy}"
   userid="${ftp.username}"
   password="${ftp.password}"
   depends="yes">
   <fileset dir="**Files****"/>
</ftp>
You can further look for details here. There are many options to try there.

3.
To copy a directory from your local host to a remote host using the ant scp, you have to specify the parent of the directory to the fileset and include the directory name eg.
<scp todir="user@hostname:/destination/dir/" keyfile="id_dsa" passphrase="abc" sftp="true" >
    <fileset dir="${parentDir}/"  >
        <include name="**/${dirToCopy}/" />
    </fileset>
</scp> 
sftp="true" helped another user on this subject so I included it. In my case I believe that adding the "/" at the end of the parentDir and dirToCopy, which signifies a directory at the command line, finally let me copy a directory and not just it's contents.

xpath and selenium : locate a node using exact text match

Problem

I want to locate a Element on a Web Page using text
I know there is a method name contains to do so
ex
tr[contains(.,'hello')]/td
But problem is if i have two elements name hello and hello1 then this function does not work properly
is there any other method like contains for exact string match for locating elements

Solutions
1
Well, your problem is that you are searching text into the tr (which is not correct anyway) and this cause a problem to the function contains which cannot accept a list of text. Try to use this location path instead. It should retrieve what you want.
//tr/td[contains(./text(),"hello")]
This location path will retrieve a set of node on which you have to iterate to get the text. You can try to append the
/text()
but this will cause (at least on my test) a result that is a string which is a concatenation of all the matched strings.

2.
tr[.//text()='hello']/td
this will select all td child elements of all tr elements having a child with exactly 'hello' in it. Such an XPath still sounds odd to me.
I believe that this makes more sense:
tr/td[./text()='hello']
because it selects only the td that contains the text.
3.
It all depends on what your HTML actually contains, but your tr[contains(.,'hello')]/td XPath selector means "the first cell of the first row that contains the string 'hello' anywhere within it" (or, more accurately, "the first TD element in the TR element that contains the string 'hello' anywhere within it", since Selenium has no idea what the elements involved really do). That's why it's getting the wrong result when there are rows containing "hello" and "hello1" - both contain "hello".
The selector tr[. ='hello']/td would be more accurate, but it's a little unusual (because HTML TR elements aren't supposed to contain text - the text is supposed to be in TH or TD elements within the TR), and it probably won't work (because text in any other cells would break the comparison). You probably want tr[td[.='hello']]/td, which means "the first TD element contained in the TR element that contains a TD element that has the string 'hello' as it's complete text".

Wednesday, December 28, 2011

How to integrate Sikuli In selenium

Lets start with some basics
Selenium is a Web Automation tool which we can integrate in java projects or any any other supported language products
Selenium is a open source tool but it has some limitations like it can not interact with things which are not part of browsers, browser pop ups, Flash Contents, Java Applets etc.

To solve this we can use another open source tool "SIKULI" and integrate it with our existing selenium automation framework.

We have other tools like AutoIt for same purpose but what's the difference, Why i choose SIKULI a new tool over AutoIT

Reasons are simple
1. I found SIKULI easier to understand as it needs only screenshots, as a Automation suite designer its my responsibility that who ever is using my Suite should find it easy to use and understand, Sikuli serves that purpose more than AUTOIT
2. Sikuli is easy to integrate with your existing  java based selenium framework as we can include sikuli jar in our framework same as including selenium jar.

How To Integrate in Existing framework
First we need to know how to use Sikuli in java


      How to use Sikuli Script in your JAVA programs

The core of Sikuli Script is written in Java, which means you can use Sikuli Script as a standard JAVA library in your program. This document lets you know how to do that.

     Get sikuli-script.jar from your Sikuli IDE installation path.

Sikuli Script is packed in a JAR file - sikuli-script.jar. Depending on the operating system you use, you can find the sikuli-script.jar in according places.
  • Windows, Linux: Sikuli-IDE/sikuli-script.jar
  • Mac OS X: Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar

     2Install OpenCV.

Sikuli Script uses OpenCV as its computer vision engine. On Windows, you can simply add the path Sikuli-IDE/tmplib to the environment variable PATH, and then Windows can automatically find OpenCV's DLLs in there. However, it's more complicated on Mac and Linux. Therefore, our suggestion is to install OpenCV by yourself.
  • Mac OS X: you can install OpenCV using  Darwin Ports. Type "port install opencv" in Terminal.app once you have installed Darwin Ports.
  • Linux: OpenCV is often packed as a series of packages. For example, libcv4, libcvaux4, libhighgui4 on Ubuntu Linux.
== 3. Include sikuli-script.jar in the CLASSPATH of your Java project.==
We use Eclipse as an example. After adding sikuli-script.jar as a library into your project, the project hierarchy should look like this.

   4Import the Sikuli classes you need

You can simply "import org.sikuli.script.*" or import the classes you need. In most cases, you would need at least RegionScreen, and SikuliScript. (Note: the package name in 0.9 and 0.10 was edu.mit.csail.uid. If you are using Sikuli X, please use org.sikuli.script as the package name instead.)

  5. Write code!

Here is a hello world example on Mac. The program clicks on the spotlight icon on the screen, waits spotlight's input window appears, and then types "hello world" and hits ENTER.

import org.sikuli.script.*;

public class TestSikuli {

        public static void main(String[] args) {
                Screen s = new Screen();
                try{
                        s.click("imgs/spotlight.png", 0);
                        s.wait("imgs/spotlight-input.png");
                        s.type(null, "hello world\n", 0);
                }
                catch(FindFailed e){
                        e.printStackTrace();                    
                }

        }

}


After you have tested sikuli lets integrate it with our Existing SELENIUM framework

First Include in your test sheet a flag by which your framework knows that next commands in the sheet represent sikuli commands as selenium and sikuli both have same command set like click, type etc.

Also it gives one more advantage you can use java reflection for sikuli to minimise your coding efforts

Like you include a flag named 'sikuli' with values on and off, if value is on than use sikuli functions else use selenium. By this way you can easily manage both selenium and sikuli functions.

Using both SIKULI and SELENIUM you will be able to easily handle all windows that selenium is not able to handle.

Disadvantages of using SIKULI in Selnium
1. It limits the selenium RC, we can not use it remotely and it we can use selenium with sikuli as a standalone server
2. It decreases the speed of selenium as sikuli click takes much more time compared to selenium click as sikuli first searches and matches then click.
3. Sikuli takes more CPU resources than selenium