Monday, December 5, 2011

JUnit or TestNG annotations gives syntax error

Sometimes JUnit or TestNG annotations gives syntax error eventhough we have imported all the necessary packages. It gives the error "Syntax error, annotations are only available if source level is 5.0". When you hover at the annotations, you will be able to see this error.

Actually nothing wrong with your JUnit or TestNG packages/jars. This is something to do with your Eclipse setup. This will happen if you have JDK 1.3 or 1.4 installed or configured with your Eclipse. If you have JDK 1.5 or 1.6 installed/configured you can fix this issue. Make sure which version of JDK has been installed/configured.

Follow these steps to get this issue fixed.

1) Eclipse > Window Menu > Preferences - Expand Java - Select Compiler - Check the version of JDK Compilance level.
2) If you see JDK 1.3 or 1.4, select JDK 1.5. or 1.6 for all the options and click on Apply.


Saturday, November 19, 2011

Browser window not getting maximized while using Selenium windowMaximize() method

Unlike QTP, selenium tests can be executed without maximizing the browser window. Unless you click on the browser window, it will not get maximized. So that we can work on other things when the tests gets executed. Though its useful for us, sometimes we want to see whats going on in the web application and we maximize the browser window from taskbar.

Even we have Selenium windowMaximize() method to maximize the web application. But, if you see, it does maximize!!!, actually it does, but the application (broswer window) will not get the focus. When selenium.windowMaximize() gets executed, the application gets maximized and still it stays in the task bar. Manually we need to click on the browser tab on the taskbar to get the focus of that application.

Normally we use the below code for starting Selenium and maximizing the browser window.

@Before
public void setUp() 
{
         selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/");
         selenium.start();
         selenium.windowMaximize();
         selenium.windowFocus();
}

@Test
public void testUntitled()
{
         selenium.open("/");
         .............................
         .............................
}

But, most of the times your browser window doesn't get maximized. Some time it gets maximized only if you dont have any other window opened in your taskbar. We have a workaround to resolve this issue. Thats Java Robot class.

We can use Java Robot class to minimize all the windows before opening the URL using Selenium object. We need to activate Window and M keys using robot class. This will minimize all the applications opened. Then we can call selenium.windowMaximize() method which will maximize your browser window. Use the below code.


@Before
public void setUp() 
{

         selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/");
         selenium.start();
         Robot robot = new Robot();
         robot.keyPress(KeyEvent.VK_WINDOWS);
         robot.keyPress(KeyEvent.VK_M);
         robot.keyRelease(KeyEvent.VK_WINDOWS);
         robot.keyRelease(KeyEvent.VK_M);
         Thread.sleep(1000);
         selenium.windowMaximize();
         selenium.windowFocus();
         selenium.windowMaximize();

         selenium.windowFocus();
}

@Test
public void testUntitled()
{
         selenium.open("/");
         .............................
         .............................
}

Hope this code helps to maximize your browser window when you execute your Selenium tests.

Friday, November 18, 2011

Handling Certificate errors on Firefox and IE using Selenium

When we test some sites, we might get SSL Certificate errors and it will be annoying for the Automation testers. They need to manually click on Add Exception which doesn't help when we go for executing Automation regression packs. Its not possible for the tester to click on Add Exception each and every time the critificate error pops up. The reason for getting this certicate error is that RC creates new Firefox profile every time the scripts gets executed.



We have some workarounds for handling these errors in Firefox and IE.

Firefox:

For Firefox, we have two workarounds.

First Workaround:

When you start your Selenium server, you need to add one parameter -trustAllSSLCertificates which will make your Selenium server as proxy for Firefox browser session. Then, it can easily handle any HTTPS website.


java -jar selenium-server-standalone-2.2.0.jar -trustAllSSLCertificates

Second Workaround:

We should not allow Selenium RC to create a new Firefox profile every time. Instead we can create a dedicated profile and ask Selenium RC to use that every time. Follow the below steps:

1) Close all Firefox windows opened.
2) Go to cmd prompt and C:\Program Files\Mozilla Firefox>
3) From Mozilla folder, open Firefox Profile manager using this command, firefox -ProfileManager


4) Create a new profile for Selenium in ProfileManager dialog box and select the folder C:\Profile.
5) Choose that profile and click on Start Firefox.
6) Once Firefox loads, open the page which gives certificate error and add the exception manually.
7) Close your firefox session.
8) Now start the selenium server with the following command:
java -jar selenium-server-standalone-2.2.0.jar -firefoxProfileTemplate C:\Profile

The server will be started with the Firefox profile which you have created. When we execute our scripts, server will launch that Firefox profile.

Internet Explorer:

For Internet Explorer browser, it will be simple and easy. We need to add a small code just after selenium.open() statement.

Ex:

@Test
 public void testMethod()
{
        selenium.open(url);
        if (selenium.isElementPresent("overridelink"))
        {
            selenium.click("overridelink");
            Thread.sleep(30000);
        }

        ....................................
        ...................................
}

Monday, November 14, 2011

Handling Authentication dialog box using Selenium and AutoIT

As we know Selenium is an automation tool for web based applications and it handles only web elements and popups which triggered from web pages. Sometimes we get Window based popups like Authentication dialog box, Security warnings while using the web pages. The question would be how to handle these popups from Selenium?

The answer would be there is no straight forward method or command in selenium to handle these popups. We can use getAlert() and ChooseOKOnNextConfirmation() methods only when the popups are triggered from web pages.

In this scenario, we need to go for AutoIT scripts which can handle these kind of popups. These scripts can be integrated with Selenium code. Follow these steps to handle Authentication dialog box using Selenium and AutoIT.

1. Download AutoIT tool. It can be downloaded from http://www.autoitscript.com/site/autoit/downloads/

Note: Please download AutoIT Full Installation

2. While installing AutoIT, you will get two options: Run the Script and Edit the Script. If you are going to use the script without any modification, choose Run the Script option. If you want to customize the scripts, choose Edit the Script option (Recommended as we always needs to customize the scripts for our needs. Sometimes we need to create our own scripts.)

3. Once installed you can find all the sample scripts in the Example folder. Assume we have a sample script for handling the Authentication box which is Login.au3 (extension of the AutoIT scripts will be .au3). Right click on that file and choose Edit option. Find the script below:


WinWaitActive("Authentication Required","","20")
 If WinExists("Authentication Required") Then
 Send("userid{TAB}")
 Send("password{Enter}")
 EndIf
4.Customize the scripts according to your need. 
For example, You can type your username and password in the script.
WinWaitActive("Authentication Required","","20")
 If WinExists("Authentication Required") Then
 Send("selenium_user{TAB}")
 Send("selenium123{Enter}")
 EndIf

5. Save the file once you modify all the required changes.

6. Now, open the application Aut2Exe which will convert your AutoIT script into EXE format.



7. Now, call this EXE file from your selenium code as below.

@Test
public void testUntitled()
{
         Runtime run = Runtime.getRuntime();
         Process pp=run.exec("C:\\Program Files\\AutoIt3\\Examples\\Login.exe");
         selenium.open(URL);
         ...................
         ...................
}

8. Execute you selenium code and check if the AutoIT script handles the Authentication Required dialog box.

Even we can handle File Save As, Open With dialog boxes using AutoIT and Selenium. It will be covered in my coming blogs. Stay connected :-)

Thursday, November 10, 2011

Selenium and Infragistics controls - Data Grid

When we work with Selenium IDE and infragistics contorls - Data Grid, we might experience the following issues.

1. Selenium IDE records click and type actions for typing text in Data Grid control. But, if we try to execute the same code, it will not type anything in Data Grid. The Gird has to be focused first, otherwise the type command will not work. When we use click command, the Gird will not get focus.

Solution: Instead of click, we need to use clickAt command which will fix this issue. Once we use clickAt, the grid will get the focus, then we can use type command to add some text.

clickAt [locator] [x,y]

Refer the following screenshot which will give you a clear picture.

Wednesday, November 9, 2011

Selenium and Infragistics controls - Grid dropdown

When we work with Selenium IDE and infragistics contorls, we might experience the following issues.

1. Selenium IDE records click action for selecting an item in Grid dropdown control. But, if we try to execute the same code, it will not select any item. First of all, it will not click on Grid.

Solution: If we use, doubleClick instead of click, it will fix this issue. Refer the following screenshot which will give you a clear picture.

Friday, October 28, 2011

No Class Definition Found Error in Eclipse

If we want to run Selenium Java code from Eclipse, we need to include the following jar files in our class path.

1) selenium-java-2.x.jar (Java Client Driver)
2) JUnit-4.x.jar or TestNG.jar (Testing Framework)

These two files are enough for executing our Selenium Automation scripts. Sometimes we might get the below error while executing the script. In this scenario, we need to include our selenium server jar file in the class path which will fix this issue. I have experienced this issue several times and this solution has fixed my problem. Please try this.

selenium-standalone-server-2.x.jar


Expected Error:

java.lang.NoClassDefFoundError: com/google/common/base/Charsets

at com.thoughtworks.selenium.HttpCommandProcessor.getOutputStreamWriter(HttpCommandProcessor.java:138)
at com.thoughtworks.selenium.HttpCommandProcessor.getCommandResponseAsString(HttpCommandProcessor.java:165)
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:108)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:90)
at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:266)
at com.thoughtworks.selenium.HttpCommandProcessor.start(HttpCommandProcessor.java:227)
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:82)
at com.eviltester.seleniumtutorials.PlacedOrder_final.setUp(PlacedOrder_final.java:12)
at junit.framework.TestCase.runBare(TestCase.java:132)
at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:228)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Charsets
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 23 more

Wednesday, October 19, 2011

Format Option missing in Selenium IDE menubar

Some people are only using Selenium IDE to prepare their test suites and run it from there. But, most of the people using Selenium RC code and run it from their favourite language editor. Usually they record the web application actions in IDE and convert the selenese table format into Java or C# or Perl or Ruby etc format. Those format can be copy & pasted into language editors like Eclipse (Java) or Visual Studio (C#) and we can run the code from there.

But, the recent issue with IDE is that we dont find any language options under Format. These options are disabled from the version IDE 1.0.11 as they declared these options as Experimental features. Because, if you convert them into any language format and make some modifications, again IDE will not be able to convert them into selenese table format if you want to revert back. If you still want those experimental features, follow the below steps in IDE:

1) Under Options menu, click on Options
2) Select "Enable experimental features" option and click on OK.



Now come back to Options --> Format. You will be able to see all the language formats.

About this Blog

I got interested in Selenium Automation Tool and done automation for different projects. When I started this tool, I didn't get proper support or forum to give some advise for the errors which I faced. However, I have done research most of the time and found solutions for some familiar Selenium issues. I felt that I want to share the solutions with selenium starters or users who are facing similar issues and started this blog.

Hope this blog will be helpful for Selenium users.

Thanks
VEERA