Browser Session testing with Selenium WebDriver - session

I have following scenario to automate :
User Logs in to a website. Navigates to a certain page on website
Save the url for that page.
Close browser
Open the browser again and navigate to the saved url.
User should be able to see the page without logging in.
I can automate first 3 steps. But when I open browser again in step 4, a new session is started and my user sees log in page.
I believe the web driver browser is in incognito mode. So session cookie is not stored.
Is there any way to automate this scenario using selenium web driver?

It's because every time when you invoke browser, selenium opens a new browser profile..so your cookies will be lost..
You can inject cookies for the second time..
Cookie ck = new Cookie("name", "value");
driver.manage().addCookie(ck);
Or you can use same browser profile for driver..
FirefoxBinary binary = new FirefoxBinary();
File firefoxProfileFolder = new File("/Users/xxx/work/xxx/selenium/src/test/resources/firefoxprofile");
FirefoxProfile profile = new FirefoxProfile(firefoxProfileFolder);
webDriver driver= new FirefoxDriver(binary, profile);

You have to quit the webdriver before launching the saved url.
At 3rd step write driver.quit(); and at 4th step write:
driver.get("<saved_url>");

Related

How to make "stay logged in" work in Selenium webdriver?

I am working in a website like https://jira.atlassian.com/ using firefox, after clicking the "log in" on the top right corner, i am taken to the next login page, where on the right of "log in" button, there is this "stay logged in" checkbox, already checked.
I tested it manually. It seems that after i log in successfully, if i do not exit the page by log out, but simply close the page, the next time i enter https://jira.atlassian.com/, i am already logged in. Ok, make sense.
However, if testing such using selenium webdriver, log in, close page, open page again, the above does not happen.
My question is what is the mechanism for this "stay logged in", is it the same as "remember me"? and how do i make it happen when using selenium webdriver?
Up till now my understanding is:
this happens because of cookie
cookie is stored in firefox profile
selenium start with a default profile that does not include cookie, and can not store cookie during the test
Is there a relatively standard solution for this problem, in java preferably?
Thanks,
As far as I am aware, each instance of loading a FF with Selenium opens a stripped browser with no history/cookies/extensions etc. It's as if you have just download FF for the first time and its a fresh copy. I am not sure how to solve this issue. Maybe if you keep the session open by not closing the browser down but log out manually and not closing down the browser. Or open a new tab and opening the same address.
you can try to store the cookies and apply them to your new firefox instance
get the cookies using:
Set<Cookie> cookies = driver.manage().getCookies();
and than apply them to your new webdriver instance:
for(Cookie cook : cookies) {
driver.manage().addCookie(cook);
}

selenium firefox webdriver - accessing session

I am already logged in to my googlemail account in Firefox browser with profile named: test_profile.
Using Selenium, I launch Firefox browser with profile test_profile, but I am seeing login page. Meaning, the session/cookies are not being used.
What am I doing wrong? Or what can be done so I can use my session.
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("test_profile");
WebDriver driver = new FirefoxDriver(profile);
driver.get("https://www.googlemail.com");
Thanks.

Get Firefox Profile Name at Runtime and Set preferrence

I have a scenario to handle where I am opening Firefox browser using Selenium Webdriver and each time it opens up a new profile. Code is written below:
WebDriver driver = new FirefoxDriver();
Each time it opens up a new profile which is something like : anonymous8958670169066009851webdriver-profile and profile name changes every time WebDriver opens up a Firefox browser. My intention is to get the profile name at runtime and set some preferences on it like handling unresponsive JS alert etc. Basically flow will look like something below:
WebDriver driver = new FirefoxDriver();
<Write some code here to get Firefox profile name>
<Set profile settings like **profile.setPreference("extensions.firebug.currentVersion", "1.8.1");>
Please help me in the second step i.e. Write some code here to get Firefox profile name if anyone already achieved something similar before.
You're approaching the problem backward. The correct thing to do is to create the FirefoxProfile object yourself, and using it in the constructor to the FirefoxDriver. The code would look something like this:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("extensions.firebug.currentVersion", "1.8.1");
WebDriver driver = new FirefoxDriver(profile);

How to relaunch firefox browser using WebDriver while retaining the previous session id

There is a bug in my app wherein Logout doesn't work. I need to workaround this issue in my automation that is in Java using WebDriver. The workaround is to close the browser and reopen it and open the Login page.
To automate this workaround, here is what I have tried:
browserDriver.quit();
browserDriver = new FirefoxDriver(capabilities);
browserDriver.get(loginPageURL);
This returns a new session id. Is there a way to retain the previous session id and set it back. I can get the previous session id using
((RemoteWebDriver)browserDriver).getSessionId();
I also tried deleting all the cookies for the current domain using the following code, but the user was still logged in.
browserDriver.manage().deleteAllCookies();
browserDriver.navigate().refresh();
browserDriver.get(loginPageURL);
Appreciate any help on this.
Up to my knowledge after calling the quit() method on driver it will not retain the previous session id.
Anyway try to launch the browser using specific firefox profile by disabling cache in that.
FirefoxProfile profile = new ProfilesIni().getProfile(profilePath);
profile.setPreference("browser.cache.disk.enable", false);
profile.setPreference("browser.cache.memory.enable", false);
profile.setPreference("browser.cache.offline.enable", false);
profile.setPreference("network.http.use-cache", false);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
driver = new FirefoxDriver(dc);
driver.get(url);
Firefox Profile creation ==>
https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles
Edit-I
Change below setting in that profile
In "about:config" you can set "Browser.sessionstore.enabled" to false, in which case firefox will not restore your browsing session after it closed.
When you either use
driver.quit();
or
driver.close();
Selenium always starts a new session. This means it's always directs you to the 'Login' page.
Just use below piece of code, no need to set capabilities.
browserDriver.quit();
browserDriver.get(loginPageURL);
You will see the Login page.

How to edit the url in current browser using Watin

I need to navigate to new url from the current opened browser using Wating Code, Let me know if any one tried the same scenario. Also I need to get the url in the current opened browser.
using (IE browser = new IE())
{
browser.GoTo("www.google.co.uk");
string curentUrl = browser.Url;
}
If the browser is already open, you use the AttachTo static method
http://watinandmore.blogspot.com/2010/01/browserattachto-and-iattachto.html
HTH!

Resources