Closed WaterLoran closed 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:
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
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
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
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.
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
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
Your options are either:
--rs
to reuse the existing browser session (which won't close the driver from the previous test).--pdb
so that the test pauses with a breakpoint()
if anything fails.--user-data-dir=DIR
so that the previous session data is reused.--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
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?
You can always call a Python breakpoint()
to pause the script.
Or use the Driver()
format, where you have more control over some things.
I hope this script can use the previous browser Background of the problem: