Is there some way to be able to access a variable that’s created upon Environment creation?
I basically want to have a Selenium session open that can be accessed when querying an API endpoint. My assumption is that I can create an environment and then specify a startup script that starts the driver and make the environment persistent with the toggle switch, but how am I able to access the Selenium driver variable?
I’m not even sure if it’s possible, but I’d like to avoid having to spin up new Selenium sessions every single time I need to make a query to the endpoint if I can.
Can I create a PSU variable and store everything in there?
I completely removed environments and variables from the equation and ended up using the $Cache environment. Each query executes a preliminary check and post query check on the Selenium session. This does slow down the query a bit, but it makes it infinitely more manageable for my use.
Do note I have some basic logic that I will need to do that will handle if there’s already a query being executed on this particular session. I will probably have a limit of say three sessions that can be ran at the same time so that way sessions aren’t being stepped on by other requests.
Here’s a basic example:
if (!$Cache:Driver){
$Driver = Start-SeDriver -Browser Firefox
$Cache:Driver = $Driver
$Cache:DriverStartedAt = Get-Date
}
else {
$Driver = $Cache:Driver
}
# Do stuff here
# Run this at the end of each query
if (((Get-Date) - $Cache:DriverStartedAt).TotalHours -gt 6){
# Adjust the timing as needed here
# This just removes stale sessions
Stop-SeDriver $Driver
$Cache:DriverStartedAt = $null
$Cache:Driver = $null
}
# If you have something to return, return it here
I will also being investigating whether or not this will work to just spin up the session on demand. It could barely make a difference in speed, I’m not sure, but in that case the session would just need to be spun up at the start of each query and you wouldn’t save anything in the $Cache environment.