seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
4.46k stars 910 forks source link

I hope this script can use the previous browser #2679

Closed WaterLoran closed 2 months ago

WaterLoran commented 2 months ago

I hope this script can use the previous browser Background of the problem:

  1. I need to debug UI scripts frequently, but selenium base is too slow to pull up the browser, which takes about 5 seconds
  2. Logging in to the tested software and loading the homepage can be time-consuming, sometimes taking 3 seconds My experience: At the slowest time, you need to wait for 8 seconds, so debugging the UI script will be slower and the work will not be coherent enough My research: In selenium, simply save the session's session ID locally and use it directly for the next retrieval However, in selenium base, after the script execution is completed, it appears that the session will be actively disconnected, meaning that all sessions will become invalid May I ask: Do you have any relevant implementation examples or ideas? I hope to be able to directly use the previous browser
mdmintz commented 2 months ago

For tests that use one of the pytest syntax formats: You can reuse the browser session by adding --reuse-session or --rs to your pytest run command. (That keeps the same session open for all tests.) If you want to close the session between different classes of tests, use --reuse-class-session or --rcs.

Duplicates:

WaterLoran commented 2 months ago

Hello, I carefully read the usage of the configuration parameters rs and rcs, but in reality, they did not meet my requirements His information is as follows --Rs | -- reuse session (Reuse browser session for all tests.) --RCS | -- reuse class session (Reuse session for tests in class.) These two parameters actually solve the reuse of the driver in the same pytest session However, my usage scenario is debugging automation scripts, rather than executing multiple pytest scripts at once Let me describe the specific scenario for you Firstly, I will use the pytest command to execute the current script, Then the script execution failed, Then I modified the pytest script and re executed it, At this point, I expect him to be able to directly connect to the driver applied for during the previous script execution

WaterLoran commented 2 months ago

I think this feature is very useful, Because when I'm debugging, initializing the browser takes a lot of time. For example, it takes 5 seconds to start the browser and then 5 seconds to log in, which results in a total of 10 seconds. Therefore, when I'm debugging UI automation scripts, it becomes very slow and inconsistent

WaterLoran commented 2 months ago

I would appreciate it if you could provide a function, such as def do_not_quit1and_closed_driver. The function is to set the configuration after obtaining the driver, and when the script is executed, it will not actively close that driver. The function will then return the corresponding session id or cookie information (which can be used for the next connection) In this way, I can encapsulate the information and write it to a local file. The next time the script executes, I can directly read the file and use this driver

mdmintz commented 2 months ago

You’re either looking for setting a user_data_dir so that you can access configuration from a previous session, or setting the remote-debugging-address and remote-debugging-port via chromium_arg so that you can connect to a previous session that’s already open, assuming you know the port number.

WaterLoran commented 2 months ago

But I found that in the pytest_runtest_teardown function you wrote, you would stop the current driver, so I won't be able to directly connect to this driver the next time I execute the script So I would like to ask if there is any method provided that can be set to not close the driver after executing the UI script

WaterLoran commented 2 months ago

Yes, you're right about my idea, but I still need to know how to set it to not stop this driver Because you disabled the driver by default in the teardown hook function, I am not sure how to modify your code before I have fully read it

mdmintz commented 2 months ago

Your options are either:

WaterLoran commented 2 months ago
--rs | --reuse-session  (Reuse browser session for all tests.)    ==>  This theory is not feasible because it is limited to one session, and my demand is to reuse a driver in multiple sessions

--pdb ==> I've tried this before, but maybe I'm not very lucky. I happened to encounter a scenario where I couldn't use it, so it was considered a failure

--user-data-dir=DIR ==> Thank you for providing this method. I will try it out, but it's already 11:30 pm. When I start working during the day, I will check and try again

Based on your reminder, I have a new idea, which is to write a service in Python that will automatically apply for a driver and maintain it in a driver pool. Then, when debugging the script, I can directly apply for an already initialized driver and use it directly After all, my usage scenario is debugging, maybe I could really do that

WaterLoran commented 2 months ago

Well, I have followed the fourth method to implement it, but it still hasn't been very smooth to use In terms of usage, it still lacks some smoothness So, I would like to ask, is there any method or function in selenium that can prevent it from stopping the driver and session during the teardown process or the corresponding hook function?

mdmintz commented 2 months ago

You can always call a Python breakpoint() to pause the script.

Or use the Driver() format, where you have more control over some things.

See Syntax Formats - Driver().