disable IE visibility while using WatiN

浪尽此生 提交于 2019-11-29 16:26:05

The WatIn.Core.IE class has a Visible property, you can initialize the object like that:
new WatiN.Core.IE() { Visible = true }

This way the IE will just blink on the screen when it's created, and then it will get hidden. You can later control the visibility of the IE with the ShowWindow method of WatiN.Core.IE class - I mean you can show it on the screen if you need, or you can hide again.

I use exactly that trick (of hiding IE) for writing UnitTests (using https://github.com/o2platform/FluentSharp_Fork.WatiN) that run in an hidden IE window

For example here is how I create a helper class (with an configurable hidden value)

    public IE_TeamMentor(string webRoot, string path_XmlLibraries, Uri siteUri, bool startHidden)
    {
        this.ie                = "Test_IE_TeamMentor".popupWindow(1000,700,startHidden).add_IE();            
        this.path_XmlLibraries = path_XmlLibraries;
        this.webRoot           = webRoot;
        this.siteUri           = siteUri;
    }

which is then consumed by this test:

   [Test] public void View_Markdown_Article__Edit__Save()
    {
        var article          = tmProxy.editor_Assert()                       // assert the editor user (or the calls below will fail due to security demands)
                                      .library_New_Article_New()             // create new article
                                      .assert_Not_Null(); 

        var ieTeamMentor     = this.new_IE_TeamMentor_Hidden();
        var ie               = ieTeamMentor.ie; 
        ieTeamMentor.login_Default_Admin_Account("/article/{0}".format(article.Metadata.Id));               // Login as admin and redirect to article page

        var original_Content = ie.element("guidanceItem").innerText().assert_Not_Null();                    // get reference to current content
        ie.assert_Has_Link("Markdown Editor")       
          .link           ("Markdown Editor").click();                                                      // open markdown editor page          

        ie.wait_For_Element_InnerHtml("Content").assert_Not_Null()
          .element                   ("Content").innerHtml()
                                                .assert_Is(original_Content);                               // confirm content matches what was on the view page

        var new_Content      = "This is the new content of this article".add_5_RandomLetters();             // new 'test content'

        ie.element("Content").to_Field().value(new_Content);                                                // put new content in markdown editor
        ie.button("Save").click();                                                                          // save 
        ie.wait_For_Element_InnerHtml("guidanceItem").assert_Not_Null()        
          .element                   ("guidanceItem").innerHtml()
                                                     .assert_Is("<P>{0}</P>".format(new_Content));         // confirm that 'test content' was saved ok (and was markdown transformed)            

        ieTeamMentor.close();
    }

Here are a number of posts that might help you to understand how I use it:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!