Coding for workarounds so you know when they are fixed

With most libraries you use to automate your work, you have to code workarounds.

I coded a workaround in my Selenium WebDriver tests because there was a bug in ChromeDriver which meant that cookies were created differently than other browsers.

When I upgraded to WebDriver 2.53.0 and ChromeDriver 2.21 I discovered I didn’t need my workaround anymore.

In the code I had one set of code for Chrome, and another set for other browsers:

// chromedriver has a bug where the domain is prefixed with '.' 
//therefore
// the app 'updates' a new cookie so when chromedriver
// returns the cookie it returns
// the first and not the updated one
if(Driver.currentBrowser()== Driver.BrowserName.GOOGLECHROME){
  aCookie = getCookieWithLargestValueNamed
               ("seleniumSimplifiedSearchNumVisits");
}else {
  aCookie = driver.manage().getCookieNamed(
                "seleniumSimplifiedSearchNumVisits");
}

The bug was 7499 and was fixed in the recent update to ChromeDriver.

How do I know it was fixed?

Because I had failing @Test methods.

Tests in error:
  changeCookieVisitsCountUsingCookieBuilder
  (com.seleniumsimplified.webdriver.cookies.
   CookiesExercisesTestWorkWithExtraSync): 
  Chrome cookie creation issue has been resolved
  changeCookieVisitsCount(
    com.seleniumsimplified.webdriver.cookies.
    CookiesExercisesTestWorkWithExtraSync): 
  Chrome cookie creation issue has been resolved

Why did they fail (the workaround was still valid)?

Because I had code that checked for that.

// This currently fails on Chrome Driver since
// ChromeDriver adds some extra cookies when it creates 
// a cookie with a domain
// https://code.google.com/p/selenium/issues/detail?id=7499
if(Driver.currentBrowser() != Driver.BrowserName.GOOGLECHROME){
  assertEquals("we added the cookie correctly", cookieCount, 
               driver.manage().getCookies().size());
}else{
  if(driver.manage().getCookies().size()==cookieCount){
    throw new RuntimeException(
                 "Chrome cookie creation issue has been resolved");
  }
// also Chromedriver now adds a '.' in front of the domain
}

I added extra code to throw an error, if the problem I was coding a workaround for went away.

I don’t know if that is a normal practice. But since I don’t really want workarounds in my code forever, and I likely won’t remember to check the release notes on every fix to see if I coded a workaround for a fix. This seems like a good workaround to check if workarounds are no longer good.

The code I was using to check for the largest value in a cookie, by the way, was:

private Cookie getCookieWithLargestValueNamed(String cookieName) {
    int largestCookieVal=0;
    Cookie largestCookie=null;
    for(Cookie aCookie : driver.manage().getCookies()){
        if(aCookie.getName().contentEquals(cookieName)){
            int cookieVal = Integer.parseInt(aCookie.getValue());
            if(cookieVal>largestCookieVal) {
                largestCookieVal = cookieVal;
                largestCookie = aCookie;
            }
        }
    }
    return largestCookie;
}

 

This entry was posted in Practices, WebDriver. Bookmark the permalink.

5 Responses to Coding for workarounds so you know when they are fixed

  1. I haven’t thought about this before. I’ve always relied on my memory to clean the workarounds, so this post gave me something to think about. Thank you for that.

  2. Ben Visser says:

    I Love it when People think a couple of steps ahead! This is a pattern I will practice myself, thank you for the voor for thought.

  3. Pingback: Testing Bits – 3/20/16 – 3/26/16 | Testing Curator Blog

Leave a Reply

Your email address will not be published. Required fields are marked *