Logging out a user properly in java - 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?"

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

Methods used to verify a "cancel" button in web application works

I am working on test cases for a web application. The current job requires me to confirm that when you press the logout button, the prompt comes up, you can click the cancel button on the prompt, and the prompt will close.
I am wondering about verify and assertion methods I could use to confirm this functionality works. The cancel button does nothing else but close the pop up prompt. What would you guys use?
Here's some code:
Actions actions = new Actions(driver);
WebElement logout = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[1]/div/img"));
actions.moveToElement(logout).build().perform();
WebElement logoutHover = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[3]/div/img"));
logoutHover.click();
WebElement logoutPushed = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[4]/div/img"));
logoutPushed.click();
WebElement cancel = driver.findElement(By.xpath("html/body/div[3]/div[4]/div/div/div[6]/div[2]/div/div[2]/div/div[3]/div/div/div/div[5]/div"));
actions.moveToElement(cancel).build().perform();
WebElement cancel2 = driver.findElement(By.xpath("html/body/div[3]/div[4]/div/div/div[6]/div[2]/div/div[2]/div/div[3]/div/div/div/div[5]/div"));
cancel2.click();
WebElement pageText = driver.findElement(By.xpath("html/body/div[3]/div[1]/div/div[3]/div/div/div/div/div/div/div/div/div[2]/div[2]/div/div/div/div/div[2]/div"));
Assert.assertTrue("Text not found!", pageText.contains("PRODUCT LIST"));
This assert method does not work. My initial idea was that if you hit the cancel button, I can assert that the user is still on the same page (my code for this does not work). Would it be a smarter choice to assert that the prompt is not present anymore? If so, how would I go about doing that?
I believe the correct way to do it would be to confirm that the logout prompt is no longer up and that you are still logged in. I don't know what your site looks like but for the logout prompt, find an element unique to that popup (maybe the OK or cancel button, hopefully something with an ID). Detect that it's no longer visible and then confirm you are still logged in by some means... look for a user name or ???
// click the logout button
// click the cancel button
List<WebElement> button = driver.findElements(By.id("sampleId")); // a button on the confirm popup
if (button.isEmpty())
{
// the logout confirmation popup is not visible
// verify that you are still logged in... maybe look for a user name or ???
}
else
{
// log a failure here because you couldn't cancel the logout popup
}

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

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

To close webengage or clickdesk poup using selenium webdriver

I got a webengage or clickdesk alert popup but that is intermittent, so I can't check everytime or wait for it to load, everytime I enter a new webpage because that would make my test case run very slow. Is there any workaround for it ( may be blocking it).
For WebEngage, all you got to do is call the respective clear() methods for the products being used on the site (to get rid of the corresponding UI components), as underneath:
webengage.feedback.clear();
webengage.notification.clear();
webengage.survey.clear();
However, this would only work when WebEngage has loaded on the page. If you'd like to completely disable loading of WebEngage products, insert this code in head tag of the page you are testing:
var window._weq = {};
_weq['webengage.defaultRender'] = false;
You can do a lot more cool stuff like the above using our JavaScript API: http://docs.webengage.com/api/js-api-v-4.0.html
Avlesh | CEO, WebEngage
If you are using ClickDesk on your website and would like to delete the Proactive rules which pop up the chat window randomly, then these can be deleted or edited to change the frequency from this link on your ClickDesk Dashboard - https://my.clickdesk.com/#proactive.
In case, you do not want the chat widget to show up at all until clicked on a text link or an image link then, you can add our Custom image link or text link code on your website, this would enable the chat widget only when clicked on it. You can find this link here - https://my.clickdesk.com/#departments/default.
If you have further questions or concerns with regards to ClickDesk, you can visit our website www.clickdesk.com and chat with one of our agents online.
Thanks
Mike Evans
ClickDesk Corporation

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