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.
Join the DZone community and get the full member experience.Join For Free
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.
- 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
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:
- WebDriver could get the cookie just fine.
I have to perform a follow-up action to see:
- 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();
Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.