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 does work
- 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 by a 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 setup 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.
i.e. I created a cookie with domain `compendiumdev.co.uk` it was added to the brower as `.compendiumdev.co.uk`
This led to the side-effect that:
- WebDriver could get the cookie just fine
I have to perform a follow on action to see:
- if it is a bug in ChromeDriver (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:
- How to Investigate and Debug Selenium WebDriver Test Error
- How to debug Java with IntelliJ: breakpoints, evaluate expression, watches and variable view
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();