Thank you very much for taking the time and giving me feedback.
Visitors are also reading...
← PreviousNext →

Setting up a selenium/chrome project

26 Nov 2011

Setting up a Selenium Chrome Project

Selenium is an automation for GUI testing for Web Applications.
Once you run the automation, a browser popup up, and you will see user action happen on its screen such as :

In order to get it running with Chrome you will need to download chrome driver and selenium. (see link below).
I am using Maven to run Selenium.
The basic pom will be :

<project xmlns:xsi="" xmlns="" xsi:schemalocation="


Next thing, you need to set the location for the chromedriver. (a download for each download is available, so links below).
There are several ways to specify the location.
If you don’t set it, you will get a nice error message to remind you.

Exception in thread "main" java.lang.IllegalStateException: The path to the chromedriver executable must be set by the system property; for more information, see The latest version can be downloaded from  
 at com.incapsula.selenium.SeleniumMain.main(  
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
 at sun.reflect.NativeMethodAccessorImpl.invoke(  
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(  
 at java.lang.reflect.Method.invoke(  
 at com.intellij.rt.execution.application.AppMain.main(  

In my code sample, you will see I set the property in the environment, and then assign it to a property.
I suggest you place the binaries in “resources” folder as accepted in maven project.
This way you will save the time and trouble for other developers to find/download it.
After you take care of that, all you have left is to write your code.
The smallest snippet of code that will allow you to see something looks something like this

import org.openqa.selenium.WebDriver;  
public class SeleniumMain  

 // download driver from :  
 // see more on :  
 public static void main( String[] args )  
  String webdriver_path = System.getenv( "" );  
  System.out.println( "webdriver_path = " + webdriver_path );  

  String property = System.getProperty( "" );  
  System.out.println( "property = " + property );  

//  System.setProperty( "", webdriver_path );  

  WebDriver driver = new ChromeDriver();  

JQuery syntax like selenium code

I’ve been working with JQuery for quite a while now, and I find the css selector usage to be very useful.
In order to get something similar working with selenium, you can define some like the following :

public WebElement $( String cssSelector )  
 return driver.findElement( By.cssSelector( cssSelector ) );  

And then you can invoke code like

$("input#username").sendKeys( "my_username" );  
$("input#password").sendKeys( "my_password" );  

You can also wrap “WebElement” to get more JQuery like API ( for example use val() instead of “sendKeys” ).


← PreviousNext →