Over a million developers have joined DZone.

Using IntelliJ to Debug @Test With the 'Watch' Function

A practical tutorial on debugging a web application using the number one Java IDE according to a recent poll.

· Integration Zone

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

I was sent a query about why a cookies test was failing on Chrome but not on Firefox.

The debug process involved me using techniques that I don't think I have blogged about or demonstrated before, so I repeated the debug session for a video and have written it up here.

Essence:

  • Run test on Firefox to make sure it works.
  • Change Run configuration in IntelliJ to configure Chrome as the browser.
  • Breakpoints on relevant lines.
  • Add a watch that uses 'code,' not just a variable name.
  • Step through the code comparing my expectations with the actual values shown in the variables and watch view.

I have a 'Driver' class, which allows me to configure which driver I use via method call, or environment variables, or properties.

The fact that it uses properties means that if I have a run configuration in IntelliJ, I can add a '-D' parameter to the VM options to configure my browser

  • '-Dselenium2basics.webdriver=CHROME'

The 'watch' functionality in the debug view can be used to watch variables, but it can also be used to 'execute arbitrary code and watch the results' after each 'step' in the debug process.

I set up a 'watch' on 'driver.manage().getCookies()' to see the cookies retrieved and set by Selenium WebDriver on each step.

By comparing the cookies that I created with the cookies that were added, I could see that the cookie created by 'driver.manage().addCookie' through the ChromeDriver added a '.' before the domain in the cookie.

For example, I created a cookie with domain 'compendiumdev.co.uk,' and it was added to the browser as '.compendiumdev.co.uk.'

This led to the side effect where:

  • The JavaScript on the page could access the cookie just fine.
  • WebDriver could get the cookie just fine.
  • When the JavaScript on the page amended the cookie, it created a new cookie.

I have to perform a follow-up action to see:

  • If I need to fix the code on my test page, as the JavaScript might not be 'amending the cookie properly,' or it might have a bug deleting the cookie, etc., or...
  • If it is a bug in ChromeDriver (the different effect between FirefoxDriver and ChromeDriver suggests it might be a bug).

I don't know yet, but the debug process identified the cause of the side effect. Now, I have to work on the fix.

You can see the debug session in the video below, and the two blog posts listed below might be useful if you are debugging Selenium WebDriver and Java @Test code:



P.S. I decided to absorb the 'fix' into my test pages so that the cookie deletion code doesn't just expire it for the current domain, it also expires it for  '"." + document.domain:'

 document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
 document.cookie = name + "=" + cval + "; domain=." + document.domain + "; expires=" + exp.toGMTString();

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
debug ,configuration ,session ,selenium webdriver ,intellij

Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}