Skip to main content

Posts

Showing posts from August, 2011

Test Enabling/Disabling JavaScript on a browser

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.thoughtworks.selenium.Selenium;

public class TestJS {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);

Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.somedomain.com");

selenium.open("/dp/gotopage/");
driver.findElement(By.id("prodImage")).click();
boolean result = driver.findElement(By.className("imageWarningMessage")).isEnabled();

if (result)
System.out.println("Result is: "+driver.findElement(By.className("imageWarningMes…

Remote Web Driver Program

Just a simple class depeciting the usage of a RemoteWebDriver which will be useful for any beginner...

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;


public class TestRemote {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DesiredCapabilities capability = DesiredCapabilities.firefox();
try {
WebDriver driver = new RemoteWebDriver(new URL("http://computername:4444/wd/hub"), capability);
driver.get("http://www.google.com");
} catch (MalformedURLException e) {
}
}
}

This will create a RemoteWebDriver which will run on a hub using firefox.

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 to null…

Set PATH

Whatever role you are into, this task comes to you eventually where you have to setup your environment! Setting up your classpath/path including...

So here is how you can do it!

Two ways:

Method one - Setting up the path just for the particular session
For LINUX: export PATH=$PATH:/home/gvsiri/apache-ant-1.8.2/bin
For Windows: set PATH=%PATH%;.;C:\SirishaGV\Software\apache-ant-1.8.2\bin

Method two - setting up the path for the profile
For Linux: Modify the .bash_profile file
For Windows: Change in the environment variables