How to safely end a program with a ChromeDriver - java

Currently I have the following issue:
my code is
WebDriver driver = new ChromeDriver();
driver.close();
But thoissometimes ens in an error.

Try using driver.quit(); Do not use close();
Actually, this is problem, especially if you are using Chrome. Try also sing Firefox.

Add driver.quit() to an #AfterClass method
Close will shut the current active window and if it is the last window will then perform a quit(), it does however need to have a valid active session to be able to do this.
If your test has failed that session is probably dead, so when you call a close it doesn't know where to send the command and doesn't do anything.
Quit will shut down all clients if there are no active sessions so if you send a quit and have no active sessions it will just clean up

Related

How do i stop the blocking of redirects in my chromedriver?

I have a scenario that clicks a button that performs a few redirects before landing on the intended page.
The issue is that these redirects get blocked within the Chromedriver session that is opened when the button is clicked.
Is there some sort of setting I can set for the Chromedriver so every time the script is run, the redirects don't get blocked?
Chrome version: 74.0.3729.157
Code tried :
options.addArguments("--disable-popup-blocking");
This as far as i'm aware will only disable the popup blocking, not redirects? I've also tried within the session manually preventing the blocking of the redirects, but the next time i run the scenario the redirects are blocked again
You can disable Safe Browsing in Chrome Settings:
Settings > Sync and Google services > Other Google services > Safe browsing
Or, try with this option
options.addArguments("--disable-web-security");
Try to add below to the driver, it works for me
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-web-security");
WebDriver driver = new ChromeDriver(options);

Selenium Using current session to re-open the browser

I am trying to open chrome, close it and then open it again using the same session.
Is a thing like this even possible? I've looked through the internet/stackoverflow and tried using driver.Close(); but with no luck.
Anybody has some experience with this and mind helping me?
thanks
In Selenium Webdriver, a browser session can be closed using two webdriver commands: close() and quit(). The situations in which they are used are briefly explained below:
close() is a webdriver command which closes the browser window which is currently in focus.
During the automation process, if there are more than one browser window opened, then the close() command will close only the current browser window which is having focus at that time. The remaining browser windows will not be closed. The following code can be used to close the current browser window:
driver.close() //where, ‘driver’ is the Webdriver object.
quit() is a webdriver command which calls the driver.dispose method, which in turn closes all the browser windows and terminates the WebDriver session.
If we do not use quit() at the end of program, the WebDriver session will not be closed properly and the files will not be cleared off memory. This may result in memory leak errors.
The following code can be used to close all the browser windows:
driver.quit() //where, ‘driver’ is the Webdriver object.
If the Automation process opens only a single browser window, the close() and quit() commands work in the same way. Both will differ in their functionality when there are more than one browser window opened during Automation.
Source: Reference link

Is there a way to check if web console (e.g. FireBug) is open in java language?

I am creating automated test cases with Selenium WebDriver 2. My code is set up, so that FireBug automatically is opened when Selenium WebDriver opens FireFox. A few seconds later I ask Selenium to close it again. For some reason, if I don't do this, sometimes FireBug won't be available to me at a later stage.
My problem:
When I use the TestNG suite to run my test suite, some of the test cases end up opening FireBug but not closing it again. My guess is, that this is due to existing browser windows already being open. The "close FireBug code" is then misused to open FireBug instead of closing it.
For this reason, I would like to make a check to see if FireBug is open, and if so, I would like to close it.
Opening code:
String firebugPath = TO_Constant.fireBug();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugPath));
profile.setPreference("extensions.firebug.showFirstRunPage", false);
profile.setPreference("extensions.firebug.allPagesActivation", "on");
driver = new FirefoxDriver(profile);
Code to close FireBug:
Actions action = new Actions(driver);
action.sendKeys(Keys.F12).build().perform();
I can find ways to do this in javascript, using window.console. Is there any way to do something similar in java?
console.log('is DevTools open?', window.devtools.open);
You can also listen to an event:
window.addEventListener('devtoolschange', function (e) {
console.log('is DevTools open?', e.detail.open);
});
It doesn't work when DevTools is undocked. However, works with the Chrome/Safari/Firefox DevTools and Firebug.
The answer is by Sindre Sorhus and has been taken from here!
Find out whether Chrome console is open

Selenium - Stored Session Data

I have search throughout the internet but none of the answers I found have a clear solution.
I am using selenium webdriver with Java.
My test needs to verify when you save your login information and close the browser and reopen it then those credentials remain and are saved on a new session. So I want to close the current session and reopen it in order to verify if a cookie persists on the page however Selenium deletes all stored session data so the test case will always fail. Is there any way to prevent Selenium from deleting the stored session data after closing the browser for the specific test case?
When I do run it I get a no such session error.
This solution works for chrome .
When you launch chrome (I think same applies for other browsers ) selenium creates a temporary profile and when you close it it deletes it. So when you relaunch the browser the new profile will not have the session information. What you can do is use profiles.
DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\"+System.getenv("USERNAME")+"\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(desiredCapabilities);
This way if you create your driver, every time chrome will be launched with the same profile and if you dont do
driver.manage().deleteAllCookies();
Your session information will persist in the new session of chrome driver. If you dont want it to persist you can clean the cookies using the above command or just simple log out. This should solve your problem. Similar things should also be possible in other browsers.
To know which profile is currently used type chrome://version/ in your address bar it has the information of the current profile being usesd. To know more about see this

Using same selenium webdriver object after a test suite has aborted

Can we continue with the same webdriver after a test execution has abruptly stopped.
I am currently using a webdriver reference in all test cases but need to reuse the same driver object afer i restart a test suite again.
After searching i got that we can get the driver's session id
Can we use the session id to reinitialize the driver
Also searched that it can be done with remote webdriver. But dont know How exactly it will work.
Please help.
you can certainly use the session id of the webdriver but for this you have to store the session id at the time of initializing in order to use it later.
for details see this blog post
For chrome browser
http://binaryclips.com/2015/08/25/selenium-webdriver-in-c-how-to-use-the-existing-window-of-chrome-browser/
For firefox browser
http://binaryclips.com/2014/09/16/selenium-web-driver-in-c-how-to-continue-script-on-the-already-opened-browser-instance/

Categories