How to close the browser by rejecting popup window in selenium webdriver - java

I have one test case where after login, on some page when user tries to close the browser, it will show popup windows(alert) asking "you might lose the data, are you sure you want to continue?', with two options:
Leave the page
Stay on page
Clicking on specific option, the page will perform action.
'Stay on page' will not leave the page and Leave the page will close the browser.
Now when I try to close the browser, it doesn't ask me for Popup
webdriver.close()
closes the browser before.
How can I Accept/Reject popup and then based on action, it should close the browser?

If I am understanding the problem correctly, then you are trying to perform a click on X to close the browser window which generates additional pop up. If that's the case, you can try executing some JavaScript action to recreate the scenario
(( JavascriptExecutor ) webdriver).executeScript( "window.close()" );
instead of webdriver.close()
Note: Written in Java
More info:
With the syntax above you can only close the child tab not the entire browser only IF it is invoked with window.open()

Related

How to check if a popup window is open or not using Selenium WebDriver in Java

I'm trying to check whether the popup window I want to open is opened or not.
I have checked some question answers like
How would you check if a popup window exists using selenium webdriver?
But, nothings helped to solve the problem.
Here, first I open the login window by clicking the login button.
driver.findElement(By.xpath("//a[#id='login_btn']")).click(); // Click Login Button
I even tried getPageSource() but, it seems not working.
Any kind of help would be appreciated.
Thanks in advance. :)
If it's a native, browser alert (= a popup) you can do the following:
try{
driver.switchTo().alert();
// If it reaches here, it found a popup
} catch(NoALertPresentException e){}
What it looks like you're actually dealing with is an iframe which you can do the following after getting the attribute value of the "iframe" attribute:
driver.switchTo.frame("ValueOfIframe");
// Treat as normal webpage. Now in iframe scope
driver.switchTo.defaultContent(); // To return back to normal page scope
String mwh=driver.getWindowHandle();
Now try to open the popup window by performing some action:
driver.findElement(By.xpath("")).click();
Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows
Iterator ite=s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mwh))
{
driver.switchTo().window(popupHandle);
/**/here you can perform operation in pop-up window**
//After finished your operation in pop-up just select the main window again
driver.switchTo().window(mwh);
}
}

How do I send Username and Password in Login Authentication box for selenium. I cannot locate any elements

Here is the image.
I m trying to send the user keys and passwords
So I think the problem you are having is it becomes a popup, when a popup occurs you have to tell selenium to go to the pop up, and then type in the username and password data you need from the popup.
https://selenium-python.readthedocs.org/navigating.html#moving-between-windows-and-frames
There is how to deal with popups and window frames in python. When dealing with a lot of popups I like to do something like this.
#Name of main/first window Selenium invokes
mainWindow = driver.current_window_handle
#Switch to first popup
driver.switch_to_window(driver.window_handles[-1])
#Switch to main window
driver.switch_to.window(mainWindow)
If it's a javascript alert you have to handle it differently, hope this helps.

Logging out a user properly in java

I'm using Eclipse as my IDE
i already have the code for login
-check if user and pass match
-check if account's session column in DB is "logged in". if false log in user, else prompt the user
when logging out, i have a log out button which when clicked changes the 'logged in' into 'logged out'.
Now the problem is when the user didnt click the log out button and instead just closes the application. I tried making a window listener when the frame is 'closing' then redirecting that to the log out button, it kinda solves my problem so I assigned every frame to redirect to the log out button action when 'window is closing'.
My app works like this: My app has multiple frames. After logging in there is the homepage, then 4 more buttons to direct you to other modules. In Homepage, when you click on module_A, homepage then disposes and module_A frame pops up, if you click the 'back' button module_A disposes and homepage pop ups again, clicking module_b disposes homepage and pops up module_b frame and so on...
Scenerio 1:
~logged in - changes user status from 'logged out' to 'logged in' redirects user from log in page to home page
~on homepage i forgot what i would do so i just close the application, since i have a listener 'window closing' that will change 'logged in' state to 'logged out' it's good.
Scenerio 2:
~logged in - changes user status from 'logged out' to 'logged in' redirects user from log in page to home page
~on homepage i click on module_A, that will then dispose the homepage AND will change 'logged in' status into 'logged out' because homepage window closed.
how can i fix scenerio 2? since closing a frame logs me out but im still using the app only with a different frame called moduleA
PS. if you guys dont understand what im saying, please ask questions, ill answer as fast as i can. im not really good at explaining my situation im so sorry :'(
So I guess what you have is something like this:
void onWindowDispose() {
logMeOut();
}
What I'd suggest is to use something like a counter:
int windowCount;
void onWindowCreate() {
++windowCount;
}
void onWindowDispose() {
--windowCount;
if(windowCount == 0)
logMeOut();
}
You might not use exactly a counter, but I hope you get the idea. You should only log out when all of the windows are closed, not just any one of them.
Also, if "multiple frames" means "multiple JFrames", please see "The Use of Multiple JFrames, Good/Bad Practice?"

How to Handle POPUP browser in Selenium2(Webdriver)?

I'm using Selenium2 (Webdriver).
Well, I have a Problem.
I have a A Browser.
When I click one button in A Browser, Another popup browser(B) is open.
And, The process what I have to do in B browser is done, B browser wasn't close.
But, this is not my problem. B browser is originally Designed this way.
my problem is ..
After B browser's process is finished, when I tried to find out C element in A browser, So B browser wans't closeed, that I can't find out C element. because Selenium tried to find out C element in B browser.
I just want...
When I do something in Browser B, Browser B has a Handle.
and the Process in Browser is done, the handle must moving in A Browser.
How can I? Please Help me.
Here is the sample code for switching between the Windows:
With windows handle we can switch between windows.
WebDriver driver=new FirefoxDriver();
//First navigating to Yahoo site
driver.get("http://www.Yahoo.com");
//Capturing the window handle
String strMainWindowHandle=driver.getWindowHandle();
//For better understanding printing the page title
System.out.println(driver.getTitle());
//Now opening a new window
driver.findElement(By.xpath("//body")).sendKeys(Keys.CONTROL+"n");
Set<String> winHandles=driver.getWindowHandles();
for(String handle:winHandles)
driver.switchTo().window(handle);
//Navigating to Google site with new window i.e new window handle
driver.get("http://www.google.com");
//For better understanding printing the page title
System.out.println(driver.getTitle());
//Switching back control to main Window which captured earlier
driver.switchTo().window(strMainWindowHandle);
System.out.println(driver.getTitle());
Once you come back to your main window handle you can perform Click or whatever u want.

How to redirect to the same view using ModelAndview in Spring MVC framework

I have a page with a link. On clicking the link a page opens up in a separate window. This new page has a form. On submitting this form, some operation takes place at the server. The result of which needs to be redirected to the same page. However after the operation on using the following:
return new ModelAndView("newUser");
//This view "newUser" actually maps to the popped up window.
A similar new window again pops up and the message gets displayed on this new page.
Any ideas as to why this behavior or how to go about this?
If you open a popup window with a form in it, any submits from here to the server will be handled in the same location, so you will get your response (and any subsequent request-responses) in that popup window.
If I understand this right, you have a page X which opens the popup, you submit in the popup and as a result you want again the content of page X, but in the popup?
If that is the case I thing the behavior is not from Spring but from what you have in the X page. Maybe a JavaScript which gets triggered on load and opens a new popup? Can't really tell without seeing more code.

Categories

Resources