5

I'm using Watin library in a windows forms app. In order to hide the browser I use this instruction :

Settings.Instance.MakeNewIeInstanceVisible = false;

However, it doesn't hide the popups (when simulating a click on an element that opens a popup). Is there a way to hide them?

Kira
  • 1,153
  • 4
  • 28
  • 63

2 Answers2

0

I've checked released notes and found this:

By default WatiN tests make the created Internet Explorer instances visible to the user. You can run your test invisible by changing the following setting. Be aware that HTMLDialogs and any popup windows will be shown even if you set this setting to false (this is default behavior of Internet Explorer which currently can't be suppressed).

IE.Settings.MakeNewIeInstanceVisible = false; // default is true

Since WatIN haven't updated since 2011, I think you wouldn't expect any new feature support what you want.

I don't know if this could be a workaround but If those popups are not important to you why just don't block all popups?

How to turn off popup blocker through code in Watin?

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WEBOC_POPUPMANAGEMENT
Value = 0 for Off
Value = 1 for On
Community
  • 1
  • 1
Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
0

You can do it programmatically by running some javascript code and make window.open function to do nothing!

Example

Here is a test page I made that has a very simple form and when the user clicks the Sum button, it sums up numA + numB and it displays the result inside a <span id="result"></span> element. After the result text update, it opens a popup window by calling window.open. In order to make this popup window disappear, we need to eliminate the window.open function:

window['open'] = function() { return false; }

To do that using Watin, we have to use the Eval function and inject the javascript code like this:

browser.Eval("window['open'] = function() { return false; }");

Then all popups are gone for that page load only and we have the wanted result.

Sample C# code

private void buttonPopupdEnabled_Click(object sender, EventArgs e)
{

    WatiN.Core.Settings.Instance.MakeNewIeInstanceVisible = false;
    IE ie = new IE();

    ie.GoTo("http://zikro.gr/dbg/html/watin-disable-popups/");
    ie.Eval("window['open'] = function() { return false; }");
    ie.TextField(Find.ById("numA")).TypeText("15");
    ie.TextField(Find.ById("numB")).TypeText("21");
    ie.Button(Find.ById("sum")).Click();
    string result = ie.Span(Find.ById("result")).Text;
    ie.Close();

    labelResult.Text = String.Format("The result is {0}", result);
}

Program running before javascript injection (Popup shows up)

Popup without javascript injection

Program running after javascript injection (Popup is gone)

enter image description here

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113