For example in gmail login, when we consider a login test, when doing it manually for the first time we'll get the login page, from next time onwards we'll be directly getting into the inbox page.
If you try to do the same thing in webdriver(Run login test twice), in all these attempts we'll get the login page as we didn't login from this machine earlier. What is happening in behind the scenes in maintaining the session with respect to cookies or session ?
Here is the description & code snippet from selenium docs to add or remove cookies:
Before we leave these next steps, you may be interested in
understanding how to use cookies. First of all, you need to be on the
domain that the cookie will be valid for. If you are trying to preset
cookies before you start interacting with a site and your homepage is
large / takes a while to load an alternative is to find a smaller page
on the site, typically the 404 page is small
(http://example.com/some404page)
// Go to the correct domain
driver.get("http://www.example.com");
// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);
// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}
// You can delete cookies in 3 ways
// By name
driver.manage().deleteCookieNamed("CookieName");
// By Cookie
driver.manage().deleteCookie(loadedCookie);
// Or all of them
driver.manage().deleteAllCookies();
Related
Basically I'm trying to use HTML unit to perform a login.
However the login as form to input the username with a button next, then it actulizes the form and the password should be inputed. My problem occurs when I do button.click() the page gets the first form not the second where should be inputted the password
public void search() throws Exception {
WebClient wb = new WebClient();
HtmlPage p = wb.getPage(
"https://account.booking.com/sign-in?op_token=EgVvYXV0aCJHChQ2Wjcyb0hPZDM2Tm43emszcGlyaBIJYXV0aG9yaXplGhpodHRwczovL2FkbWluLmJvb2tpbmcuY29tLyoCe31CBGNvZGUqDDCgqZHe5rMjOgBCAA");
// HtmlPage p = (HtmlPage) wb.getPage(this.bUrl);
List<HtmlForm> form = p.getForms();
form.get(0).getInputByName("loginname").setValueAttribute("1234567");
HtmlForm fm = form.get(0);
System.out.println(form.get(0).getInputByName("loginname").getValueAttribute().toString());
List<Object> button = fm.getByXPath("//button[#type='submit']");
HtmlButton bt = (HtmlButton) button.get(0);
System.out.println(p.asText() + "\n+_________________");
bt.click();
System.out.println(p.asText());
}
The output shows to be the same before and after the bt.click()
1234567
Booking.com Account
This website uses cookies. Click here for more information.
Close
Sign In to Manage Your Property
Username
1234567
Next
Having trouble signing in?
Questions about your property or the Extranet? Visit the Partner Help Center or ask another partner on the Partner Forum.
Add your property to Booking.com
Create a partner account to list and manage your property.
Register
By clicking "Allow access" you authorize Extranet to use your Booking.com account info according to Extranet Terms of service.
+_________________
Booking.com Account
This website uses cookies. Click here for more information.
Close
Sign In to Manage Your Property
Username
Enter your username
Next
Having trouble signing in?
Questions about your property or the Extranet? Visit the Partner Help Center or ask another partner on the Partner Forum.
Add your property to Booking.com
Create a partner account to list and manage your property.
Register
By clicking "Allow access" you authorize Extranet to use your Booking.com account info according to Extranet Terms of service.
Sorry, but your code is based on a fundamental misunderstanding of Html and HtmlUnit.
HtmlPage p = wb.getPage(.....
retrieves a (html) page. This page is shown inside a browser window (same in HtmlUnit). If you interact with elements on this page like
form.get(0).getInputByName("loginname").setValueAttribute("1234567");
or better
form.get(0).getInputByName("loginname").type("1234567");
these elements are changing there state and as a result the whole page changes.
But:
Clicking an submit button is a total different story. In this case the browser (and HtmlUnit also) sends a Http Request to the server and gets back a new HtmlPage. Usually this page is shown inside the same window.
In HtmlUnit this is reflected by the return value of the click method - the return value is the new page. As long you are not assigning this value to a page variable and doing your next steps on this new page you are still working with the old one.
BTW: there is a commented sample on the Getting Started HtmlUnit page.
So far the simplest version of form/submit handling. But today the thinks are a bit (in fact many bits) more complicated because most of the pages out there doing (additional) magic based on javascript (e.g. Ajax).
Suggestion:
if you send me some credentials via private mail i can try to help you to get this login working based on HtmlUnit.
Suggestion 2:
Try to learn and understand all the technical stuff related to the web, without this you will be lost.
I'm trying to add cookies to a link before I open it with webdriver but it keeps giving me this error:
org.openqa.selenium.UnableToSetCookieException: Unable to set cookie (WARNING: The server did not provide any stacktrace information)
Please find my code below:
System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example
Please help with a relevant solution.
You are creating the cookie before navigating to the site. If you are trying to create a cookie on the domain www.example.com, then you would want to navigate to some page on that domain, create the cookie, and then start your test.
From my reading a while back, the best way to do this is to navigate to some page you know will not exist on the domain, e.g. www.example.com/this404page, then create the cookie. It should load a lot faster since it's an error page and shouldn't contain much content. After creating the cookie on the 404 page, start your test.
You are unable to do this because the WebDriver spec requires that you have the browser landed at the domain you are trying to set cookies for.
The work-around as mentioned is to go to the page prior to setting the cookie. But that causes some issues:
This unfortunately prevents 2 key use cases:
You want to re-use cookies from another webdriver session in a new session to avoid repeating the work it took to get the cookies. Such as having to repeat a login.
Allow a webdriver pool to be shared amongst unrelated threads where each thread might have their own cookies.
The webdriver spec clearly needs to take this into account. I have opened an issue here:
https://github.com/w3c/webdriver/issues/1238
Give it some votes. All browsers should start carrying a way to handle this.
First navigate to URL and then try to add cookies, try below code:
System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example
Replace your code with this :
System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
Adding cookies before navigating to the domain are called domain-less cookies which i believe is not possible.
I have not found a way to drop a cookie before the url, but I think below scenario might help you out-
Create a factory class to create webdriver instances
Before returning the webdriver instance, navigating to any page on the domain under test, and drop the cookie, then navigate browser back.
Your test can now begin, unaware that the navigation and cookie dropping has taken place
Just in case this is simpler, I did not need to create a factory class, merely load the root page from the site (which did not demand a cookie)
First collect the cookie as described above
pickle.dump(driver.get_cookies(), open("ChromeCookies.pkl", "wb"))
Then get the web site root which does not require a cookie (in my case)
driver.get(url_root)
Then execute the cookie load
for cookie in pickle.load(open("ChromeCookies.pkl", "rb")):
driver.add_cookie(cookie)
Then go to the page that I actually wanted to access
driver.get(url_not_root_demanding_cookie_which_is_now_there)
Please do post if there is an issue with this approach
Scenario:
I am setting Cookies in page https://www.example.com/#step1 by using method:
Cookies.setCookies("firstFileName","NewDocument");
Cookies.setCookies("firstFileExt","doc");
Users are supposed to click next and redirect themselves to https://www.example.com/#step2 . But in Case, user click to some other menu (like Home, About us, Contact us), I am deleting these Cookies by using the following method:
Cookies.removeCookie("firstFileName");
Cookies.removeCookie("firstFileExt");
But on removing I found that these two Cookies still holding the values in browser, When I do the following in https://www.example.com/#step1 before setting these two Cookies:
if(!Cookies.getCookie("firstFileName").toString().equals("undefined")){
Window.alert("firstFileName "+firstFileName);
}
I get an alert box that firstFileName NewDocument
Please let me know how can I set and remove the Cookies in my Case.
If you set a cookie without providing a path, as you do using Cookies.setCookies("firstFileName","NewDocument");, the cookie can only be removed on the same page.
A solution would be to always set the cookie for a specific path like that:
// TODO: set expiration time to something more useful
Date expires = new Date();
Cookies.setCookie("firstFileName","NewDocument", expires, null, "/", false);
Then you can remove it by calling that later on:
Cookies.removeCookie("firstFileName", "/");
As your application is based on the relative path "/", every page can set or remove the cookie and all pages access the same cookie value.
If you just set the cookie without providing a path, two pages of your application could even set different cookies.
I have a very basic question how the creation of HTTPSession works.I know you folks will fire me on looking at this question as similar kind
of questions exist.But there is reasoning why i am asking this question Here it is :-
I know httpsession is unique to web browser and server creates it when we do HttpServletRequest.getSession first time.It will maintaintain the same session till we
close the browser. But i have little bit different scenario.I Have a web application on one tomcat instance say T1.On welcome page of this web application
i have provided two links on click of which takes me to same java servlet(S1) of different web application hosted on another tomcat instance T2 (these two links
opens two seperate pop up windows). Now first i click the link1 and inspect the sessionId in S1 and find its value as 1678. Now first i click the link2 and
inspect the sessionId in S1 and find its value again as 1678. My question here is why i am getting the same session id for both the requests origintaing
from link1 and link2? what can i do to to get the different session for both of these requests?
What i tried after looking for possible solutions on net :- On click of link1, in Servlet S1 , i copied session attributes, invalidate it and create new one.
Say new session id is 8765 . Now i click the link2 and found the same session in this request too. So i further invalidate it and creates new one(say
new session id is 4897). Ideally it should expire the first browser session (generated on click of link1). To verify it,i click anywhere on pop up 1 it does not get
expired but i see again last generated session id i.e 4897. I am not getting why it attaching the same session id with both pop up windows?
Folks Thanks for your patience for taking your time out and read this long scenario?
Edit :-
Cookie[] cookies = req.getCookies();
if(cookies!=null)
for (int i = 0; i < cookies.length; i++) {
cookies[i].setMaxAge(0);
context.getResponse().getHttpServletResponse().addCookie(cookies[i]);
}
HttpSession myAppSession = req.getSession();//line 1
Assume on click of link1 i get session id as 1234,then after click of link 2 also i get the same session id. As per my understanding, after executing the code above line 1 , i should get the different session id as i am setting the MaxAge as0 before getting the session. But its not happening?
I think this is what you are looking for :
By default session tracking happens by cookies. WebServer sends the session id to the browser in the form of cookie. And, the browser send the cookie having session id for the subsequent requests.
How does the browser identifies which cookies to send for a link/request?
It is based on the these parameters. If the request matches these paramters the browser sends that particular cookie:
Domain: The domain name to which the request is made. Verify in your case if the domain name is same for two instances
Path: If the path name is same. Web Server send the context root as the path , requests under same context root share cookies.
Secure: Server sends if the given cookie is secure or not. Meaning, if the cookie can be sent on non-secure channel.
These parameters will let the browser to send the cookies to the server. And because the same cookie is sent for both the instances you are having. I think the session id is being shared.
If the request propeties such as Request URI, domain and path(i.e, context root) are same between requests, there is no way to tell the browser to use different cookies.
You have some options below:
Use different domain names.
Use different context roots.
Have a LB in front of two nodes and redirect to the correct node based on Session id
I have a website built on App Engine(Java) and need user use Google Account to login.
The situation is that:
User Adam has multiple accounts.
User Adam login with account Adam1 and get his Adam1 data in browser page A.
He clicked logout link, but opened it in another tab page B(the same browser of course)
He login with another account Adam2 in browser page B get his Adam2 data shown.
He then returned to browser page A and made some changes to his data and then send to server, at this time my app would recognize the current user is Adam2 , and the changes would be taken on Adam2, it does not match the status with its current page A, our user may be confused.
I thought maybe I can attach a userID parameter while making change request to the server and server side will compare the current user id with this userID parameter to make the change request processed or return a refresh command to make the out-of-date page be refreshed to the current account's if the ids are not same.
What is the best practice to handle this situation?
Put a hidden field on your forms that is a combined hash of the session ID and the user ID. When your server processes the request, double check that the combined hash sent along with the request matches what you expect. If either the user or the session is wrong, the hash won't match, and you can report an error appropriately.
Presumably the user would be identified by a Session ID that is send as Cookie information. Adam on site A will have a different Session ID than Adam on site B because of the differing login. Also presumably the form page will be protected such that a user needs to be logged in in order to access it.
When Adam logs out on page B, the old session is destroyed on the server and the login becomes invalid. When Adam submits the form from page A, the browser doesn't know this has happened and will submit the form together with the old Session ID. The server will (should) reject this submit since the session has already expired.
Hence, in a properly coded Session/User management system, this becomes a non-issue. The critical point is to renew/invalidate the Session ID upon logout.