Search code examples
c#watin

How to click OK in confirm popup opened from a frame (Watin)?


I can't find a way to click confirm popup when it is inside iframe. I could find the iframe and click "Clicked OK" button, then confirm button pops up, at this point I am stuck. I posted my html and c# code below.

Here is file:///C/Temp/IFrame.html

<html>
    <body>
        <iframe id="FrameID" src="file:///C:/Temp/confirm1.html" width="100%" height="100%">
          <p>Your browser does not support iframes.</p>
        </iframe>
    </body>
</html>

here is the file:///C:/Temp/confirm1.html

<html>
  <body>
    <input id="myButton1" type="button" value="this is a button" 
      onclick="confirmMe(); return false;"><br>
    <script>
      function confirmMe() {
        var answer = confirm ("Are you having fun?")
        if (answer)
          document.getElementById("myButton1").value="Clicked OK";
        else
          document.getElementById("myButton1").value="Clicked Cancel";
      }
    </script>
  </body>
</html>

Here is what it looks:

enter image description here

I have this code that works without frame but when frame is used, I can't find a way to click the confirm dialog poped up from iframe.

public static void ConfirmPopupFromFrame()
{
    IE ie = new IE();
    ie.GoTo("file:///C:/Temp/IFrame.html");
    ie.WaitForComplete();

    Frame iframe;
    try{
        iframe = ie.Frame("FrameID");
    }
    catch (FrameNotFoundException ex){
        throw ex;
    }

    ConfirmDialogHandler confirm = new ConfirmDialogHandler();
    using (new UseDialogOnce(ie.DialogWatcher, confirm))
    {       
        iframe.Button("myButton1").Click(); // At this point confirm dialog pops up but below lines doesn't work since this is frame instance not ie. is there a way to make it work?
        confirm.WaitUntilExists();
        confirm.OKButton.Click();
    }       
}

Solution

  • Change

    iframe.Button("btnId").Click(); 
    

    to

    iframe.Button("myButton1").ClickNoWait();
    

    The button id is probably just a typo in your test code, what's important is change Clinck to ClickNoWait. I've just tested - it works for me