Initialize Tests Using Spring
Twist uses the Spring Inversion of Control (IOC) container for managing dependencies. Using an IOC container makes it possible to manage the life-cycle of objects and resources outside the scenarios. For example, the IOC container makes it easy to share a browser instance across multiple tests. Resources that are expensive to create, and that can be utilized across multiple tests, are ideal candidates for injection via IOC.
Please refer to the bundled example - Example Mingle Test Suite to see an example of how Spring is configured for use with Selenium.
Resources are managed at two levels (containers):
Suite Level Container
The Suite Level Container is created and destroyed once during the entire test run. This can be used to create and inject resources that are required across all the test runs . For example, if you would like a browser instance to be reused across a test suite, the suite level container would be a good place to create it. This container can be configured using applicationContext-suite.xml.
Scenario level container
The Scenario Level Container is created and destroyed each time a scenario is executed. This container can be used to create and inject resources and classes that are required during the execution of every scenario. This container can be configured using applicationContext-scenario.xml.
Twist loads applicationContext-suite.xml and applicationConext-scenario.xml from the project's classpath.
Examples
The following configuration allows your Workflows to use objects initialized in the container:
public class Login {
public Login(Authentication auth, Selenium selenium){
this.auth = auth;
this.selenium = selenium;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http://www.springframework.org/schema/lang org/springframework/scripting/config/spring-lang-2.0.xsd">
<!-- ========================================================== -->
<!-- SimpleBean should have a default no-args constructor. -->
<!-- ========================================================== -->
<bean id="simpleBean" class="com.foo.example.container.SimpleBean"/>
<!-- ========================================================== -->
<!-- An example of a bean that needs some constructor parameters -->
<!-- ========================================================== -->
<bean id="initializedUsingConstructor" class="com.foo.example.container.Authentication">
<constructor-arg type="java.lang.String" value="username"/>
<constructor-arg type="java.lang.String" value="password"/>
</bean>
<!-- ========================================================== -->
<!-- You can also have a bean that depends on another bean, via it's constructor -->
<!-- ========================================================== -->
<!--
public class NeedsSimpleBean {
public NeedsSimpleBean(SimpleBean simpleBean) {
// do something with simpleBean
}
}
-->
<bean id="beanDependingOnSimpleBean" class="com.foo.example.container.NeedsSimpleBean" autowire="autodetect"/>
<!-- ========================================================== -->
<!-- beans can be initialized using properties from property files -->
<!-- ========================================================== -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="locations">
<value>classpath:twist.properties</value>
</property>
</bean>
<!-- ========================================================== -->
<!-- You can also set system properties as part of initialization -->
<!-- ========================================================== -->
<bean id="systemproperty" class="com.thoughtworks.twist.core.SystemPropertyInitializingBean">
<property name="systemProperties">
<map>
<entry key="http.proxyHost" value="your.proxy.host"/>
<entry key="http.proxyPort" value="8080"/>
<entry key="http.proxyUser" value="proxyUser"/>
<entry key="http.proxyPassword" value="proxyPassword"/>
</map>
</property>
</bean>
<!-- ========================================================== -->
<!-- A bean can be created using a factory as well. -->
<!-- ========================================================== -->
<!-- Create and initialize the factory that'll create the bean. -->
<bean id="seleniumDriverFactory" class="com.thoughtworks.twist.driver.selenium.SeleniumRCDriver"
init-method="start" destroy-method="stop" lazy-init="true">
<property name="serverOptions" value="${selenium.server.options}"/>
<property name="browserLauncher" value="${selenium.browserLauncher}"/>
<property name="browserURL" value="${selenium.browserURL}"/>
</bean>
<!-- Create the bean using the factory 'seleniumDriverFactory' -->
<bean id="selenium" factory-bean="seleniumDriverFactory" factory-method="getTwistSelenium" lazy-init="true">
<constructor-arg value="${twist.selenium.enableImplicitWait}" />
<property name="highlight" value="${twist.selenium.highlight}"/>
</bean>
<!-- ========================================================== -->
<!-- Use a database to get or set test data. -->
<!-- ========================================================== -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
The bean definitions have some additional attributes:
| init-method | Called after the bean is created in order to initialize it |
| destroy-method | Called before the bean needs to be destroyed (typically when the container shuts down) |
| lazy-init | If this is set to 'true', the bean will be created only when it is required, setting this to false will mean that the bean is created when the container starts. |
