1

I use watin, because I need to open some websites in the background for which the user needs to support Javascript. I don't know if WatiN is the best for this job, but at the moment it takes very long until Internet Explorer gets visible. I need to disable to popping up of Internet Explorer while using WatiN. User doesn't need to see the opening of sites. Is it possible while using WatiN to visit a website without showing it the user or should I use another alternative which supports JS on client side? My code at the moment;

    public static void visitURL()
       {
           IE iehandler = new IE("http://www.isjavascriptenabled.com");

           if (iehandler.ContainsText("Yes"))
               Console.WriteLine("js on");
           else
               Console.WriteLine("js off");
       }
dehner
  • 47
  • 1
  • 8
  • is the goal to fetch data from another site/url or to test for javascript availability for your own purposes? If the prior - see this question/answer http://stackoverflow.com/questions/21255725/webrequest-equivalent-to-curl-command , if the later - consider including "noscript" content which advises the user that your application requires JS – Brandt Solovij Dec 11 '14 at 18:03
  • I just need to visit webpages not more. On these websites are some analyse script like google analytics or piwiik. I just need to be able to run local js so they count my visit. – dehner Dec 11 '14 at 18:16
  • check out this answer - you have some options for generating headless browsers from c# http://stackoverflow.com/questions/10161413/headless-browser-for-c-sharp-net – Brandt Solovij Dec 11 '14 at 18:21

2 Answers2

1

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.

msporek
  • 1,187
  • 8
  • 21
1

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:

Dinis Cruz
  • 4,161
  • 2
  • 31
  • 49