Cucumber With Spring Boot
Cucumber With Spring Boot
Spring
04 Dependency Injection
Spring Boot
Dependency
Injection
Prerequisites & Assumptions
Spring boot bean scope Bean Scopes & Parallel test execution
Aspect Oriented Programming Window Switch / Take Screenshot just with custom
annotation
Spring Data JPA Data driven testing, Test data management, Querying
test data
Java 8 or above
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
Salary
Address
Dependency Injection - @Autowired
@Component
Salary
@Autowired
@Component
Address
Dependency Injection - @Value
● Primitive types
● Properties
● Environment Variables
● URL
● Arrays / Lists
● File / Path
● Default values
Constructor / Setter / Field Injection
Easy to unit test with Easy to unit test with Works only with DI
custom parameters custom parameters frameworks
setBeanClassLoader
(BeanClassLoaderAware)
setBeanFactory (BeanFactoryAware)
afterPropertiesSet (InitializingBean)
Inversion Of Control
Bean Bean
● Bean
○ FirefoxDriver
○ ChromeDriver
Page Objects / Fragments
SearchComponent
WebDriver
WebDriver Config GooglePage GoogleTest
Chrome
SearchResult
WebDriver
Lazy 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
● Singleton (default)
● Prototype
Singleton
Salary
s=100 s=150 s=200
s=200
s=100 s=150
Business
workflow
webdriver
webdriver webdriver
Business
workflow
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
@Autowired
Private Prototype prototype
Application Context
Spring Container
Singleton Prototype
Spring Container
Singleton Prototype
● 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
FileCopyUtils.copy(
resourceLoader.getResource(url).getInputStream(),
Files.newOutputStream(path.resolve(saveAs))
);
PropertiesLoaderUtils
● 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
Features
Cross cutting
concerns
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
● 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
H2
MySQL
Spring Data In Testing
User
Entity
user_order
UserOrder
Spring Data JPA - Basics
User
user
UserRepository
UserOrderRepository
UserOrder
Spring Data JPA - Naming Conventions
first_name firstName
last_name lastName
dob dob
Spring Data JPA - Summary
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
Spring Data JPA - H2
H2
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
● 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
-Dcucumber.options -Dcucumber.filter.tags=”@google”
● 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