Spring Selenium Tests With Annotations

This post describes how to implement Selenium tests in Java. It is inspired from the post by Alex Collins, with annotations. The code is available on GitHub in the Spring-Selenium-Test directory. Some alternative and much lighter techniques are available to unit test a Spring MVC application. To unit test services, see here.
 
 
 
 
 
 
 

Page, Configuration & Controller

We create a simple page with ‘Hello World’:

<!doctype html>
<html lang='en'>
<head>
  <meta charset='utf-8'>
  <title>Welcome !!!</title>
</head>
<body>
  <h1>
   Hello World !
  </h1>
</body>
</html>

We keep our controller very simple:

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = 'com.jverstry')
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix('WEB-INF/pages/');
        resolver.setSuffix('.jsp');
        return resolver;
    }

}

and our controller too:

@Controller
public class MyController {

    @RequestMapping(value = '/')
    public String home() {
        return 'index';
    }

}

For Selenium Testing

We create a configuration for testing. It provides the URL to open the application locally. The application is opened with Firefox:

@Configuration
public class TestConfig {

    @Bean
    public URI getSiteBase() throws URISyntaxException {
        return new URI('http://localhost:10001/spring-selenium-test-1.0.0');
    }

    @Bean(destroyMethod='quit')
    public FirefoxDriver getDrv() {
        return new FirefoxDriver();
    }

}

We also define an abstract class as a basis for all tests. It automatically closes Firefox after the test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={ TestConfig.class })
public abstract class AbstractTestIT {

    @Autowired
    protected URI siteBase;

    @Autowired
    protected WebDriver drv;

    {
        Runtime.getRuntime().addShutdownHook(new Thread() {
           @Override
           public void run() {
                drv.close();
           }
        });
    }

}

And we implement a selenium test where we make sure our page contains ‘Hello World’:

public class SeleniumTestIT extends AbstractTestIT {

    @Test
    public void testWeSeeHelloWorld() {
        drv.get(siteBase.toString());
        assertTrue(drv.getPageSource().contains('Hello World'));
    }

}

The maven dependencies are the same as those described in Alex Collins‘s post.

Building The Application

f you build the application, it will open and close firefox automatically. The test will be successful.
 

Reference: Spring Selenium Tests With Annotations from our JCG partner Jerome Versrynge at the Technical Notes blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

2 Responses to "Spring Selenium Tests With Annotations"

  1. Alex says:

    Great post! (Though I would say that)

  2. sumit kher says:

    I am comparatively new to selenium and played a little with selenium IDE, and trying to do with selenium rc. I am baffled to use the scripting language there. Can you tell me what is the most used language in selenium across industries? And also I came across this course http://www.wiziq.com/course/12451-selenium-automated-web-browser-testing-for-web-applications os selenium automated web browser testing is this good? If someone does in Java and he joins a company where everyone does in ruby, then it’ll be a pain to learn ruby again. And also it would be great if you address any comparison about the available languages like (perl, python, ruby, java etc.) or tell me any other guidance would really appreciate help and also i would like to thank for all the information you are providing.

Leave a Reply


× two = 16



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.