Run your Selenium driven tests in parallel using TestNG.

Sometime ago I came across this post:
http://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/
And I thought it’d be good to have it in a repo to save you time setting up your own project.

Basically this example project runs JUnit tests in parallel using TestNG.
Tests are grouped by the browser in which they’re going to be executed (have a look at the TestNG XML Suite files in src/test/resources).
As you might have guessed to drive the browsers we use Selenium WebDriver 🙂
And of course you can run your tests locally or remotely using Selenium GRID.

You can find the repo here: https://github.com/kowalcj0/parallel-selenium-with-testng

And here’s a video showing this project in action 🙂

Replacing FF driver with Chrome in JBehave’s tutorial project

Hi All,

Recently I’ve started playing with JBehave or rather with their interesting extension JBehave-web

JBehave-Web is a collection of extension for JBehave that extend its capabilities in ways related to HTTP and the web.

As a starting point I tried running an example project available here: https://github.com/jbehave/jbehave-tutorial.
After getting bored with running all the tests in FF, I’ve decided to replace it with Chrome. Below is the short tut. describing how to do that 🙂

Steps to get you there:

  1. Download chrome driver
  2. Change the webDriver to a PropertyWebDriverProvider
  3. Set System Properties

Step 1)
Download chrome driver that suits your needs: http://code.google.com/p/chromedriver/downloads/list
Extract it wherever you want.

Step 2)
Change the webDriver from FirefoxWebDriverProvider to a PropertyWebDriverProvider that allows running your tests with Google Chrome.
Edit etsy-steps.xml located […]/jbehave-tutorial/etsy-stories-java-spring/src/main/resources/etsy-steps.xml
and replace a FirefoxWebDriverProvider bean with a PropertyWebDriverProvider:

<bean id="driverProvider" class="org.jbehave.web.selenium.PropertyWebDriverProvider" >
</bean>

Step 3)
To set System Properties, edit Maven’s etsy-stories-java-spring module config file: […]/jbehave-tutorial/etsy-stories-java-spring/pom.xml
Follow this xpath to add the new props in the right place:

/pom:project pom:build/pom:plugins/pom:plugin/ pom:executions/pom:execution[2]/pom:configuration

                <systemProperties>
                    <property>
                        <name>browser</name>
                        <value>chrome</value>
                    </property>
                    <property>
                        <name>webdriver.chrome.driver</name>
                        <value>filepath/to/the/chromedriver/you/extracted/in/step1</value>
                    </property>
                </systemProperties>

ps. in case you’ll encounter problems solving all the dependencies while running the demo project, just add repos listed below to your main project Maven config file: ([…]/jbehave-tutorial/pom.xml)

  <repositories>
    <repository>
      <id>jbehave-web-repo</id>
      <name>jbehave web repo</name>
      <url>https://nexus.codehaus.org/content/groups/snapshots-group</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>codehausSnapshots</id>
      <name>Codehaus Snapshots</name>
      <releases>
        <enabled>false</enabled>
        <updatePolicy>always</updatePolicy>
        <checksumPolicy>warn</checksumPolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
        <checksumPolicy>fail</checksumPolicy>
      </snapshots>
      <url>https://nexus.codehaus.org/content/groups/snapshots-group</url>
      <layout>default</layout>
    </pluginRepository>
  </pluginRepositories>

Cheers,
J

How to fall-back from WebDriver’s findElementBy() method to the one using native JS getElementBy* methods

Hi All,

If any of you ever encountered problems using WedDriver’s “By.*” selectors (especially in IE7 !!!! ) then you can find this post somehow useful/helpful 🙂

The idea is simple: Use native JavaScript selector’s to find desired element.
I know that this is not a perfect solution, but works for me 🙂

ps. Sorry for a crappy post title 🙂

Cheers,
J

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;



/** 
 * Can be used as a fall-back method to find element(s) on page using native JS selector.  
 * VERY USEFUL when buil-in WebDriver's selector doesn't work, especially with IE7 !!!!!!!!!!!!!!!!!!!!!!
 * 
 * Author				: Janusz Kowalczyk
 * Created Date			: 07Jul2011   
 * REVISION HISTORY
 * DATE				CHANGED BY				DESCRIPTION
 * 07Jul2011		Janusz Kowalczyk		Initial Creation  
*/ 
public class JShelper
{
	private JavascriptExecutor jsExec = null;
	private Object o = null;
	private String jsSelector = null;
	
	/**
	 * Class constructor that instantiates JavascriptExecutor 
	*/
	public JShelper(WebDriver webdriver)
	{
		this.jsExec = (JavascriptExecutor) webdriver;
	}
	
	/**
	 * Tends to find HTML element by using three native JavaScript methods:
	 * getElementById(), getElementsByName(), getElementsByTagName().
	 * As a result WebElement is expected.
	 * For more info please refer to docs:   
	 * http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html
	 * 
	 * @param byIdentificator Any unique ID (like: ID, Name, TagName) that gonna be used to find desired element, 
	 * by using one of the native JS methods: 
	 * getElementById(), getElementsByName(), getElementsByTagName()
	 * 
	 * @param foundElementsOffset If you expect to find more than 1 element, 
	 * then explicitly specify which one you want to get. 
	 * 
	*/
	public WebElement getElement(String byIdentificator, int foundElementsOffset)
	{
		String tmpSelector = null;
		
	
		tmpSelector = "document.getElementById('" 
			+ byIdentificator 
			+ "')[" 
			+ foundElementsOffset 
			+ "]";
		o = exec("return " + tmpSelector);
		if (o != null) 
			{ 
				jsSelector = tmpSelector; 
				return (WebElement) o; 
			}
		
		
		tmpSelector = "document.getElementsByName('" 
			+ byIdentificator 
			+ "')[" 
			+ foundElementsOffset 
			+ "]";
		o = exec("return " + tmpSelector);
		if (o != null) 
			{ 
				jsSelector = tmpSelector; 
				return (WebElement) o; 
			}
		
		
		tmpSelector = "document.getElementsByTagName('" 
			+ byIdentificator 
			+ "')[" 
			+ foundElementsOffset 
			+ "]";
		o = exec("return " + tmpSelector);
		if (o != null) 
			{ 
				jsSelector = tmpSelector; 
				return (WebElement) o; 
			}
		
		return (WebElement) o;
	}
	
	
	/**
	 * Retrieves the value of element's attribute
	 * For more information please refer to documentation:
	 * http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html
	 * 
	 * 
	 * @param attribute Any html attribute like: value, class, name, id etc.
	 * 
	 * @return Value of element's attribute
	*/
	public String getAttributeValue(String attribute)
	{
		String tmpCmnd = "return " 
			+ jsSelector 
			+ ".getAttribute('" 
			+ attribute 
			+ "');";
		
		return (String) exec(tmpCmnd);
	}
	
	
	/**
	 * Sets the new value of element's attribute
	 * 
	 * @param attribute Any HTML attribute like: value, class, name, id etc.
	 * @param value Attribute's new value 
	*/
	public void setAttribute(String attribute, String value)
	{
		String tmpCmnd = jsSelector 
		+ ".setAttribute('" 
		+ attribute 
		+ "', '" 
		+ value 
		+ "');";
		
		exec(tmpCmnd);

		assertIsSetTo(attribute, value);
	}
	
	
	/**
	 * Simple assertion to chech is current attribute's value is the same as expected
	 * 
	 * @param attribute Any HTML attribute like: value, class, name, id etc.
	 * @param expValue Expected attribute's value 
	*/
	public void assertIsSetTo(String attribute, String expValue) 
	{
		String currentValue = (String) getAttributeValue(attribute);
		
    	Assert.assertTrue(currentValue != null && currentValue.equals(expValue),
    			"Attribute's new value is different from exptected. Expected value: '" 
    			+ expValue 
    			+ "' Current value: '" 
    			+ currentValue 
    			+ "'");
    }
	
	
	/**
	 * Executes JS
	 * 
	 * @param script Executes JavaScript in the context of the currently selected frame or window. 
	 * For more information please refer to documentation:
	 * http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html
	*/
	private Object exec(String script)
	{
		return jsExec.executeScript(script);
	}
}

Use custom Chrome profile with Selenium WebDriver

Hi,

Today, I was struggling with webdriver and Chrome.
As I found out, currently there’s no built-in webdriver feature that allows start Chrome with custom profile.
Fortunetely I found short work-around 🙂

There’re only 2 steps to follow:
#1: Create custom Chrome profile
#2: Add chrome.switches to Chrome driver capabilities

Steps:
#1: To create new custom Chrome profile follow these instructions:
http://www.labnol.org/software/create-family-profiles-in-google-chrome/4394/

#2: Then add new capability to your

//....

capabilities = DesiredCapabilities.chrome();
String[] switches = {"user-data-dir=\\path\\to\\your\\custom\\chrome\\profile\\directory"};
capabilities.setCapability("chrome.switches", switches);
webDriver = new RemoteWebDriver(gridServerURL, capabilities);

//....

Hope you’ll find this useful 🙂

Cheers,
J