Clean Coding Practices

Angie Jones

http://angiejones.tech

http://TestAutomationU.com

@techgirl1908

Senior Developer Advocate

Applitools, San Francisco, CA, USA

for Test Automation

I made a mess

@techgirl1908

a

CODE SMELL

is an implementation that violates fundamental design principles in a way that may slow down development and increase the risk of future bugs.

@techgirl1908

APPLICATION

UNDER

TEST

@techgirl1908

SMELLY

CODE

BASE

@techgirl1908

WORKSHOP FORMAT

@techgirl1908

1. Review a code smell

2. Review an example

3. Discuss what makes it a smell

4. Discuss how to refactor

5. Refactoring exercise

6. Review

@techgirl1908

LONG CLASS

 

 

 

The purpose of the class is multifold.

To find anything, you must scroll and scroll.

@techgirl1908

@techgirl1908

ISSUES

@techgirl1908

1. No single responsibility

2. Hard to find things

3. Difficult to maintain

@techgirl1908

@techgirl1908

@techgirl1908

EXERCISE 1: SEPARATE CONCERNS

 

  • Create two sections within project (main and test)
     

  • Create a Page Object class called SearchPage and add a constructor: SearchPage(WebDriver driver)
     

  • Move browser logic from SearchTest into a method in SearchPage: public boolean search(String title)

 

@techgirl1908

LONG METHOD

 

 

 

This guy does it all!

But it makes it hard to know when to call.

@techgirl1908

ISSUES

@techgirl1908

1. No single responsibility

2. Confusing for callers

3. Difficult to read and understand

4. Naturally grows bigger

@techgirl1908

@techgirl1908

WE HAVE A PROBLEM!

 

How come our second test cannot utilize the search method?

@techgirl1908

EXERCISE 2: SPLIT METHODS

 

  • Revisit the search method in SearchPage and split it into 3 methods:

    public void search(String title)
    public boolean isBookVisible(String title)
    public void clear()

     

  • Update SearchTests to use isBookVisible for verification

 

@techgirl1908

DUPLICATE CODE

 

 

 

 

When you find yourself coding in haste, it's very tempting to copy and paste.

@techgirl1908

ISSUES

@techgirl1908

1. Any change needed has to take place in multiple spots

2. Can lead to other smells

@techgirl1908

@techgirl1908

EXERCISE 3a: REMOVE DUPLICATION

  • Revisit the methods in SearchTests

    • make the WebDriver and SearchPage objects global

    • move the driver logic to two new methods
      (one that will run before the tests and one that runs after)


  • Add @BeforeClass annotation to the method that runs before test, and @AfterClass to the one that runs after


@techgirl1908

EXERCISE 3b: REMOVE DUPLICATION

  • Revisit the search method in SearchPage and make the searchBar element a global object

      By searchBar = By.id("searchBar")


     

  • Replace the duplicate instances of the locator within the
    search method with the new global object

@techgirl1908

FLAKY LOCATOR STRATEGY

 

 

 

 

At this moment,

your locators work fine.

But can they

stand the test of time?

@techgirl1908

 

Copy Xpath:

//*[@id="loginPanel"]/form/div[1]/input

 

ISSUES

@techgirl1908

1. Fragility

2. Unreliability

@techgirl1908

 

Reliable locator:

input[name="username"]

 

@techgirl1908

EXERCISE 4: STABILIZE LOCATORS

 

  • Revisit the isBookVisible method in SearchPage

    • The pid* points to one specific book, and could very well be dynamic.

    • By inspecting the SUT, find a more suitable locator using xpath or css that locates all visible books.
       

  • Save the new locator as a global By object

@techgirl1908

INDECENT EXPOSURE

🛀

Too many see you.

Your scope is too wide.

All who should know you

are the ones inside.

 

@techgirl1908

ISSUES

@techgirl1908

1. Violates encapsulation (fields)

2. Allows tests to directly access and manipulate the DOM, which is not their responsibility

@techgirl1908

@techgirl1908

EXERCISE 5: NARROW SCOPE

 

Review all code and set access modifiers to the appropriate levels

@techgirl1908

INEFFICIENT WAITS

 

 

 

 

The speed of machine

is faster than man

so you slow it down

any way you can.

@techgirl1908

ISSUES

@techgirl1908

1. Slows down runtime

2. Different environments may require different wait times

@techgirl1908

@techgirl1908

EXERCISE 6: WAIT INTELLIGENTLY

  • In SearchPage, replace the hard-coded wait statements with WebDriverWait statements.

    Hint: your code should use 'hidden books' in the expected condition

     

  • Overload the search method to accommodate for cases where your search rightfully returns all books

@techgirl1908

MULTIPLE POINTS OF FAILURE

 

 

 

 

The framework's job

is to force state,

not to determine

a test's fate.

@techgirl1908

ISSUES

@techgirl1908

1. Violates single responsibility

2. Limits reusuability

@techgirl1908

@techgirl1908

EXERCISE 7: INCREASE FLEXIBILITY

 

  • In SearchPage, refactor the isBookVisible method to remove assertions

 

@techgirl1908

REVIEW

@techgirl1908

DISCUSSION

Clean Coding Practices

Angie Jones

http://angiejones.tech

http://TestAutomationU.com

@techgirl1908

Senior Developer Advocate

Applitools, San Francisco, CA, USA

for Test Automation