Skip to main content

Posts

Showing posts with the label java

Could not start Selenium session: You may not start more than one session at a time

Error is: java.lang.RuntimeException: Could not start Selenium session: You may not start more than one session at a time at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:107)   If you get this error on Selenium then this might be the problem… If in your test you are using a webdriver to create the DefaultSelenium or Selenium object then remove this line from your code:         selenium.start(); Happy Testing

“Try-Catch-Finally” vs Throws

Its really confusing sometimes when different people ask me to use “try-catch” or throws in my code while they religiously do the code reviews… Some ask me to stick to try-catch and others suggest just throwing all exceptions         After getting frustrated for a couple of times, I finally decided to read through various resources and find out for myself what’s best! Below is the gist of my findings:   What’s a Try-Catch Block? Oh yes the answer lies in its definition. A try-catch block helps us handle an exception. So basically its our own Exception Handling Construct…So use it when you would want your program to handle its exceptions by itself.   Thus we should fall back to using Throws when your program/function isn’t handling the exceptions it raises. You simply pass on the exceptions to its higher layer Pass the ball!!!   Hope this helps!

Simulating Keyboard strokes using Selenium1

Many a times we want to simulate keyboard strokes in our selenium tests. Here is how you can do this using Selenium1.   Command: selenium.keyPressNative(<KeyEvent_TobePassed>);   For Ex: If you want to simulate pressing Tab then you need to pass the command: selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+"");   View the list of KeyEvents here: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html   Hope this helps!

Convert String Array to Collection

How to convert a String Array to a Collection? Before we get to know how to convert a String Array into a Collection. We might want to know more details about the two… What’s the difference between a String Array and a Collection? Collection: It’s a group of data manipulated as a single object. Technically, its an interface in java which is implemented by various other classes. Many of which you uses regularly. One of its implementations can be seen in an array. An array best defined by the Sun is “An array is a container object that holds a fixed number of values of a single type.” So by implementing Collection interface, an Array skips the code required for its implementation – that is now taken care by the Collection interface! Why would I want to  convert a String Array into a Collection A Collection contains “Objects” if you understand the language…An Object is more generic implementation of the data types. So in a Collection, you can have any type of Object…So while w...

Get the Parent Directory!

We all work with files and file paths…I got this recently when I was trying to get the current directory of the user while he is navigating through the application. Issue: At any time, I should get the parent directory of the current user directory and perform the designated actions. Solution: We have system properties which will give us the current user directory… Access the system property using String userDir = System.getProperty(“user.dir”); Now you can user the File Java Class to access the parent directory as String parentDir = new File(userDir).getParent(); For Example: User is in /home/someuser/SeleniumTests/Test1 userDir will have the value /home/someuser/SeleniumTests/Test1 parentDir will have the value /home/someuser/SeleniumTests   Hope this helps! Happy Coding!

Standard way to find a Character TYPE!

While we are processing Characters, it’s a very common practice that we use ASCII values… I still remember in my computer class if I had to find out a character is a capital letter or not I would compare it with its ASCII value   I have also seen these comparisons if (ch >= ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’) – This means our character is an ALPHABET if (ch == ‘ ’) – This means our character is a whitespace     Now this comparison is language specific…I mean it works for English and some other international languages but fails for others… If you are interested in having an internationally accepted Character Test Code ready then read on     Char cTest; If (Character.isLetter(cTest) – This mean cTest is an Alphabet If (Character.isDigit(cTest) – This mean cTest is a Digit If (Character.isSpaceChar(cTest) – This mean cTest is a Space   There is a very useful function called getType which can return meaningful c...

Interface for and just for Static Constants?

http://stackoverflow.com/questions/320588/interfaces-with-static-fields-in-java-for-sharing-constants   Looks like using an interface and making core classes implementing the interface which has just the static constants pollutes the core class export API’s. You might want to reconsider the choice of using Interfaces for Static Constants

Java String Initialization - Null vs ""

In a code review, I got a comment which asked me why I initialized with a null instead of a "" Reason #1 When you use String s = null it will create variable "s" on stack only and no object will exists on heap,but as soon as you declare things as like String s=""; what it will does is like it will create "" object on heap.As we know that Strings are immutable so whenever u wil assign new value to string varible everytime it will create new Object on heap...So I think String s=null is efficient than String s = ""; Reason #2 When we initialize a string as "",we are actually supressing a possible exception that will be automatically raised when we have it initialized to null. Yes Java has inbuilt exception handler for treating nulls - remember NUllPointerException (the buggy exception)...So if we have used this string somewhere on our webpage and it doesn't get inialized properly at runtime, if we had it initialized ...

Selenium throws an exception when running on Firefox

we get this error when we are trying to run a testng annotated java selenium code on firefox: "[testng] com.thoughtworks.selenium.SeleniumException: this.getCurrentWindow is not a function" Then all the other annotations are SKIPPED. A workaround for such an error would be to put the line of code which gives this error under a try block. In my case it was openReader() I changed the code as below: try{ openReader()} catch(Exception e){System.out.println(e.toString());} The class should throw the Exception too! This solved the problem and after the exception is printed, the execution continues!

Java Exception: The type java.lang.Object cannot be resolved

Exeption Text: The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files There can be two solutions to this problem when it occurs in Eclipse Eclipse doesn’t know where the J2EE jar file is present. To give the path: - Right Click Project you are working on from Package Explorer- Go to Properties- Go to Java Build Path from the right tree structure- Go to Libraries Tab- Choose Add External Jars Button- Select J2EE.jar file normally in C:\Sun\AppServer\lib folder.- Hit Finish In Eclipse, the cause of this problem is that you somehow deleted the SYSTEM JRE LIBRARY associated with your project. Simple fix in Eclipse:-Right Click Project you are working on from Package Explorer-Go to Properties-Go to Java Build Path from the right tree structure-Go to Libraries Tab-Choose Add Library Button-Select JRE System Library-Hit Next, the Radio button will prompt the Workspace default JRE, and let it be that way-Hit Finish