Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Saving and loading cookies for a future test case #1234

Answered by mdmintz
akunteseterwork asked this question in Q&A
Discussion options

Hello Sir,

I'm kinda new to automation. and I love how fast seleniumbase is and it's rich features.
But, let me straight to the question, I want to make (in this case) automation test for facebook, but I don't want to use the "setUp" to do the login everytime the def is called. I want to make it only login at first then in the next test case, i just need to hit the url without the need to pass the login detail and clicking Login.

So, i have this piece of code that i learn from your documentation & discussion that I think I'm stuck right now :D

from seleniumbase import BaseCase
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
class OverrideDriverTest(BaseCase):
 def get_new_driver(self, *args, **kwargs):
 """ This method overrides get_new_driver() from BaseCase. """
 options = webdriver.ChromeOptions()
 options.add_argument("user-data-dir=selenium")
 if self.headless:
 options.add_argument("--headless")
 return webdriver.Chrome(options=options)
class BaseTestCase(BaseCase):
 def setUp(self):
 super(BaseTestCase, self).setUp()
 self.open("http://facebook.com")
 self.type("#email", "myemail")
 self.type("#pass", "mypassword")
 self.click('login', by=By.NAME)
 def tearDown(self):
 time.sleep(3)
 super(BaseTestCase, self).tearDown()
class Case1(BaseTestCase):
 def test_01_search_marketplace(self):
 self.click("//span[contains(text(),'Marketplace')]")
 self.type(
 "//div[@class='bp9cbjyn j83agx80 hv4rvrfc dati1w0a']//input[@placeholder='Search Marketplace']", "Suzuki\n")
 time.sleep(3)

I've also tried with the self.save_cookies(name="cookies.txt") and self.load_cookies(name="cookies.txt") but i think i misunderstood about how to use it. I place the save_cookies after the login step, but when after i successfully save the cookies.txt i can't load the cookies by placing the load_cookies before the url, it gave me bunch of error.

Could you please explain to me what did i do wrong, or how i am supposed to do with the code to achieve my goal?
Thanks ;)

You must be logged in to vote

Instead of doing that, run pytest using --reuse-session (or --rs), and have only your first test perform the login. You can mark a test to go first by adding @pytest.mark.run(order=1):

import pytest
from seleniumbase import BaseCase
class MyTests(BaseCase):
 @pytest.mark.run(order=1)
 def test_aaa(self):
 ...

While reusing a session with --reuse-session, all tests will run in the same browser window.

Replies: 1 comment 3 replies

Comment options

Instead of doing that, run pytest using --reuse-session (or --rs), and have only your first test perform the login. You can mark a test to go first by adding @pytest.mark.run(order=1):

import pytest
from seleniumbase import BaseCase
class MyTests(BaseCase):
 @pytest.mark.run(order=1)
 def test_aaa(self):
 ...

While reusing a session with --reuse-session, all tests will run in the same browser window.

You must be logged in to vote
3 replies
Comment options

The --rs did the trick for facebook case.

fb

But, how to properly save a cookies, then load it for maybe next day, for example whatsapp web, or ecommerce website that always need an OTP on every automation browser.
I find it chrome_options.add_argument("user-data-dir=selenium") from selenium to be helpful, but i don't know how to combine with seleniumbase.
can you please help? is that even possible?

Comment options

Here are the methods for saving and loading cookies to and from a file:

self.save_cookies(name="cookies.txt")
self.load_cookies(name="cookies.txt")

As for using a user-data-dir, here's an example of that:

pytest --user-data-dir=my_data_dir
Comment options

My God, turns out it's just as simple as putting extra command for the run script.
Sorry for my noob question, but this is great for a starter like me,

For everyone who's reading this and have the same question, just type pytest yourscript.py --user-data-dir=data --rs :

  • --user-data-dir=data will create a data folder in the same folder as your script, this folder contains all the data just like your normal chrome data (including cookies of course) for future test purpose.
  • you need to run your script with login detail for the first time to generate cookies, and then you can delete or just comment it for the 2nd run, then you good to go
  • --rs is just command for reuse session just as Mr. Michael mentioned.

Thank you Sir, thank you so much for this (y)

Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /