Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Coding for Workarounds So You Know When They Are Fixed

DZone's Guide to

Coding for Workarounds So You Know When They Are Fixed

Alan Richardson talks about how handy the act of setting up your code to detect when workarounds have been fixed can be. Read on to see an example.

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

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;
}

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
webdriver ,practice ,tests ,code ,cookie ,methods ,selenium ,workaround

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}