I am using ChromeDriver for Selenium in Java.
I want to leave the browser open after the test is complete. The default behavior is to close the browser.
I have tried the following
options.setCapability("detach", true);
driver = new ChromeDriver(options);
or
options.setExperimentalOption("detach", true);
Neither seems to work. What is the correct way to use this in Java?
According to detach the property description
If false, Chrome will be quit when ChromeDriver is killed, regardless of whether the session is quit. If true, Chrome will only be quit if the session is quit (or closed). Note, if true, and the session is not quit, ChromeDriver cannot clean up the temporary user data directory that the running Chrome instance is using.
which means (AFAIU) that this only controls if you want to keep your browser open or not in case when your WebDriver process is unexpectedly terminated. This does not cover your particular case.
You can just leave your driver active after the test has completed (not quit it) and create new WebDriver object for each new test. However this is not effective approach from performance standpoint.
Related
I am trying to find a way that enables me to hide the driver (bot) of the selenium in VBA during the execution of the code and display it at end
I can hide it using this line
bot.AddArgument "--headless"
But how I restore it?
No, it won't be possible to make Chrome operate initially in headless mode and then switch back to normal mode within the same session.
When you configure an instance of a WebDriver with Options() to span a new Browsing Context the configuration gets baked within the driver executable which will persist for the lifetime of the WebDriver and being uneditable. So you can't modify/add any existing/new configuration through Options() class to the WebDriver instance which is currently in execution.
Even if you are able to extract the Driver and Session attributes e.g. Session ID, Cookies, UserAgent and other session attributes from the already initiated Driver and Browsing Session still you won't be able to change the set of attributes of the Driver.
You can find more details in these discussions:
How to set selenium webdriver from headless mode to normal mode within the same session?
Change ChromeOptions in an existing webdriver
How do I make Chrome Headless after I login manually
Credits to undetected Selenium
I am using Firefox and the Selenium IDE plugin. I have a test for the login of my application. After the end of the test the browser stays logged in and the session gets reused. Can this be avoided ?
You need to invoke driver.quit() method within the tearDown() {}. Invoking quit() DELETEs the current browsing session through sending "quit" command with {"flags":["eForceQuit"]} and finally sends the GET request on /shutdown EndPoint.
References
You can find a couple of relevant detailed discussions in:
Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?
PhantomJS web driver stays in memory
Why is the code //RemoteWebDriver driver= new FirefoxDriver(); not used instead of //WebDriver driver= new FirefoxDriver() to create a driver object?
I feel that RemoteWebDriver gives more capabilities for the driver instance than webdriver reference. Can someone clarify this?
WebDriver will start up a web browser on the computer where the code instantiates it. For example. If you write a bit of code, and then run it to see how you are doing, the browser will pop up on your screen and you will see WebDriver begin to manipulate that web browser window (if everything went well!)
With a major exception which I will explain below, RemoteWebDriver will do the same thing; it will open and manipulate a browser window (if everything went well!) Generally speaking, You can actually switch the instatiation of a WebDriver with a RemoteWebDriver (well, there are advanced cases where you might not be able too do this) The major difference is that RemoteWebDriver sends that request to open and control a web browser to a server, so you normally wouldn’t see the browser open and do it’s thing.
Selenium server is the program that runs and waits for RemoteWebDriver connections. You can run it on your local computer to test it out. If you get it set up and running, you’ll be able to create a RemoteWebDriver and see that the Selenium server accepts the connection and allows you to control the web browser window.
The gains from using RemoteWebDriver?
If you can do connect to a local Selenium server, you can be confident that you have the knowledge and skills needed to connect to a remote Selenium server, or even to a paid service like SauceLabs (Hosting Selenium for you) that allows you to run lots of tests on lots of OS’s and lots of browsers without having to actually maintain or install any of them (Linux, Windows 8, Windows 10, MacOS, Andriod, IOS, IE, Firefox, Opera, Safari, Firefox Mobile, etc) You’ll want to look into running tests asynchronously at this point. You don’t have to run them one at a time, so can test a large number of OS/Browser variations in a very short time.
What does it mean when something is used only for client/server communication?
When you uses a Selenium grid with have one hub and multiple clients, you invoke RemoteWebDriver through which you instantiate the server and and make the request to it.
WebDriver is an interface in selenium which extends SearchContext interface (super most interface in selenium)
Where as RemoteWebdriver is a class which implements WebDriver,
We can use RemoteWebdriver, when we going to execute the test in romote environment,(selenium grid).
WebDriver interface will invoke the driver locally,
Currently in automation mostly we are using WebDriver only.
Grid not using widely.
WebDriver driver=new ChromeDriver() ;
driver is the reference variable where used to access chromedriver class.
Using driver instance we can access all the unimplemented methods available in WebDriver interface, also able to access all the properties available in chromedriver class.
For more details
https://www.softwaretestingmaterial.com/webdriver-driver-new-firefoxdriver/
I am running Selenium automation test in one browser, but at the same time, I want to open the browser in another window and do something like checking mail, googling email then active mode or focus is coming to the current working window, not the automation test run browser.
Is it possible to work on the browser while automation test is run?
In general, when doing UI automation, you cannot use the test machine to do any other tasks that involve using the keyboard or mouse.
Since WebDriver automation performs keyboard and mouse input, such as typing text and clicking items, you will be constantly interfering by taking focus away from the WebDriver instance of the browser and doing your own mouse and keyboard interaction in other applications.
This will adversely affect both you and the automation, with neither being able to do what they want to do!
You should either use a separate test machine, or setup a virtual machine using software such as VirtualBox (free).
Did you try doing that?
Selenium uses WebDriver to communicate with a specific instance of a browser, not the currently focused window. So you should be able do continue to use other instances of browser windows. The best thing to do would be try.
If it isn't working, I would recommend getting a VM up and running and using that as your test environment. Generally that is the way I work to keep everything separate.
I ran my tests on Firefox and then used chrome on the side. Otherwise, run your tests on a remote machine.
You can do 2 things
1. Use a third tool to run test cases like Jenkins. so that test will run in memory.
2. If you are using firefox you can create a seperate firefox profile so that if you use firefox at the same time there should be any issue.
To Create new FF profile use below code:
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(false);
profile.setAssumeUntrustedCertificateIssuer(true);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new FirefoxDriver(dc);
I have a test where I need to do a login, close browser and open it again then check something on the page.
How can I do this when running webdriver with remote hub setup? or do i have to run this without remote?
Test is something like this:
Open browser
Login
Close browser
Open browser again
Check login is remembered
The process to accomplish this is going to be very similar to that of a solution in a non-grid environment. Note that the following code is written for Java, but I can't imagine C# being much different.
WebDriver driver = new RemoteWebDriver("hubURL", desiredCapabilities);
driver.manage().deleteAllCookies();
driver.get("http://path/to/page");
//login to application
driver.quit(); //This will close the browser on the remote machine
//Now to try it again
driver = new RemoteWebDriver("hubURL", desiredCapabilities);
driver.get("http://path/to/page");
Assert.assertTrue(some element that is not on the login page but is on the page after is present);
driver.quit();
Presumably you're testing some cookie stuff. Unfortunately, there's no guarantee any particular node will execute any request, unless you constrain it properly. You need to have a node advertise a unique capability that the client then requests, ensuring the hub will route to that node every time. But, naturally, if that node goes down, you won't have any others that could service the request.