Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.get_cookies() returns [] although .add_cookie() was called #279

Open
Ceravoux opened this issue Nov 7, 2024 · 2 comments
Open

.get_cookies() returns [] although .add_cookie() was called #279

Ceravoux opened this issue Nov 7, 2024 · 2 comments

Comments

@Ceravoux
Copy link

Ceravoux commented Nov 7, 2024

async def main():
    async with webdriver.Chrome(options=webdriver.ChromeOptions()) as driver:
        window = await driver.new_window("tab", 'https://github.com/kaliiiiiiiiii/Selenium-Driverless')
        await window.add_cookie({'name': 'foo', 'value': '123', 'domain': 'domain.com', 'path': '/', 'expires': -1, 'size': 3, 'httpOnly': True, 'secure': True, 'session': True, 'sameSite': 'None', 'priority': 'Medium', 'sameParty': False, 'sourceScheme': 'Secure', 'sourcePort': 443})
        print("window cookies",await window.get_cookies())

await window.get_cookies() returns []. Is this an expected behaviour?

@kaliiiiiiiiii
Copy link
Owner

kaliiiiiiiiii commented Nov 7, 2024

Uh yes I think this is expected, since you're adding it for a different domain than the one tou're on.
Though, I maybe I should either

  1. Change it to return all cookies
  2. implement get_all_cookies
  3. Implement for target, context & driver level

Can you confirm that the tests at https://github.com/kaliiiiiiiiii/Selenium-Driverless/blob/master/tests/pages/test_cookies.py pass in your env?

@Ceravoux
Copy link
Author

I'm afraid I have little idea about how to run the tests above, but as I did it:

D:\Codes\PY\cookiemonster\Selenium-Driverless\tests\pages\test_cookies.py:67
--------------------------------------------------- Captured stdout setup ---------------------------------------------------- 
======== Running on http://localhost:63276 ========
(Press CTRL+C to quit)
_____________________________________________ ERROR at setup of test_set_cookie ______________________________________________ 
file D:\Codes\PY\cookiemonster\Selenium-Driverless\tests\pages\test_cookies.py, line 90
  @pytest.mark.asyncio
  async def test_set_cookie(h_driver, subtests, test_server):
      url = test_server.url + "/cookie_echo"
      target = h_driver.current_target
      expires = datetime.datetime.utcnow() + datetime.timedelta(days=30)
      cookie = {'name': uuid.uuid4().hex,
                'value': uuid.uuid4().hex,
                'domain': 'localhost',
                'path': '/',
                'expires': expires.timestamp(),
                'httpOnly': False,
                'secure': False,
                'session': False,
                'priority': 'High',
                'sameParty': False,
                'sourceScheme': 'NonSecure',
                'sourcePort': test_server.port
                }
      cookies_received = await get_echo_cookies(target, url)
      with subtests.test():
          assert len(cookies_received.keys()) == 0
      await target.add_cookie(cookie)
      cookies_received = await get_echo_cookies(target, url)
      with subtests.test():
          assert cookies_received[cookie["name"]] == cookie["value"]

      get_cookie = (await target.get_cookies())[0]
      for key, value in cookie.items():
          value_got = get_cookie[key]
          with subtests.test(key=key, value=value, value_got=value_got):
              assert value == value_got
E       fixture 'subtests' not found
>       available fixtures: _session_event_loop, anyio_backend, anyio_backend_name, anyio_backend_options, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, driver, event_loop, event_loop_policy, h_driver, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, sync_driver, sync_h_driver, test_server, tests/pages/test_cookies.py::<event_loop>, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, unused_tcp_port, unused_tcp_port_factory, unused_udp_port, unused_udp_port_factory
>       use 'pytest --fixtures [testpath]' for help on them.
========================
tests/pages/test_cookies.py::test_set_cookie
  D:\Codes\PY\cookiemonster\Selenium-Driverless\tests\server_for_testing.py:95: DeprecationWarning: There is no current event loop
    asyncio.ensure_future(self.app.shutdown())     
    
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

Apparently, although .get_cookies() returns [], the cookies added by .add_cookie() still do their job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants