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

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
}

Related

Continue button is clicked but does not navigate to the NEXT step on the same page in selenium java

I'm trying to automate a site but stuck at the point where I need to complete the onboarding process of the user account.
So the problem is I have to add a user and to add the user I have to go through few steps, I have successfully added the user details but when I click on continue button it does not navigates me to the next on same page.
I want to know that how can I navigate to the next step on the same page button by clicking the continue button
Here is my code
public void enter_advisor_details() {
driver.findElement(user_mgmt_opt).click();
driver.findElement(advisor_tab).click();
driver.findElement(add_advisor_btn).click();
driver.findElement(first_name).sendKeys("Test");
driver.findElement(last_name).sendKeys("Automation");
driver.findElement(email).sendKeys("TestAuto#gmail.com");
WebElement element = driver.findElement(By.xpath("//div[#class='mt-8']//button"));
element.click();
}
Note: I have tried Actions class, WebDriverWait and JavaScript executor but nothing works for me
As you can see in the below image the test is getting passed and the button is clicked and the next step does not show up
enter image description here
You are probably missing a delay. Wait for element to become clickable before clicking it, as following:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='mt-8']//button"))).click();

How i can click on link to get back to a previous page

I'm trying to get back to previous page by clicking on "Précédent" button
but, i didn't succeed, i tried many codes, all of the get me to the login page, that's mean i sign out,
in the page, if you perform a refesh or open it from another tab, it will disconnect and get you back to the login form
so, i recently figure it out how to use the same phantomjs session without disconnect but for one page, another page that i didn't scceed to access it without sign off, but there's a back button so, i want to use it to get back to the home page then enter the needed page
sorry for the confusion
the code
public void photoProfile() throws IOException {
// String locator = cssLocator;
String cookie = String.join("\n",Files.readAllLines(Paths.get("temp\\cookie.txt")));
Login webpage = new Login();
WebDriver driver = dd.driver;
driver.navigate().to("https://www4.inscription.tn/ORegMx/ListeInscriptions.jsp?Idsession="+cookie);
WebElement back = driver.findElement(By.xpath("//a[contains(text(),'Précédent')]"));
//back.click();
//Actions action = new Actions(driver);
//action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Précédent')]"))).click().perform();
//action.moveToElement(back).perform();
//Right Click
//action.contextClick(back).perform();
//Actions builder = new Actions(driver);
// builder.moveToElement(back).click(back);
//builder.perform();
System.out.println(driver.getTitle()); //to check if the page is the correct one
i tried
WebElement back = driver.findElement(By.xpath("//a[contains(text(),'Précédent')]")).click;
but i'm getting
type mismatch cannot convert from void to webelement
javascript of this button is
javascript:history.back()
what i need to do?
It's returning that error because you're trying to call the .click(); method while setting a WebElement, and .click returns a void. You need to break it up into 2 commands:
WebElement back = driver.findElement(By.xpath("//a[contains(text(),'Précédent')]"));
back.click();
I guess its because of the webpage security the session is getting expired and you are getting logged out when you are trying to go back after doing login, though if you are able to go back manually then you should be able to do it through automation as well. You are try the follow method to go back to the previous page:
driver.navigate().back()

Selenium Java Chrome send enter key while the elements on the page are disabled

I need to close a dialog box that pops up after I had selected 'Save' by clicking 'Ok', however none of the elements on the page as well as dialog box can be inspected by right clicking after the pop-up (tried F12 doesn't help).
Alternatively, to close the dialog box enter key could be given, however I'm unable to send the enter key as listed below.
Actions action = new Actions(driver); //attempt 1
action.sendKeys(Keys.RETURN);
action.sendKeys(Keys.RETURN).perform(); //attempt 2
Both actions does not close the dialog box. I had performed the driver switch as well. In addition to the dialog box pop-up there's also an outlook email pop-up could this create an issue in identifying the alert pop-up? Kindly advise on how enter key could be passed to close the dialog box.
Found the issue, it was due to Thread.sleep(3000) that I had used after the click, guess this caused it to miss the alert from being recognized. I might be worng as well. Thanks for all your help!
As none of the elements can be inspected by right clicking after the pop-up which indicates its an Alert which is Javascript generated and you can use the following line of code :
Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
myAlert.accept();
Update A
As per the comment update :
Observation : In addition to the alert msg there's also outlook mail also pops up
Conclusion : If outlook mail pops up possibly it's not an Alert as suspected. You should be able to track the element within the HTML DOM.
Update B
As per the comment update :
Observation : there are 2 thing happens when i click on 'Save': [1]: Outlook email pop-up [2]: Alert notification on the saved page stating the records are being updated
Conclusion : Sounds like two window_handles opening up, treat them as window_handles.
Solution 1 : Try switching to the pop up and handle it.
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Solution 2 : If you want to go with Keyboard keys.
Actions action = new Actions(driver);
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
action.sendKeys(Keys.RETURN).perform();
In both cases, use 'wait' until you get an alert.
First, check the dialog box that pops up after you click 'Ok' is JavaScript Alert or anything else?
If the dialog is JavaScript Alert then try below code.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();

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

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?"

Categories

Resources