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

Selenium and self-signed SSL Certificates in Firefox [java / linux]

AAAARRGGHHHHHH!!!!!!!!….. Finally, I’ve managed to solve my problem with self-signed certificates in FF and Selenium.

I think I spend three days trying various methods, following tutorials and ‘hacking’ through the selenium source code, etc.
Maybe it’d take someone much less time than me, but what’s done is done, and let’s get crackin’ 🙂

A. Follow Girlie’s tutorial to create customized Firefox profile with few changes:

  1. in step no. 7 – remember not to use spaces in forlder names
  2. skip steps no. 11 & 12
  3. [OPTIONAL STEP] Configure FF according to Girlie’s suggestions
  4. Once you’re done with creating custom profile, go to the HTTPS site which troubles you with an invalid security certificate. Use “Add exception” button to add security exception.
  5. Close FF

B. Go to your FF custom profile folder (see A.1)

  1. If you skipped step A.3, then remove all files and folders except: cert8.db, cert_override.txt, key3.db
  2. If you followed Girlie’s suggestions in step A.3, then you can remove all files and folders except:
    • cert8.db, cert_override.txt, content-prefs.sqlite, key3.db, localstore.rdf, permissions.sqlite, prefs.js, secmod.db, XPC.mfasl, xpti.dat, XUL.mfasl
    • More information about contents of FF profile folder at: http://kb.mozillazine.org/Profile_folder_-_Firefox

C. Open your selenium project and add some new code:

private static Selenium selenium = new DefaultSelenium(String serverHost, int serverPort, String browserStartCommand, String browserURL);
private static SeleniumServer seleniumServer;
seleniumServer = new SeleniumServer();


/* 
* And here are the DRAGONS!!
* Short description:
* start with preparing new RemoteControlConfiguration and profile location
* then, change some default settings
* start selenium server
* and finally pass new configuration to Selenium 
*/

RemoteControlConfiguration rcc = seleniumServer.getConfiguration();
File profileLocation = new File("/path/to/your/custom/FF/profile");
rcc.setProfilesLocation(profileLocation);
rcc.setFirefoxProfileTemplate(profileLocation);
rcc.setReuseBrowserSessions(true);
seleniumServer.start();
selenium.start(rcc);

Hope this this work also for you.
J

Functional Test Automation

http://functionaltestautomation.blogspot.com/

[EN]
Above mentioned blog is dedicated to software functional test automation of web apps and not only. with special stress on selenium suite of tools, frameworks, methodologies and approach. It’s focused of usage of tools like: Selenium czy TestNG. I must say that in general author put quite a lot of his time for explore the area of web testing.  Concerning the blog essence you’ve to rate it yourself.


[PL]
Strona poświęcania automatyzacji funkcjonalnego testowania aplikacji webowych i nie tylko. Skupia się na wykorzysaniu narzędziach takich jak: Selenium czy TestNG. Generalnie widać, że autor zainwestował trochę czasu w zgłębienie tematu, co do merytoryki musicie ocenić sami.