【问题标题】:Watin Internet Explorer how to start in-private mode?Watin Internet Explorer 如何启动私密模式?
【发布时间】:2014-11-13 07:49:09
【问题描述】:

我正在使用 C# Watin 框架编写一些自动化 Web 测试。

希望有人能帮帮我,

如何在“私有”浏览模式下打开新的 IE 实例? (即隐身模式)

需要“私下”浏览,因为有些测试需要登录。(我同时运行了一些)

我找不到有关此事的任何资源。 (除了我在某个论坛找到的半个补丁)

感谢您的帮助!

【问题讨论】:

    标签: c# internet-explorer watin


    【解决方案1】:

    我能找到的唯一解决方案是在隐身模式下通过命令打开 IE 实例,然后将 Watin 附加到它。

        //gen random url so we can find the window later
        Random rnd = new Random();
        int id = rnd.Next(1000, 10000); 
        string url = "id" + id+".com";
        //opening explorer
        Process.Start("IExplore.exe", "-private -nomerge " + url);
        browser = Browser.AttachTo<IE>(Find.ByUrl(new Regex(url)));
        browser.GoTo("http://www.google.com");
    

    【讨论】:

      【解决方案2】:

      我下载了WatiN source codes,打开IE.cs,编辑了方法CreateIExploreInNewProcess

          private static Process CreateIExploreInNewProcess()
          {
              var arguments = "about:blank";
      
              if (GetMajorIEVersion() >= 8 && Settings.MakeNewIe8InstanceNoMerge)
                  arguments = "-nomerge " + arguments;
      
              if (Settings.OpenInIncognitoMode == true)
              {
                  arguments = "-private " + arguments;
              }
      
              var m_Proc = Process.Start("IExplore.exe", arguments);
              if (m_Proc == null) throw new WatiNException("Could not start IExplore.exe process");
      
              return m_Proc;
          }
      

      然后,将此添加到 Settings.cs:

          /// <summary>
          /// Gets or sets a value indicating whether the browser will be opened in incognito mode.
          /// </summary>
          /// <value>
          ///     <c>true</c> if IE instance needs to be opened in incognito mode, otherwise <c>false</c>.
          /// </value>
          public static bool OpenInIncognitoMode
          {
              get { return Instance.OpenInIncognitoMode; }
              set { Instance.OpenInIncognitoMode = value; }
          }
      

      之后,将此添加到 ISettings.cs

          /// <summary>
          /// Gets or sets a value indicating whether the browser will be opened in incognito mode.
          /// </summary>
          /// <value>
          ///     <c>true</c> if IE instance needs to be opened in incognito mode, otherwise <c>false</c>.
          /// </value>
          bool OpenInIncognitoMode { get; set; }
      

      最后,像这样编辑 DefaultSettings.cs:

          private struct settingsStruct
          {
              ...
              ...
              public bool makeNewIe8InstanceNoMerge;
              public bool closeExistingFireFoxInstances;
              public bool incognitoMode;
          }
      
          public bool OpenInIncognitoMode
          {
              get { return settings.incognitoMode; }
              set { settings.incognitoMode = value; }
          }
      
          private void SetDefaults()
          {
              settings = new settingsStruct
              {
                  ...
                  ...
                  makeNewIe8InstanceNoMerge = true,
                  closeExistingFireFoxInstances = true,
                  incognitoMode = false
              };
          }
      

      编译它并将新的 DLL 添加到您的项目中。在此之后,您在项目中需要做的就是:

      Settings.OpenInIncognitoMode = true;
      var browser = new IE(url, true);
      

      【讨论】:

        【解决方案3】:

        诀窍是将参数-private 传递给IExplore.exe 的调用,如下所示:

        string argument = "-private -nomerge about:blank";
        process = Process.Start("IExplore.exe", argument);
        if (process == null)
            throw new InvalidOperationException("The IExplore.exe process can't be started");
        
        Thread.Sleep(3000);
        handle = process.MainWindowHandle.ToInt32();
        var allBrowsers = new SHDocVw.ShellWindows();
        if (handle != 0)
        {
            foreach (InternetExplorer browser in allBrowsers)
                if (browser.HWND == handle)
                {
                    ie = browser;
                    iehandle = (IntPtr)ie.HWND;
        
                    break;
                }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-06-25
          • 2011-09-21
          • 2010-12-06
          • 1970-01-01
          • 2019-04-23
          • 2023-03-04
          • 1970-01-01
          相关资源
          最近更新 更多