Cucumber With Spring Boot
Spring
● Donec risus dolor porta venenatis
Lorem ipsum dolor sit amet at
01 Most Popular Java Framework
nec at adipiscing
●
●
Pharetra luctus felis
Proin in tellus felis volutpat
● Donec risus dolor porta venenatis
Lorem ipsum dolor sit amet at
02 Mature & Lightweight
nec at adipiscing
●
●
Pharetra luctus felis
Proin in tellus felis volutpat
● Donec risus dolor porta venenatis
Lorem ipsum dolor sit amet at
03 Inversion Of Control
nec at adipiscing
●
●
Pharetra luctus felis
Proin in tellus felis volutpat
04 Dependency Injection
Spring Boot
● Donec risus dolor porta venenatis
Lorem ipsum
Provides Rapiddolor sit amet at
Access
01 nec at adipiscing
Development to Spring Framework
●
●
Pharetra luctus felis
Proin in tellus felis volutpat
● Donec risus dolor porta venenatis
Lorem ipsum dolor sit amet at
02 Opinionated
nec at adipiscing
●
●
Pharetra luctus felis
Proin in tellus felis volutpat
● Donec risus dolor porta venenatis
Increased
Lorem ipsum
Developer
dolor sit amet at
03 nec at adipiscing
Productivity
●
●
Pharetra luctus felis
Proin in tellus felis volutpat
Demo
● A Simple MicroService development!!
Goal
● Let spring handle the routine task!
● Manage the webdriver instance
● Manage Page objects / fragments
● Executing tests in multiple test environments like dev / qa / stg / prod
● Localization Testing for multiple languages
● Data driven testing by loading a CSV file in to a runtime DB
● Accessing files
● Automatic window/frame switching
● Taking screenshots
● Parallel test execution
● Executing cucumber feature files with automatic dependency injection
Coverage
Integration with JUnit /
TestNG & Cucumber
Spring Boot In Test
Automation
Dependency
Injection
Prerequisites & Assumptions
Spring Boot 0 knowledge!!!
Java Should be very comfortable with Java 8
Selenium Should know basics.
Cucumber Not really required. Not an exhaustive
cucumber course.
JUnit / TestNG All the demos will be in TestNG / notes will
be there for Junit users.
Coverage
Spring Boot Basics No test automation examples. Just basics of Spring
Boot
Spring Boot with Selenium Managing WebDriver, Automatic Dependency
Injection of WebDriver, Page Objects etc
Executing tests in different env by using command line
- dev/qa/stg/prd
Spring boot bean scope Bean Scopes & Parallel test execution
Aspect Oriented Programming Window Switch / Take Screenshot just with custom
annotation
Accessing Files How to easily access files using Spring Boot
Spring Data JPA Data driven testing, Test data management, Querying
test data
Logging Logging, writing into a file
Cucumber Complete integration of Feature files, Spring Boot,
Parallel testing etc
Softwares Required
Java 8 or above
Maven 3.3 or above
Intellij / Eclipse / IDE
Spring Boot - Basics
Object Dependency
A B C
A B C
Object Dependency
TimeOut
Page Object
(wait for
element)
WebDriverWait
WebDriver
Path / URL /
Options
Tight Coupling
Loose Coupling
Passing the Dependencies
● Constructor
● Setter methods
● Field injection
Field Injection
Dependency Injection - @Autowired
● Injecting your application classes - @Component
Salary
User Test class
Address
Dependency Injection - @Autowired
@Component
Salary
@Autowired
@Component
User Test class
@Component
Address
Dependency Injection - @Value
● Primitive types
● Properties
● Environment Variables
● URL
● Arrays / Lists
● File / Path
● Default values
Constructor / Setter / Field Injection
Constructor Setter Field
Boilerplate code / verbose Boilerplate code / verbose Short & Neat
Less readable Less readable More readable
Easy to unit test with Easy to unit test with Works only with DI
custom parameters custom parameters frameworks
Immutable Mutable (Im)mutable
Page Object - Constructor Injection(!!)
External Libraries - Dependency Injection
● Classes from external libraries
○ @Bean
○ @Configuration
@Bean vs @Component
● @Component - Your classes (automatic configuration)
● @Bean - You explicitly create a spring bean by doing your own configuration
○ Why? - because we can not modify 3rd party classes source code. Instead we create an instance and request
spring to manage it for us!
● Both are Spring managed beans!
@Component/@Bean for all!?
Bean Lifecycle
Set Constructor & Field values destroy (DisposableBean)
setBeanName (BeanNameAware) @PreDestroy
setBeanClassLoader
(BeanClassLoaderAware)
setBeanFactory (BeanFactoryAware)
Bean is ready to be used!!! Enjoy your bean!
@PostConstruct
afterPropertiesSet (InitializingBean)
Inversion Of Control
Bean Bean
● Automatic object creation
● Method invocation Spring IoC Container
● Life cycle management
Bean
Summary
● Spring allows loose coupling by injecting the dependencies
● Spring boot is an extension for rapid development
● Constructor / Setter / Field injections
● Bean is a Java Object managed by Spring
○ @Component - for automatic configuration
○ @Configuration & @Bean for manual configuration
● @Autowired for spring beans
● @Value - from application.properties, environment variables etc
● Bean has a lifecycle
○ @PostConstruct
○ @PreDestroy
● Spring is IoC container
Spring Boot - Integration
With Selenium
WebDriver - Bean
● Bean
○ FirefoxDriver
○ ChromeDriver
Page Objects / Fragments
SearchComponent
WebDriver
WebDriver Config GooglePage GoogleTest
Chrome
SearchResult
WebDriver
Lazy Bean
● Beans can be lazy!
● @Lazy should be used along with @Component/@Bean & @Autowired
Unique Bean!
● Spring should be able to inject a bean uniquely!!!
Unique Bean!
● @Primary
● Conditions
○ @ConditionalOnProperty
○ @ConditionalOnExpression
○ @ConditionalOnMissingBean
● @Qualifier
WebDriver - Managed By Spring
chrome firefox
WebDriver - Managed By You!
Profile - Properties
spring.profiles.active=qa
application-qa.properties
application.properties
application-stg.properties
@Value
spring.profiles.active=stg
Profile - @Component / @Bean
DEV
QA
Bean
STG
PRD
Summary
● WebDriver Bean - Managed by Spring
● WebDriverFactory - Managed by Spring & You create the Driver
● Page Objects / Page Components can be autowired
○ Calendar widget - can be a spring component
● Remember - Spring Boot will crate all the beans before your test starts!
● Bean should be Unique
○ @Primary
○ Conditions
■ @ConditionalOnProperty
■ @ConditionalOnExpression
■ @ConditionalOnMissingBean
○ @Qualifier
○ @Profile
Summary
● Profiles can be used for environment specific details
○ URL
○ Username/Passwords
● Profiles can be combined
○ Remote + qa
○ Local + stg
● spring.profiles.active=remote,qa
● Bean can be @Lazy
○ Do not forget to add @Lazy in all the places @Autowired & @Bean/@Component
Assignments!
● Create a new page object by autowiring WebDriver for this site -
http://newtours.demoaut.com/mercurywelcome.php
● Practice to run the same test by passing browser / profiles via command line
Spring Boot - Bean Scope &
Parallel Testing
Bean Scope
● Singleton (default)
● Prototype
Singleton
● Only one instance of the Bean
● Shared with all the objects
● Shared with all the threads
● Good for Reporting / Logging
● Avoid mutating instance variables
● Your responsibility to keep it thread safe!
Singleton
JuniorEngg SeniorEngg Manager
Salary
s=100 s=150 s=200
s=200 s=200 s=200
Prototype
● Fresh instance for all!
● Note: @PreDestroy is not invoked!!
Prototype
Junior Senior Manager
s=200
s=100 s=150
s=100 s=150 s=200
Custom Annotation
● Group annotations and create custom annotations
WebDriver - Singleton
Thread 1 Thread 2 Thread 3
Page 1 webdriver webdriver
webdriver
Page 2 webdriver webdriver webdriver
WebDriver - Prototype
Thread 1 Thread 2 Thread 3
webdriver webdriver webdriver
Business
workflow
webdriver webdriver webdriver
WebDriver - Thread Scope!
Thread 1 Thread 2 Thread 3
webdriver
webdriver webdriver
Business
workflow
webdriver webdriver webdriver
webdriver webdriver webdriver
WebDriver - Thread Scope???
Thread 1 Thread 2 Thread 3
webdriver webdriver webdriver
webdriver webdriver webdriver
webdriver
Custom ThreadScope
ThreadLocal / Map
Thread 1
Thread 1
Thread 2
Thread 3
Thread 3
Thread 4
Application Context
Spring Container
Singleton Prototype
● Configured one time ● Configured every time with latest beans
available
@Autowired
Private Prototype prototype
Application Context
Spring Container
Singleton Prototype
● Configured one time ● Configured every time with latest beans
available
@Autowired
Private ApplicationContext ctx;
...
this.ctx.getBean(Prototype.class)
WebDriverFactory
Spring Container
Singleton Prototype
● Configured one time ● Configured every time with latest beans
available
@Autowired
Private ApplicationContext ctx;
...
this.ctx.getBean(Prototype.class)
Summary
● Bean Scope
○ Singleton
○ Prototype
○ ThreadScope - custom scope. We have to register ourselves!
● Parallel Browser Testing
● ApplicationContext
○ To get bean ourselves
○ getBean(“method”, WebDriver.class)
● WebDriverFactory - Managed by Spring & You
● Custom Annotations
○ We will do something cool with this!!
● Kelvin…………..!!!!
Spring Boot & Selenium -
Files & Properties
Accessing Resources
Prefix Example
classpath classpath:data/udemy.txt
file: file:c:/some/path/udemy.txt
http: http://somesite.com/udemy.txt
AWS S3 Resources
● We could easily access S3 resources as well
○ No additional code required
○ We need to include AWS core dependency
○ (You should have the permission)
ResourceLoader
● To get resources at runtime (similar to application context to get beans at runtime)
● To copy files
FileCopyUtils.copy(
resourceLoader.getResource(url).getInputStream(),
Files.newOutputStream(path.resolve(saveAs))
);
PropertiesLoaderUtils
● To load N number of property files at runtime!
@PropertySource
● To read any additional property files which are not part of application.properties.
● Instead of injecting values in multiple places, just inject once!
● Properties into a Java object!
Localization
Locale Properties
en-us.properties es.properties
label.username=Username label.username=Nombre de Usuario
label.password=Password label.password=Contraseña
@TestPropertySource
● To set test specific properties
Spring Boot & Selenium -
Aspect Oriented
Programming
Cross Cutting Concern
Features
Cross cutting
concerns
Aspect Oriented Programming (AOP)
Main Page Window 1 Window 2
Aspect Oriented Programming (AOP)
@Aspect
Main Page Window 1
Cross cutting
Aspect A class which handles the logic to A test class which contains before, after concern
implement the cross cutting concern test methods (Just for comparison
purposes)
Advice The action taken by the Aspect / a Before Test / After Test - steps to be
method in the Aspect performed
JoinPoint A point of execution / an event in the @Test method execution
program - like method invocation /
exception being thrown / field being
modified.
PointCut At which JoinPoint we need the @BeforeTest / @BeforeAll ….
advice!
AOP - Sample PointCut Expressions
When To Invoke @Before the method execution
@After the method execution
@Around the method execution
A target class with annotation @target(com.udemy.spring.springselenium.kelvin.annotation.Window)
A target class with annotation @target(window)
(here we assume that the method has a parameter called window. The type of the window
would be used as target class)
A method with the annotation @annotation(com.udemy.spring.Something)
A method with the annotation @annotation(something)
Complete Doc: https://docs.spring.io/spring/docs/2.0.x/reference/aop.html
Summary
● AOP
● Cross cutting concern
○ Separate core and secondary concerns
● Window Switch
● Frame Switch
● Taking Screenshot
● Logging
● Re-execute the method if the result is not as expected
● Do a data setup / make a REST call before method/after method invocation
Spring Boot - Data JPA
Spring Data JPA
● JPA - Java Persistence API
● Provides a nice abstraction to connect to various SQL/NoSQL Databases
● Do not write a single line of SQL (Hibernate lib)
● Select / Insert / Update / Delete
● Where / Aggregation
Spring Data JPA
H2
Spring Boot Postgres
MySQL
Spring Data In Testing
● Data Driven Tests
● Query test data
● Let spring handle routine activities!
● CSV → Table
Spring Data JPA - Basics
user
User
Entity
user_order
UserOrder
Spring Data JPA - Basics
User
user
UserRepository
Service / Tests user_order
UserOrderRepository
UserOrder
Spring Data JPA - Naming Conventions
DB Table / Column Name Java Class / Field Name
user_visa (table) UserVisa (class)
first_name firstName
last_name lastName
dob dob
Spring Data JPA - Summary
● JPA - Java Persistence API
● Provides a nice abstraction to connect to various SQL/NoSQL Databases
● CRUD w/o any SQL
● Data driven tests
● To treat CSV like a table.
Spring Data JPA - Query
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
Spring Data JPA - H2
H2
Spring Boot Spring Boot Postgres
csv spring.datasource.url=jdbc:postgresql://10.11.12.13:5432/appdb
spring.datasource.username=vinsguru
spring.datasource.password=admin
Spring Data JPA - DML
● To update
● To Delete
● To insert
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
Spring Data JPA - Curious to know more!?
Spring Boot & Selenium -
Logging
Logging
● We can create logger for any class.
● Default - console.output
Logging Levels
● ERROR
● WARN
● INFO
● DEBUG
● TRACE
Logger Properties
● logging.level.root=INFO
● logging.level.com.udemy=WARN
● logging.file.name=test.log
● logging.file.max-size=10MB
https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-logging.html
Spring Boot, Selenium &
Cucumber Integration
Behavior Driven Development
Behavior Driven Development
● Feature - a business functionality (Login feature / Search feature)
● Scenario - a specific case within the feature. Feature will have at least 1 scenario
○ Check for the successful login with valid user credentials
● Given / When / Then / And - Steps to form a meaningful sentence
○ Given I am on the google site
○ When I enter ‘spring boot’ as a keyword
○ And I click on the search button
○ Then I should see at least 2 results
● Scenario Outline - data driven scenarios with placeholders keyword resultcount
○ Given I am on the google site
○ When I enter <keyword> as a keyword Spring boot 5
○ And I click on the search button
○ Then I should see at least <resultcount> results selenium 3
● Gherkin - a plain english language to write feature files
Cucumber - Tags
-Dcucumber.options -Dcucumber.filter.tags=”@google”
To run only @google @google
either @google or @visa @google or @visa
@google scenarios which are also marked as @google and @visa
@visa
All scenarios which are not marked as @visa not @visa
Some advanced (@front or @backend) and (not @slow)
Cucumber - Data Table → UserRepository
Summary
● Integrated with Cucumber
● Dependency Injection in the Step Definitions
● Reporting
● Failed tests screenshots
● Parallel scenarios run
● Filtering tags
● Cucumber Runner & Command line execution
● Closing browsers
JVM Shutdown Hook
Spring Boot & Selenium -
Miscellaneous
Docker Integration
● You need to update the <build> section in the pom file with appropriate plugins
● CucumberRunner will NOT be used for running the tests.
● We would be using below command
● Parameterize glue, tags, threads, browser, profiles etc
java -cp spring-boot-selenium.jar:spring-boot-selenium-tests.jar:libs/* \
-Dbrowser=chrome \
io.cucumber.core.cli.Main \
classpath:features \
--glue com.udemy.spring.springselenium.bdd \
--tags "@google" \
--threads 2
Thank You!