How to handle a popup in Selenium - selenium

I'm automating an application where on clicking a button a popup opens up , I am a bit confused about handling this popup.I am attaching the screenshot below where i have to handle the " Daily Trip details" popup. Can it be handled by driver.getwindowhandles() code or autoit?Thanks
Image of the popup:

You can handle using getWindowHadles(). After switching to new window you can perform actions which are required for your test case.
Following code may help you out for switching to modal
String currentWindowHandle=driver.getWindowHandle();
for(String windowHandle : driver.getWindowHandles()){
if(!windowHandle.equals(currentWindowHandle)){
driver.switchTo().window(windowHandle);
//perform actions which are required
}
}

Related

how to click on an element which comes under a toaster message

hi im into selenium automation course and as part of our project im automationg a web application. when i add users toaster message appears as User added succesfully. after this step i have to signout, but the signout element gets overllaped by the toaster. and error Element not clickable exception comes. i tried to give explicit wait, javascriptexecutor click. but its not working.
attaching my code:
public static void waitForElementToBeClickable(WebDriver driver, WebElement target) {
WebDriverWait wait = new WebDriverWait(driver, TimeUnit.SECONDS.toSeconds(EXPLICIT_WAIT));
wait.until(ExpectedConditions.elementToBeClickable ((target)));
}
public void isUserMenuLoaded() {
WaitUtility.waitForElementToBeClickable(driver, userMenu);
}
public SignOutPage clickOnUserMenu() {
PageUtility.clickOnElement(userMenu);
return new SignOutPage(driver);
}
In case the toast has close button you can wait for the toast appearance, then click the close button and wait until toast is disappeared.
Otherwise you should wait until toast disappeared and only after that you will be able to click the logout button.
You can enforce the process by clicking the logout button with JavaScript click but this is not recommended since this is not the way real user can do via the site UI.

How to close popup windows in Selenium?

I'm doing a test with java and popup windows appear (ads) and I do not know how to close them to continue with my test.
I have to click on a button but I change the focus to the window.
The url is www.orbitz.com
Please help!
orbitz
Java solution:
//switch to opened tab
ArrayList<String> tabs_windows = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs_windows.get(1));
//close current tab and switch driver back to original
((JavascriptExecutor)driver).executeScript("close();");
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
I haven't tested this for a while, but in different browsers getWindowHandles() used to return different orders for the tabs. Not sure if you can assume 0 is the first tab for all browsers. Change the index accordingly, or store the current handle before popup, and close all that don't match that.

Handle javascript alert in iframe using Selenium 2

I trying to practice handling Javascript alert. I want to accept alert ,which comes out of iframe in w3school website.
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("/html/body/button")).click();
WebElement editable = driver.switchTo().activeElement();
editable.sendKeys("enter");
//handle pop alert
String mainPage = driver.getWindowHandle();
Alert alt = driver.switchTo().alert();
alt.accept();
driver.switchTo().window(mainPage);
I am just getting alert box and then below error. I am not able to accept it.
Exception in thread "main" org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : I am an alert box!}
You have all the right code. The error comes when you try to do something other than handle the alert once the alert is up. You have some extraneous code that isn't needed to handle the alert. I've removed it in the code below. You can add things back but you will need to add them before or after launching the alert and handling it.
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("/html/body/button")).click(); // this launches the alert
driver.switchTo().alert().accept(); // this one line accepts the alert
driver.switchTo().defaultContent(); // you will probably need to switch back out of the IFRAME at some point to access the main page again so I added this line
One thing that I would strongly suggest for you is to read some guides on debugging in the IDE of your choice. If you learned how to debug your code, you could walk through it and more likely see where the issue was and fix it.

Selenium IDE window focus in Internet Explorer

I'm using selenium IDE and webdriver to test a web page in Internet Explorer. I discovered a while back that IE will not fully accept commands from Selenium if it's not the window in focus. For example, if the Selenium IDE window is in focus, and the command is to click a button in IE, the button will push down, but it won't let go.
With that in mind, my test involves popping up a window, doing a few things in it, leaving it open and returning to the null window to do a few things, then returning to the popup for a few more commands.
Is there a way I can make the null window come forward (over the popup) when I need to execute the commands for the null window? And then vice versa, can I make the popup then come forward when I need to return to it? I tried using windowFocus, but that did not work.
Use the SwitchTo() method and the TargetLocator Interface in Selenium.
A really simple example would look like this:
// Switch to new window
public String SwitchToNewWindow()
{
// Get the original window handle
String winHandleBefore = driver.getWindowHandle();
foreach(String winHandle in driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
return Constants.KEYWORD_PASS;
}
// Switch back to original window
public String switchwindowback()
{
String winHandleBefore = driver.getWindowHandle();
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
return Constants.KEYWORD_PASS;
}
I remembered that webdriver sometimes acts differently than running non-webdriver tests. It turns out that using windowSelect followed by windowFocus switches between windows when running webdriver tests.

How to click on facebook longin dialong window using Selenium tool

Hi All,
I'm new buddy to the selenium web automation tool. I'm stuck with an scenario were in I need to send log in credentials in the Facebook login window page to complete my test case. So please help out or provide some suggestion to pass the credentials and login to the Facebook account.
I tried out using the Selenium IDE and record my test script but while running the recorded script I'm unable to get access to that particular dialog window.
Below I had attached my scenario image
Thanks in Advance.
After lot of trails, I was able to achieved the above scenario by using the following below code.
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
try with this one please
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window
//switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
//Perform the actions on new window
//close the new window, if that window no more required
driver.close();
//Swithc back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)