How to handle multiple pop-ups - java

I am writing a selenium test for the following flow:
Select all items.
Click on the Run button - opens a pop-up with two buttons "Proceed" and "Cancel".
Click on "Proceed", opens the new pop-up which will display the progress bar.
Here are two problems I am facing:
I am able to click the "Proceed" button via selenium code, but the moment it gets clicked the other pop-up which displays the progress bar doesn't show up, because of thisthe selenium is not able to identify the element. How can I handle this?
The Progress bar has one button "Cancel", while the progress bar is being filled the Value of the button remains "Cancel", but after it reaches 100% the Value changes to "Close". How should I handle this?
I tried the following, but it didn't work:
Tried to wait until the visibiltyOfElement, presenceOfElement of the element (progress bar) by XPath and id.
Tried Thread.currentThread.sleep after clicking "Proceed" and Implicit wait too.
Tried to wait until AlertIsPresent.
The code:
// Select all items
wd.findElement(By.id("ListSelectAll")).click();
// click on Add the selected
wd.findElement(By.xpath("//*#id='ListRunLink']/img")).click();
// Proceed to confirm Adding
wd.findElement(By.xpath(".//*#id='Proceed']")).click();
// Wait until the Progress Bar Gets Displayed using the buttons
// present in the Progress bar to address Wait!!
wt.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[#id='Close']‌​")));
// The above code does NOT work - "not able to locate element with id CLOSE"
Can someone help me with this?

Related

After selecting mouse hover in selenium, Screen is getting disturbed. Want to know the option to change the mouse pointing

my code is working as it is hovering on the menu and select the element which I want to select but when it selects the element, UI is getting disturbed. Code is written in java.
public void HoveronMenu() {
Actions action = new Actions(ldriver);
WebElement mainMenu = ldriver.findElement(Hover);
action.moveToElement(mainMenu).moveToElement(ldriver.findElement(selectmenu)).click().build().perform(); }
This is normal view.
This is the image after executing script
After selecting the element, system acquires entire space of hovering element from top to bottom and showing as a pop up. Is there any way to clear the screen after clicking on hover element?
Thanks.

System returns me back to the previous page instead of taking to the next one without even clicking 'Next' or 'Back' button

I have a webpage See WebPage screen shot
Code
WebElement whatAirline = driver.findElement(By.cssSelector(".BaggageFlightDetailsView-airlineField input"));
whatAirline.sendKeys("AeroGal (2K)");
whatAirline.submit(); //This is kind of drop down menu list is the COMBOBOX with no class 'select', just class 'input'
WebElement confirmationCode = driver.findElement(By.cssSelector(".Input-input"));
confirmationCode.sendKeys("test");
whatAirline.submit();
It brings me to the previous page, though 'Next' button on this page becomes enabled!
IMPORTANT:'Next' button is not even a button it just has class -that's all, no other data! `class = ''PagerButtons-button PagerButtons-button--next''
What else i tried? Explicitly wait, click 'Next' as a button (though it is not even a button)
Nothing works ! HELP!
I think you have entered invalid data in any of two fields. So the next button may not be enabled, it automatically clicks enabled back button. Check your inputs.
Use select class for drop down list. Also use explicit Wait to click next button.
Solved!
The problem that i found was:
Combobox was not holding the value selected in drop-down, just typed it.
System was taking me back because 'Back' button was the only one enabled.
That is why submit was taking me back every time.
I added whatAirline.click(); before submit and it worked out!
Please note, that i should have not write button click as on top of everything i had no button at all, just an element that looked like a button.

Is there a definite Selenium solution to modal pop up dialogs in Internet Explorer with Java?

I have searched many, many places for a solution to my problem, but haven't found it. I figured that by now, Selenium would have provided a straight forward and simple solution to handling modal windows/dialogs from Internet Explorer using Java.
The web application that I am testing has the following characteristics:
It is ONLY supported by Internet Explorer (no way around this)
Main page accepts a userid and password, with a "Login" button
Upon login and on page load, there is a pop up "Welcome - What's new" window with a checkbox to "Don't display this again" and an "OK" button to dismiss the window.
I cannot do anything to the parent window until I dismiss the pop up window
Right-click is disabled on the pop-up window (however, I can see the source code by opening the F12 tools before login and window pop-up)
This is what I've tried:
getWindowHandles() always returns 1 for the parent window, so this makes driver.switchTo(handle) not-applicable
It is not an alert, so driver.switchTo().alert() or accept() do not work
findElement(By whatever) will NOT find any elements in the pop up window (like the "OK" button or the checkbox, etc.)
Robot class is the only thing that I have seen work, where I can send keypresses to navigate to the "OK" button and click it to dismiss the window...
Here is my issue:
Since there is a checkbox to "Don't show this again", there are users for which this modal pop up window will display and some for which it won't. I need to account for both cases
I need to find a 100% sure way to know whether the pop up is displayed or not. If I have this information, I can make use of the Robot class (although "dirty") to perform actions on the pop up if needed
I tried finding out if the parent window elements are enabled using isEnabled(), but even though items are not manually "clickable" while the modal pop up window is displayed, isEnabled() always returns TRUE--so this does not work--is there a better way to check for this for the "blocked" elements in the background?
My questions:
How do you check for the existence of a modal pop up that does not display 100% of the time? (on Internet Explorer 10, using Selenium with Java)
Besides using Robot class, how do you interact with the actual Elements in a modal pop-up dialog (for example, dynamic Radio Buttons that don't always display the same options to the user)?
Thank you.
You should use WebDriverWait with some expected condition. For example,
WebDriverWait wait = new WebDriverWait(driver, 5); // sets timeout to 5 seconds
wait.until(...); // Use ExpectedCondition to set the condition you need to check for (i.e. element to be clickable, frame to be visible, etc.)
// Do your thing.
The until method will return an object type relative to the function passed. For example, until(ExpectedConditions.elementToBeClickable(...)); will return a WebElement object you can use to exert an action on (like clicking on it).
Lastly, you should wrap those lines in a try/catch and handle the TimeoutException the wait method will throw if the condition never arises.
To summarize, structurally, your code should look something like this:
// instantiate the WebDriver
...
int timeoutMax = 2; // The maximum number of seconds you wish to wait before timing out (let's assume 2 seconds is reasonable for your case)
try {
By checkboxLocator = By.id("checkboxID"); // Locate element by some criteria (id, css, xpath). Using by ID for illustration purposes only
By buttonLocator = By.id("buttonID"); // same as above
By popupLocator = By.id("frameid"); // same as above
WebDriverWait wait = new WebDriverWait(driver, timeoutMax);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(popupLocator)); // assuming it is an iframe
// The next lines will not be executed if the "Don't display this again" and clicking "OK" were clicked before (locating the frame will timeout because it not be visible)
WebElement checkbox = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
WebElement okBtn = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
checkbox.click();
okBtn.click();
driver.switchTo().defaultContent(); // Switch back to default window
} catch (TimeoutException exc) {
// Handle exception (i.e. log a warning) - This should be thrown as long as the modal dialog doesn't become visible
// If modal dialog ever becomes visible again, clicking on the checkbox and OK button will be executed again.
}
Something like this should work. Of course, this make some assumptions that might not be true for your case. However, if you use the right locating technique for your modal dialog, you should be able to:
Locate the modal window (use By class to locate it)
Use WebDriverWait to setup your timeout conditions
Tell the driver to switch to it (if this times out, skip steps 3, 4, and 5)
Locate the checkbox and OK buttons
Click the checkbox and the OK button in that order
Tell the driver to switch back to the main window
Continue with your test
Create an If statement in terms of a boolean variable to check for the existence of a modal pop up that does not display 100% of the time.
If the modal is html generated (which it should be, if it holds dynamic content), then try:
driver.switchTo().activeElement();
driver.switchTo().defaultContent();
Also, you may have to insert a wait so that the html has time to generate.
If the modal is a browser alert, then try:
alert.accept();

How to click contextual action bar item using Espresso framework

I am writing a test case that long clicks an item in my ListView (backed by a CursorAdapter) which will pull up a Contextual Action Bar and has a menu item that allows the user to delete that item.
I use the following code:
public void testDeleteFirstAccount(){
// Long click
onData(is(instanceOf(Cursor.class))).atPosition(0).perform(longClick());
// Click delete menu item
onView(withId(R.id.action_delete_account)).perform(click());
// Find alert button with text
onView(withText("Yes")).perform(click());
}
However, I am unable to click the button because I believe the test is running too quickly. I get the following error:
android.support.test.espresso.PerformException: Error performing
'single click' on view 'with id:
com.example.android.cashcaretaker:id/action_delete_account'.
Caused by: java.lang.RuntimeException: Action will not be performed
because the target view does not match one or more of the following
constraints: at least 90 percent of the view's area is displayed to
the user.
The reason I believe the test is too quick is because if I add Thread.sleep() after the long click, everything will work fine. I only did this as a test to verify my suspicions, I'm not sure stopping the UI like that is the proper way to move forward.
I have also tried adding getInstrumentation().waitForIdleSync() but had no success.
Am I doing something else wrong? What is the proper way to use Espresso to click a CAB item?
Perhaps the view is still being animated at the moment that Espresso tries to click the ContextualActionBar item. And if you set a timeout, the animation has time to finish and the view is completely displayed by the time the click is performed.
Have you tried disabling animations in the test device as stated in the Espresso Setup guide?

print dialog does not let next code to be executed in selenium

The code that i'm using is below. First line clicks on 'PrintButton" which opens up "Print" dialog. Once this is open, I expect a console output "Sending Esc key - Start" and then subsequently one of the action like robot, actions, alert dismiss, window switch to happen (based on whichever code is uncommented). But instead, the console does not print anything UNLESS i click on Cancel in print dialog. So, once i click on cancel, the sysout prints the message, performs the action (and nothing useful happens because of those actions) and then prints another console msg.
My Question is two parts. a. Why is the compiler (or program) not moving to next line? b. How can I handle this print dialog? (read all the articles in internet, tried the suggested methods but nothing worked).
driver.findElement(By.id("PrintButton")).click();
System.out.println("Sending Esc key - Start");
/*Robot r = new Robot();
r.delay(10000);
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyPress(KeyEvent.VK_ESCAPE);
*/
/*Actions a = new Actions(driver).sendKeys(Keys.CANCEL);*/
/*driver.switchTo().alert().dismiss();*/
/*List<String> handles = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(handles.get(handles.size()-1));*/
System.out.println("Sending Esc key - done");
I had the same problem on ChromeDriver and found the only way is to click the print button with JavascriptExecutor in a async fashion.
WebElelement print_button = driver.findElement(By.id("PrintButton"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("var elem=arguments[0]; setTimeout(function() {elem.click();}, 100)", print_button);
Inspired by this tread
The driver never returns because Selenium uses JavaScript internally to "click" the button. Clicking the button opens a modal dialog which blocks the browser's JavaScript engine (all animations stop as well). This is partly because of the modal dialog (alert() does the same thing) but also because you probably want to print the page as it is - it shouldn't change while you print it.
Workaround: Make sure the element has the focus (using Selenium). Then use Robot to drive the whole process: Send a key press to activate the button (it's very hard to locate the button on the screen). Then the print dialog should show up and it should have the focus. The next key presses in Robot should then drive the dialog.
Note: This is brittle. My suggestion is not to test the print dialog this way. If you need this, then Java+Selenium might not be the correct solution. Look at professional UI testing tools like QF-Test.
Related:
How to handle print dialog in Selenium?

Categories

Resources