VJ_QE
VJ_QE

Reputation: 143

How Can we Retain old session of browser using FirefoxProfile?

I was asked a question in an interview: You are working on a session and there are some questionnaire for first time users. But when the user close the browser and reopen it, the questionnaire must not appear (because he is not a first time user now). I had to tell how to automate this scenario in Selenium using TestNG framework(using Java). So basically he wanted to close/quit the current driver and wanted to use another driver to reopen the page and make sure that new session remembers that the user has visited before (just like its done manually).

My answer to it was to add the cookie to the new driver session. But he dint want to do that. He wanted to test exactly like a user would do. He gave me a hint about using FirefoxProfile for this to retain the profile that we are working on.

What is the use of FirefoxProfile and how can we use it in current scenario. I'd appreciate if you can provide the code that is to be written for this.

Upvotes: 2

Views: 219

Answers (1)

Erki M.
Erki M.

Reputation: 5072

So it comes down to how that specific website determines if you have answered questionnaire or not. As you gave no explanation in the question, I will just assume it is done the way most people do it - that is - using a cookie.

If this assumption is correct, your suggestion to use a cookie is the best suggestion and your interviewers statement to use FF profile simply makes no sense. I mean, I am not even sure you can do such thing with FF profile, anyway, injecting a cookie when a browser is fired (or inject using a proxy) is definitely a way to go with this. It also may or may not be that your interviewer has a little distorted knowledge about how the web works. So:

He wanted to test exactly like a user would do.

how would that really look like in your interviewers mind - "a prankster user wondering around the web, closing his sessions whenever acceptable and starting again with clean installation of a browser, but in the same time always secretly injecting a cookie so he is not getting the questionnaire?"... I am not really a certified expert in this matter and I have no scientific data to prove it, but I hardly think that this is what my users do.. So what users really do is .. nothing. It is the browser that is remembering the cookies, hence giving the webapp a way to understand if this user has visited you or not. So yea, I state again, that injecting that cookie is what you should do. And even if it possible to inject cookies or smth with FF profile - just refuse to do it and use WebDriver object instead, as using a profile for such task just makes no sense (new FF profile is created when FF is launched, also the statement:

"lose/quit the current driver and wanted to use another driver to reopen the page and make sure that new session remembers that the user has visited before (just like its done manually). "

: spinning up new WebDriver instance and clicking "open firefox button as a user" are not the same thing. Please consult the docs if you don't believe me. Calling new FirefoxDriver() causes the system to load new FF with new anon profile, whereas the "normal FF installation" lives in the harddisk with all his caches and other stuff.

So sometimes in interviews they intentionally give you questions that make no sense and trying to solve them would hurt your family as well as mine and all the mankind in general - like "Write a regexp to parse HTML". In these cases you just need to say: "Thats not really how we do it, let proceed to the next question". And thats what you should do in my opinion. Best luck.

Upvotes: 1

Related Questions