2

Ok so I know there are a number of other answers on this question about alerts in webdriver and I've looked through them but I think my situation is a little different. When I click my submit button I have already switched into 3 frames and then I get the alert, so I tried to switch back to default content and then click the alert using a try catch and alert.accept but it still doesn't click the alert. Code is below. Thanks in advance for your assistance :)

public class BookAHoliday {

    public FirstPage completeHolidayFormAndSubmit(String firstDate, String lastDate) {

        sleepsAreBad();
        driver.switchTo().frame("ContainerFrame");
        driver.switchTo().frame("iframeCommunityContainer");
        driver.switchTo().frame("FORMCONTAINER");
        fluentWait(By.id("StartDate_txtInput"));
        firstDayOfLeaveInput.sendKeys(firstDate);
        sleepsAreBad();
        lastDayofLeaveInput.sendKeys(lastDate);

        try {
            submitButton.click();
        } catch (UnhandledAlertException f) {
            try {
                sleepsAreBad();
                driver.switchTo().defaultContent();
                Alert alert = driver.switchTo().alert();
                String alertText = alert.getText();
                System.out.println("Alert data: " + alertText);
                alert.accept();
            } catch (NoAlertPresentException e) {
                e.printStackTrace();
            }
        }
        sleepsAreBad();

        return PageFactory.initElements(driver, FirstPage.class);
    }


    private void sleepsAreBad() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


public class BaseTest {


    public static WebDriver driver;
    static String driverPath = "C:\\";

    @BeforeClass
    public static void setUp() {
        System.out.println("****************");
        System.out.println("launching Browser");
        System.out.println("****************");
        // Browser selection

        //Firefox

        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
        driver = new FirefoxDriver(dc);

driver.get(URL);

 @AfterClass()
    public static void tearDown() {
        if (driver != null) {
            System.out.println("Closing browser");
            driver.quit();
        }

    }


public class Bookings extends BaseTest{

    @Test(description = "Holiday booking")
    public void CD01() {

        FirstPage firstPage = PageFactory.initElements(driver, FirstPage.class);
        firstPage


                 .logIn("username", "password")
                .clickHolidayLink()
                .completeHolidayFormAndSubmit("12/05/2016", "15/05/2016");



    }

alert box Here is the alert box

OhAye
  • 93
  • 1
  • 4
  • 12
  • You generally do not need to switch to the default content for alerts. Are you sure it's a javascript alert? It works only for those kind of alerts. – Mobrockers Jun 02 '16 at 09:53
  • To be honest with you I'm not really sure I've added the picture of the alert box. Is there any easy way to check? firebug doesnt work once the alert is present. – OhAye Jun 02 '16 at 09:59
  • That does not look like a javascript alert to me. – Mobrockers Jun 02 '16 at 10:01
  • If its not javascript what options do I have to click it? As it doesnt look like it has an id at least not one I can check. Really want to avoid using the Enter key! – OhAye Jun 02 '16 at 10:05
  • So it looks like its Modal dialog, any idea how you click that? – OhAye Jun 02 '16 at 10:12
  • 1
    Is there any exception when you are going to accept alert or not?? – Saurabh Gaur Jun 02 '16 at 10:22
  • 1
    Yes - org.openqa.selenium.UnhandledAlertException: Modal dialog present: Your request to book holiday has been submitted – OhAye Jun 02 '16 at 10:23
  • The modal is probably shown as an element on the DOM. Try if you can find the button in it and click on it – RemcoW Jun 02 '16 at 10:30
  • @RemcoW so i did notice an ID in the stacktrace error but when I try and click on it, it doesnt do anything. Do I need to switch to the active element? – OhAye Jun 02 '16 at 10:35
  • @RemcoW ah never mind that's not the right ID! I'll look in the DOM – OhAye Jun 02 '16 at 10:37
  • If the modal dialog is in a `` you'll have to switch to the frame before you can interact with it. – Mobrockers Jun 02 '16 at 10:43
  • @Mobrockers would driver.switchTo().activeElement(); work or would I need to find out the id of the frame? – OhAye Jun 02 '16 at 10:44
  • You need to find out the id or other selector of the frame, if there is one. – Mobrockers Jun 02 '16 at 10:45
  • I'm struggling to make heads or tails of the DOM. Is there an easier way to find the element? – OhAye Jun 02 '16 at 10:55
  • 1
    I think you have to implement some wait until alert is present... – Saurabh Gaur Jun 02 '16 at 11:16
  • I don't have your DOM so how would I know? – Mobrockers Jun 02 '16 at 12:45
  • @Mobrockers yes I'm very aware you dont have my DOM, I meant other than using the DOM is there an easier way to get the element of the Modal Dialog? – OhAye Jun 02 '16 at 12:47
  • There is no other way no. There would be no way for selenium to know what is a modal dialog as anything can become a modal dialog. – Mobrockers Jun 02 '16 at 13:31
  • Ok well thanks for your time and assistance :) – OhAye Jun 02 '16 at 13:35

2 Answers2

2

Try this when you have got UnhandledAlertException in catch

WebDriverWait wait = new WebDriverWait(driver, 3000);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
alert.accept();

May be it will help you...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • I thought alert.accept only works with javascript and not modal dialogs? – OhAye Jun 02 '16 at 12:47
  • 1
    Because I have faced this type of multiple issues with dialogs model... and if selenium clearly throws UnhandledAlertException... means it can be handle by selenium...so you can not down vote only with your thought.. – Saurabh Gaur Jun 02 '16 at 12:54
  • And I didnt down vote you, why would I down vote someone who is trying to help me? – OhAye Jun 02 '16 at 12:55
0

For most alerts you can press enter to press alert`s "ok".

George Marin
  • 385
  • 2
  • 5
  • 14
  • 2
    I did think of that but wanted something a little better than sending the Enter key, will use it as a last resort though :) – OhAye Jun 02 '16 at 09:59