selenium icon indicating copy to clipboard operation
selenium copied to clipboard

[🐛 Bug]: toString() method of Cookie.class outputs expiry time in 12-hour format with no AM/PM indication

Open ricky-murphy opened this issue 1 year ago • 2 comments

What happened?

REPRODUCTION STEPS

  1. In the afternoon, using Selenium with any WebDriver, load any website that sets cookies that expire
  2. Print the output of the toString() method for the cookies.

EXPECTED RESULTS Expiry date is printed in either 24-hour format or a 12-hour format with an AM/PM indicator

ACTUAL RESULTS Expiry date is printed in a 12-hour format with no AM/PM indicator.

IMPACT What this means is that an expiry time of "4:23:21 PM EDT" gets printed as "04:43:21 EDT" - implying "04:43:21 AM EDT".

This can make debugging interesting, and intermittently breaks use cases that use the toString() method to save cookies. But works fine in the morning. Doing a Google search for

Selenium Cookie.toString()

reveals many examples like this where that method is used to save cookies. This will result in valid cookies being marked as expired in the afternoon.

There are multiple workarounds

  • Don't use toString(), build the string manually using the getters
  • Use toJson() to save cookies

ROOT CAUSE/FIX The toString() method of common/src/java/org/openqa/selenium/Cookie.java formats the expiry as "EEE, dd MMM yyyy hh:mm:ss z"

Least disruptive fix would likely be to switch the hour format from "hh" (Hour in am/pm (1-12)) to "HH" (Hour in day (0-23))

Fixed code would look like : "; expires=" + new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").format(expiry))

How can we reproduce the issue?

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.time.LocalDateTime;


public class CookieTest2 {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();

        String website = "https://www.whatarecookies.com/cookietest.asp";

        driver.get(website);
        System.out.println("Current System Time: " + LocalDateTime.now());
        System.out.println(driver.manage().getCookies());

        driver.quit();
    }
}

Relevant log output

> Task :CookieTest2.main()
Current System Time: 2024-05-10T16:23:25.297501900
[dta=vcount%3D0%2Cprev%3D1715372601980; expires=Fri, 10 May 2024 04:43:21 EDT; path=/; domain=www.whatarecookies.com; sameSite=None, _ga_6QRS5RXYHS=GS1.2.1715372602.1.0.1715372602.0.0.0; expires=Sun, 10 May 2026 04:23:22 EDT; path=/; domain=.whatarecookies.com; sameSite=None, ASPSESSIONIDQETBTRSA=IPDOBBMCMKEMPGBEFJCNILOG; path=/; domain=www.whatarecookies.com;secure;; sameSite=None, FCNEC=%5B%5B%22AKsRol8zQbsSlVT7FIYKPVyOkjeIWs0UmAoO3Vdw8_CYei6Os22DCy6l1F_-BWa-FgyFXgU1ahtGZYZvFc74ws1b60OpPER7La69NMg-TJw0YlQDKrxJIVxb_HoODUuS8bKNjaaf5jFUDZEXvYX86_u9A8gPixTHqg%3D%3D%22%5D%5D; expires=Sat, 10 May 2025 04:23:25 EDT; path=/; domain=.whatarecookies.com; sameSite=None, _gat=1; expires=Fri, 10 May 2024 04:24:22 EDT; path=/; domain=.whatarecookies.com; sameSite=None, __gpi=UID=00000e05819e8840:T=1715372603:RT=1715372603:S=ALNI_MYA2y9P0tO5Clam7UbMNoaPoyb_sw; expires=Wed, 04 Jun 2025 04:23:23 EDT; path=/; domain=.whatarecookies.com; sameSite=None, _ga=GA1.2.691575010.1715372602; expires=Sun, 10 May 2026 04:23:22 EDT; path=/; domain=.whatarecookies.com; sameSite=None, __eoi=ID=1a500bf16b1c5e8b:T=1715372603:RT=1715372603:S=AA-AfjbH5vPECkfZCyNOs1SCv0Lh; expires=Wed, 06 Nov 2024 03:23:23 EST; path=/; domain=.whatarecookies.com; sameSite=None, cookieconsent=2AR; expires=Fri, 09 May 2025 08:00:00 EDT; path=/; domain=www.whatarecookies.com; sameSite=None, __gads=ID=d9f42925dc75504e:T=1715372603:RT=1715372603:S=ALNI_MZxL317YiDN0mcUVRErzD95e4O15w; expires=Wed, 04 Jun 2025 04:23:23 EDT; path=/; domain=.whatarecookies.com; sameSite=None, _gid=GA1.2.227887166.1715372602; expires=Sat, 11 May 2024 04:23:22 EDT; path=/; domain=.whatarecookies.com; sameSite=None]

Operating System

Windows 10

Selenium version

4.20.0

What are the browser(s) and version(s) where you see this issue?

Firefox 125.0.3, likely affects all browsers

What are the browser driver(s) and version(s) where you see this issue?

FirefoxDriver, likely affects all drivers as the cookie class is common to all of them. expiry was added to toString() method in 2.34.0

Are you using Selenium Grid?

No

ricky-murphy avatar May 10 '24 20:05 ricky-murphy

@ricky-murphy, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] avatar May 10 '24 20:05 github-actions[bot]

I think the timezone must be GMT to match the HTTP date format.

joerg1985 avatar May 10 '24 21:05 joerg1985

@ricky-murphy The format and the timezone is fixed with d1b84e4, thanks for reporting!

joerg1985 avatar May 13 '24 17:05 joerg1985

This issue has been automatically locked since there has not been any recent activity since it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jun 12 '24 22:06 github-actions[bot]