Developing and debugging in Java via an interpreter

2 Feb

This is written from QA/automated testing perspective, but can be applied to general Java programming.

While the traditional and correct way to develop and debug in Java is using a good IDE with debugger and set breakpoints and watch variables and object references, etc., it is also useful to dynamically work and test with Java at runtime using an interpreter shell session.

You can say I got bit by the “bug” or am spoiled by such feature from Python, Ruby, etc.

I find it useful this way to test thing out quick w/o having to use full featured bloated IDE, setting breakpoints, and monitoring stuff. Just try some code, don’t work try something else from the shell.

This is particularly useful for quick test of code snippets and can be useful in QA for testing Selenium / WebDriver API calls and see if things work or not, or if you got the correct element locator or not.

With Java, you don’t really get an interpreter, but there are some options like interface to Java using Jython, JRuby, BeanShell. Don’t know all the options, but I believe BeanShell gives the closest approximation of using Java in a shell, so this article describes a bit of using it.

As an example of using Beanshell to test/debug/develop WebDriver test code, you can do the following:

Start up Beanshell with reference to Selenium library, I show Windows method, but Linux/Mac OS X method is similar:

java -cp .;selenium-server-standalone-2.15.0.jar;bsh-2.0b4.jar bsh.Console

or

java -cp .;selenium-server-standalone-2.15.0.jar;bsh-2.0b4.jar bsh.Interpreter

select the option you like best starting up an IDE like GUI console shell (bsh.Console) or use existing command prompt shell of your OS (bsh.Interpreter).

From there, you just use normal Java code like:

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;

WebDriver driver = new FirefoxDriver();
driver.get(“http://www.yourdomain.com”);
driver.findElement(By.id(“signin”)).click();

There may be some caveats from using interpreter shell as I’ve found for using WebDriver sendKeys() method, you have to write the command differently in the shell:

driver.findElement(By.id(“email”)).sendKeys(new String[]{“yourusername@gmail.com”});

see reference post here on reasoning for that:

http://groups.google.com/group/webdriver/browse_thread/thread/be568157b38ee47b/f83818c0d28afc49?lnk=gst&q=beanshell#f83818c0d28afc49

Once you got code you like, you can then copy & paste into your real Java code in IDE to complete it and run formal test.

Happy playing with Java interpreter shell now…particularly with WebDriver and Selenium!

Advertisements

One Response to “Developing and debugging in Java via an interpreter”

Trackbacks/Pingbacks

  1. A Selenium IDE alternative for other browsers and another record & playback method « autumnator - May 3, 2012

    […] Developing and debugging in Java via an interpreter […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Seek Nuance

Python, technology, Seattle, careers, life, et cetera...

TELLURIUM

New Era of Test Automation

Der Flounder

Seldom updated, occasionally insightful.

The 4T - Trail, Tram, Trolley, Train

Exploring Portland with the 4T

Midnight Musings

Thoughts on making art

Automation Guide

The More You Learn The More You Play...!

The Performance Engineer

Code.Test.Tune.Optimize.

humblesoftwaredev

Thoughts related to software development

Yi Wang's Tech Notes

A blog ported from http://cxwangyi.blogspot.com

Appium Tutorial

Technical…..Practical…..Theoretically Interesting

LinuxMeerkat

I swear! Meerkats can do Linux

PacketsDropped

Requeuing the packets dropped in my memory.

Two cents of software value

Writing. Training. Consulting.

@akumar overflow

wisdom exceeding 140 chars.

Lazy Programmer's Shortcut

Java, J2EE, Spring, OOAD, DDD & LIFE! .......all in one :)

Testing Mobile Apps

www.SoftwareTestingStudio.com

%d bloggers like this: