Wednesday, December 29, 2010
Getting size of File or Folder
A class SizeCounter implements FileFilter interface from io package.
public class SizeCounter implements FileFilter {
private long total = 0;
public SizeCounter(){};
public boolean accept(File pathname) {
if ( pathname.isFile()) {
total+=pathname.length();
} else {
pathname.listFiles(this);
}
return false;
}
public long getTotal() {
return total;
}
private static long getFileOrDirectorySize(File file) {
SizeCounter counter = new SizeCounter();
file.listFiles(counter);
return counter.getTotal();
}
public static void main(String[] args) {
String folder = "E:\\test\\Shared";
long size = SizeCounter.getFileOrDirectorySize(new File(folder));
System.out.println("Size in Byte :" + size);
}
}
Monday, December 27, 2010
Singleton Design Pattern in Java
Singleton Class Code:
package designpattern.creational.singleton;
public class MySingleTon {
private MySingleTon() {
}
private static MySingleTon instance = null ;
public static synchronized MySingleTon getInstance() {
if(instance == null) {
System.out.println("Creating New Instance");
instance = new MySingleTon();
} else {
System.out.println("Returning Existing Instance");
}
return instance;
}
}
Code Explanation:
- Why the constructor declared private? – This is required so that the Singleton class could not be instantiated by any other class.
- Why the getInstance() method is synchronized? – This is required so that at any point of time only single class can access the Singleton Class. which means at any point of time we can have only single instance of class going outside the singleton class.
- Here we have created an static instance of MySingleTon Object, this object will be returned whenever user calls the Singleton class.
- Inside getInstance() we check whether the static instance of Singleton class holds the object if yes than we return the object else we create an new instance of the class.
- You must be wondering why clone method is written here. This is required so that we are not able to clone this class and create multiple instance of the SingleTon Class.
package designpattern.creational.singleton;
public class SingleTonClient {
public static void main(String[] args) {
//Will not compile, throw an error
MySingleTon s0 = new MySingleTon();
MySingleTon s0 = MySingleTon.getInstance();
System.out.println("Count:- " + s0.hashCode());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MySingleTon s1 = MySingleTon.getInstance();
System.out.println("Count:- " + s1.hashCode());
MySingleTon s2 = MySingleTon.getInstance();
System.out.println("Count:- " + s2.hashCode());
}
}
Here if you see if you try to create an new object of
MySingleTon class, the java compiler will give an error
that “Constructor MySingleton is not visible”. Then we
try to access the object of the Singleton class and to
cross check the hashcode printed is the same.
Factory Design Pattern in Java
Factory pattern comes into creational design pattern category, the main objective of the creational pattern is to instantiate an object and in Factory Pattern an interface is responsible for creating the object but the sub classes decides which class to instantiate. It is like the interface instantiate the appropriate sub-class depending upon the data passed. Here in this article we will understand how we can create an Factory Pattern in Java.
Now in the example that we are looking at for explaining the Factory Design Pattern is we are creating an object of Dog and Cat without calling the Dog or Cat class.
Mammal Class Code:
class MammalsFactory {
public static Mammals getMammalObject(String name) {
if (name.equalsIgnoreCase("Cat")){
return new Cat();
} else {
return new Dog();
}
}
}
Now if you see this class, here we will be passing an
argument to the getMammalObject function based on the
argument passed we return the Mammals object of the class.
Abstract Class Code:
public abstract class Mammals {
public abstract String doWalking();
}
Here we have created an abstract class Mammals.
This class will be used by the Dog and Cat class.
Cat Class Code:
public class Cat extends Mammals {
public String doWalking() {
return "Cats has been Informed to perform";
}
}
Here we have created Cat class, this class extends
from Mammals class so it is understandable that
Cat needs to have the implementation for doWalking()
method.
Dog Class Code:
public class Dog extends Mammals {
public String doWalking() {
return "Dogs has been Informed to perform";
}
}
Here we have created Dog class, this class extends
from Mammals class so it is understandable that
Dog needs to have the implementation for doWalking()
method.
Factory Method Client Code:
public class FactoryClient {
public static void main(String args[]) {
MammalsFactory mf = new MammalsFactory();
System.out.println(mf.getMammalObject("Dog").doWalking());
}
}
Here if you see i want to create an object for Dog
class and for that we are not directly loading the
Dog class. Instead we are instantiated the object
for MammalsFactory class.
Thursday, December 23, 2010
Selenium RC Issues as per my experience
I wanted to start this discussion by emphasizing on the effectiveness of Selenium RC for test automation of our web applications, but I am finding it difficult to pen down a paragraph not because it is not a very good tool and I had to materialize something to market Selenium RC but people already know so much about Selenium and you can find a lot of information about the effectiveness of Selenium RC and I do not want to repeat all that.
Therefore I decided to pen down some of the issues that we have faced while using Selenium RC and share the solutions where we find one and if some of you has better solution to these then please do not hesitate to share……Happy Sharing
1. Xpaths are very slow in IE so i prefer css locator. So Selenium IDE should record as css selector also because css locator is much faster than xpath for IE browser. But it records only as id locator or xpath locator. We need to convert those into css locator.
2. Selenium Rc does not support file upload/download pop ups. I am using AutoIt for providing these support with selenium.
3. Lack of built-in wait for ajax requests, we had to use a lot of custom waitFor statements based on the visual cues.
4. When i execute my tests on IE browser, few commands running very slow as :
- When any element is not present and i fire isElementPresent() command then it returns false very much slow. I can ten times slow than FF browser.
- I analyze that few commands like getValue, getAttribute running very slow.
Thanks
Wednesday, December 22, 2010
Reverse XPath
Finding the xpath of a element using bottom up approach
Assume that we need to find the xpath of a link "Login" appearing in a webpage. Usually when we find xpath of a GUI element, we normally follow a top-down approach, meaning the xpath is recorded from the parent level to the child. But there is also a bottom up approach, where in you can begin from a inner child element and navigate all the way up till the GUI element is reached.
Below is the corresponding xml code and various method of finding XPath of "Login" link. The bottom-up approach is explained by note (e) in the below table.
Sample XML Code | XPath |
(a) Using Top Down approach starting from a top node (b) Directly accessing the child node using its text value (c) Directly accessing the child node using child's attribute value (d) Directly accessing the child node using wildcard search. All occurences of text "Logi*" is matched by below xpath.It will work in this example since there is only one text "Login" which has occurence "Logi" in it. (e) Using Bottom Up approach starting from another child node and navigating to the actual node. Note: Here the DOM structure should be maintained (as it is in the xml) between the start node and end node
|
Thanks
Selenium Rc with https certification
I think I've now worked out the solution, It's a bit of a pain in the arse, but it does seem to work. Here goes.
- Close down any running Firefox instances.
- Start Firefox (the one you're going to run your tests with) with the profile manager:
firefox -ProfileManager
- Create a new profile. You'll be prompted to choose a directory for the profile. Put it somewhere inside the project where you're writing the tests.
- Select the profile and run Firefox using it.
- Browse to the HTTPS URL (with self-signed certificate) you're going to be testing against.
- Accept the self-signed certificate when prompted. This creates an exception for it in the profile.
- Close the browser.
- Go to the Firefox profile directory.
- Delete everything in the directory except for the cert_override.txt and cert8.db files.
- When you run your Selenium server (like in my Ant example above), pass a
-firefoxProfileTemplate /path/to/profile/dir
argument to it. This tells Selenium to use your partial profile (with certificate exceptions) as a basis for minting its new profile. So you get the certificate exceptions, but without any of the other clutter you would get if you used a whole profile.