Selenium - Stored Session Data - java

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

Related

Automating with Chrome headless browser, required OTP everytime

I am automating a application in selenium automation tool using java, with headless chrome.
My application needs OTP verification on first login in a computer or a new browser(chrome/mozila/IE). Once OTP verified on the machine or browser, from next time it will not request to enter OTP. Login is enough.
Since its headless browser which I am automating ,I thought I can give OTP and verify by entering OTP in console for first time and from next time it may not ask for OTP.
System.setProperty("webdriver.chrome.driver", constant.browserPathChrome);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setHeadless(true);
chromeOptions.addArguments("--enable-javascript");
chromeOptions.addArguments("--disable-gpu");
driver = new ChromeDriver(chromeOptions);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("my application url");
But chrome headless keep asking for OTP everytime, when I launch the site from same machine even after verifying it first time in my machine by providing the received OTP in console as input. But everytime I can not give OTP when automating. how to make it behave like other browsers? which capability I should add?
Phantom.js doesnt have this kind of issue, when I give OTP via console for the first time, it takes verifies and from next time it doesnt ask for OTP. but due to some other reasons I am not able to use pahntom.js. I want to use chrome headless.
Selenium will create a new instance of browser for each run, so it is valid that it asks OTP for each run.
One solution may be you can add a Chrome profile while initiating the browser instance, so that selenium will pick this profile instead of launching its default one.
chromeOptions.addArguments("user-data-dir=path_of_chrome_profile");
chrome profile path can be your local path or repository path

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);

How to Force Selenium to use Permanent Firefox Profiles with WebDriver?

I'm using Selenium Web Driver API with Java. Whenever I run Web Driver, it automatically creates a tmp Firefox Profile and executes all driver related code relative to the tmp profile.
My issue is not with the extra space this creates as asked in this question: How to stop Selenium from creating temporary Firefox Profiles using Web Driver? and I know I could call driver.quit to clear the resources used by the tmp profile.
Rather, my issue is I want to use the permanent Firefox Profile so that the next time I launch this FF profile I inherit all the cookies and cache of the previous profile. I know I can manually save and add cookies each time but this is tedious.
So, is there any way to force Firefox not to create a tmp profile and launch Firefox as if I was browsing normally using the permanent profile? I just want it to launch as if I was browsing like a normal user.
Thanks
Generally Selenium do not support cross-session cookies.
Most easy way is to use Serialization.
You need to create wrapper class around selenium's cookie and make it serializable. And create class CookiesManager where will be 2 methods: SaveSession() -- to save and RestoreSession() - to restore from serialized file.
Another way is to save some cookies information into some temp cookies file. Like.... Csv or XML.
Sample of this way you can see here: Keep user logged in - save cookies using web driver
but only for c#.
Using specific profile:
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("profileToolsQA");
WebDriver driver = new FirefoxDriver(myprofile);
to create some additional profile for firefox you need to run firefox profile manager by the following way: firefox.exe -p

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/

How do I rerun Selenium 2.0 (webdriver) tests on the same browser?

I am trying to use Selenium 2.0 (Webdriver) to implement a series of tests. Before these tests can run, I have to login into the application. Since the application is not my 'own' (testing api-built functionality), each test should not be logging into my application to run.
I would prefer to do the following:
Connect my webdriver tests to my open Firefox browser (already loggedin)
Run my webdriver projects with the same browser.
I understand that Selenium usually assigns a session id to its browsers. However, the current Java implementation of Selenium 2.0 driver does not make use of session id (maybe it does but I don't know where to find it. )
Can someone provide some direction on how to resolve my issue (existing browser and run multiple tests with Selenium 2.0 (java))? Any code provided would also be helpful. Thanks!
Here is what I have learnt:
Selenium 1: As Ioan suggested earlier, use "-firefoxProfileTemplate" when starting up the Selenium RC server and point to the location of your Firefox profile.
Selenium 2: I suppose you can use the Selenium 1 RC server, however, since Selenium 2 uses WebDriver, you can point to the profile information within your code.
File profileDir = new File("/Users/_____/selenium/FFprofile");
FirefoxProfile profile =
new FirefoxProfile(profileDir);
WebDriver driver = new FirefoxDriver(
profile);
Notes:
Make sure you run "firefox -profilemanager" to create your initial profile and save your login information.
Allow the browser/website to always store your authentication credentials avoiding "popup"/"login" wwindows, etcs.
Hope this helps somebody who may run into a similar issue: Using the same browser profile in Selenium, etc.

Categories