More Related Content
Similar to SelConf_Final_Final.pptx (20)
SelConf_Final_Final.pptx
- 1. © 2022 Thoughtworks | Confidential
Functional Programming
in
Test Automation
-Narmadha & Sameena
1
- 2. © 2022 Thoughtworks | Confidential 2
Narmadha Ramasamy
Senior Consultant @Thoughtworks
linkedin.com/in/narmadha956/
narmadha956@gmail.com
Sameena Syed
Senior Consultant @Thoughtworks
linkedin.com/in/sameena-syed/
sameena9010@gmail.com
- 3. © 2022 Thoughtworks | Confidential
Functional
Programming
Functional Programming is a
programming style where we use Pure
functions extensively
3
- 4. © 2022 Thoughtworks | Confidential
● Functions as variables
● Functions as arguments
● Functions as return types
First class citizens – FUNCTIONS
4
- 5. © 2022 Thoughtworks | Confidential
Impure Functions
f(x)
Pure Functions
f(x)
Pure & Impure Functions
5
Same input Same output Same input
Output 1
Output 2
……….
- 6. © 2022 Thoughtworks | Confidential
Business Requirement
6
Hey Team !
Our app juice shop is ready,
can we have automated tests
to test different workflows
- 7. © 2022 Thoughtworks | Confidential
Functional Programming thinking
IMPURE FUNCTIONS (Actions)
PURE FUNCTIONS (Calculations)
DATA (Data about events, users etc)
7
- 9. © 2022 Thoughtworks | Confidential
Business use case
Buy juice items from juice shop
o Given user is logged in
o When cart is empty
o Then user should be able to select juice items and checkout successfully
9
We need to ensure checkout of juice items works as expected, as it’s the most sold type of item.
- 10. © 2022 Thoughtworks | Confidential
User Journey
1. Login to application with registered user
2. Get all items in Juice shop portal
3. Add juices to cart
4. Navigate to cart
5. Checkout items in cart
6. Select delivery address
7. Select delivery speed
8. Select payment method
9. Validate cart total and checkout
10
- 12. © 2022 Thoughtworks | Confidential 12
1. Launch application -> launchApplication()
2. Login to application with registered user -> login()
3. Get all items in Juice shop portal -> getAllItems()
4. Add juices to cart -> getJuiceTypeItems(), addJuicesToCart()
5. Navigate to cart -> navigateToCart()
6. Checkout items in cart -> checkoutCart()
7. Select delivery address -> selectAddress()
8. Select delivery speed -> selectDelivery()
9. Select payment method -> payAndContinue()
10. Validate total price -> assert.equal(totalPriceDisplayed,expectedTotalPrice)
11. Validate order confirmation -> getOrderConfirmation()
Method Mapping
- 13. © 2022 Thoughtworks | Confidential
Business Requirement
13
Hey Team !
Looks like we are getting
more data and business
flows, is our existing
automated tests are scalable
???
- 14. © 2022 Thoughtworks | Confidential
Map : Returns list of same length as input
Filter : Returns list reduced length as input
Reduce : Returns single object
Commonly used Higher Order Functions
14
- 16. © 2022 Thoughtworks | Confidential
Call Graph
17
expectedTotalPrice()
addJuicesToCart()
getJuiceTypeItems()
filter() map() reduce() slice()
getAllItems()
navigateToCart() checkoutCart()
selectDeliverySpeed() getOrderConfirmation()
BUSINESS LAYER
CART OPERATIONS
ITEM OPERATIONS
selectAddress() payAndContinue()
Array-Built Ins
- 17. © 2022 Thoughtworks | Confidential
Always separate out business rules as functions
Using call graph to design test case
Extract calculations from actions as much as possible
Keep data immutable
Avoid implicit input & output instead use function arguments & return statements
Refactor specific names to generic names
Duplicate code is code smell
Practices to Follow
18
- 18. © 2022 Thoughtworks | Confidential
New Requirement
19
Clients have decided to give
bonus points for cart price
above 999 !
Can we develop this asap
- 19. © 2022 Thoughtworks | Confidential
Currying
How to stop passing the same arguments repeatedly to different functions??
• Use currying
Transformation of a function with multiple arguments to a function with single arg and
returns a function that takes the other args
20
- 21. © 2022 Thoughtworks | Confidential
Call Graph
23
expectedTotalPrice()
addItemsToCart()
geRequiredItems()
filter() map() reduce() slice()
getAllItems()
navigateToCart() checkoutCart()
calcDeliveryCharges() getOrderConfirmation()
BUSINESS LAYER
CART OPERATIONS
ITEM OPERATIONS
selectAddress() payAndContinue()
Array-Built Ins
calcBonusPoints()
- 22. © 2022 Thoughtworks | Confidential
Dynamic systems
Distribution
Data heavy systems
Concurrent systems
FP is more suitable for
24
- 23. © 2022 Thoughtworks | Confidential
Other FP languages
25
• Elixir
• Erlang
• Common Lisp
• Haskell
• F#
• Clojure
• Elm
• Racket
• OCaml
• Idris
• PureScript
• Wolfram
• Scala
• Python
• Kotlin
• JavaScript
- 24. © 2022 Thoughtworks | Confidential
● Data immutability affects performance. If
performance takes a hit , then code will have to be
refactored
● Recursion is complex than loop iteration
Disadvantages
26
- 25. © 2022 Thoughtworks | Confidential
References
27
https://www.manning.com/books/grokking-simplicity
https://www.youtube.com/watch?v=BQ-9e13kJ58&t=2035s
https://www.youtube.com/watch?v=sCAR8ZPM6ew
https://dev.to/darkmavis1980/a-practical-example-on-how-to-use-currying-in-javascript-1ae9
Editor's Notes
- Business layer should only call cart layer