WatiN and .net winforms WebBrowser management - is DialogWatcher possible?

Our goal: testing a browser with Watin support built into winform.net.

We are currently using the .net WebBrowser control to enable browser behavior in winform. We attach WatiN to the WebBroswer control in the form with this code (thanks prostynick):

var thread = new Thread(() =>
{
    Settings.AutoStartDialogWatcher = false;
    var ie = new IE(webBrowser1.ActiveXInstance);
    ie.GoTo("http://www.google.com");
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

The problem is that the "winform browser" must handle pop-ups during testing / automation.

Question: How can I handle pop-ups when Watin is attached to the winforms webBrowser control (and does not use its own IE window generated by WatiN)?
a) Can I use the Watin DialogWatcher? If so ... how?
b) If not, then maybe we can write our own DialogWatcher, but we need hWnd or processID to add it. Where do we get the correct hWnd or processId in this scenario where Waitin does not have its own window or process?

Thanks in advance for any ideas ... welcome alternative approaches that achieve the same goal!

+3
source share
2 answers

WatiN (head revision - 1166 - trunk: https://watin.svn.sourceforge.net/svnroot/watin/trunk/src/). DialogWatcher , DialogWatcher .

:

public class WebBrowserIE : IE
{
    private IntPtr hwnd;

    public WebBrowserIE(WebBrowser webBrowserControl)
        : base(webBrowserControl.ActiveXInstance, false)
    {
        hwnd = webBrowserControl.FindForm().Handle;
        StartDialogWatcher();
    }

    public override IntPtr hWnd
    {
        get
        {
            return hwnd;
        }
    }

    protected override void Dispose(bool disposing)
    {
        hwnd = IntPtr.Zero;
        base.Dispose(disposing);
    }
}

IE . javascript:

var ie = new WebBrowserIE(webBrowser1);
var thread = new Thread(() =>
{
    ie.GoTo("http://www.plus2net.com/javascript_tutorial/window-alert.php");
    ie.Button(Find.ByValue("Click here to display alert message")).Click();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

. WebBrowserIE . , Handle Form.

+4

, , , - : DialogWatcher (: watin WebBrowser?), , , , . , , , .

  • FormDialogWatcher, DialogWatcher, , ..
  • . , , , , , WebBrowser controll, , , . :

    • private static IList<DialogWatcher> dialogWatchers
    • public static DialogWatcher GetDialogWatcher(IntPtr mainWindowHwnd)
    • public static DialogWatcher GetDialogWatcherFromCache(IntPtr mainWindowHwnd)
    • public static void CleanupDialogWatcherCache()
    • public void IncreaseReferenceCount()
    • public void DecreaseReferenceCount()
    • public int ReferenceCount { get; private set; }
    • private bool IsWindowOfIexploreProcess(Window window)
  • Start() :

    if (new Window(MainWindowHwnd).Exists())
    {
        var winEnumerator = new WindowsEnumerator();
        var windows = winEnumerator.GetWindows(win => true);
    

    :

    var mainWindow = new Window(MainWindowHwnd);
    if (mainWindow.Exists())
    {
        var winEnumerator = new WindowsEnumerator();
        var windows = winEnumerator.GetWindows(window => window.ProcessID == mainWindow.ProcessID);
    

    ( GetWindows)

  • HandleWindow(Window window) :

    if (!IsWindowOfIexploreProcess(window)) return;
    

! , : new FormDialogWatcher(Handle) Handle Form. , IE (LOL, , :)) - Form_Load - . (. ), , .

EDIT. , ( WatiN) , MessageBox.Show :)

2 (!). DialogWatcher, WatiN SVN 1056. : http://watin.svn.sourceforge.net/viewvc/watin/trunk/src/Core/DialogHandlers/DialogWatcher.cs?revision=1056&content-type=text/plain&pathrev=1056

+3

Source: https://habr.com/ru/post/1743754/


All Articles