Application.ShowForm =>System.InvalidOperationException

Class library usage, coding and language questions.
nikivancic
Posts: 10
Joined: Sun Oct 05, 2008 5:31 pm
Location: Bellingham, WA, USA
Contact:

How to associate the System.Windows.Forms.WebBrowser control

Post by nikivancic » Tue Oct 07, 2008 9:00 pm

with the Ranorex.Control instance which is the expected argument to WebDocument.GetDocument(browserInstance) method?

From what I gathered looking around, I expect the the answer is to find the handle of the System.Windows.Forms.WebBrowser control instance and use it as the argument to the Ranorex.Control constructor - but I would prefer a definitive and complete answer to a lot of experimentation.

Thanks

Nik

nikivancic
Posts: 10
Joined: Sun Oct 05, 2008 5:31 pm
Location: Bellingham, WA, USA
Contact:

Relationship between System.Windows.Forms and Ranorex object

Post by nikivancic » Tue Oct 07, 2008 9:09 pm

There is a (clearly intended) name collision - my previous post about the embedded WebBrowser control is just one example of the situation I do not completely understand. While it is obvious that you had to enhance the native browser control in order to be used in the context of your Automation environment, I am not sure what all the rules are and how are your own controls acquired from native ones (I do not expect to be able to use Ranorex Controls in the Visual Studio Designer for example, neither do I believe that you would like me to drop Visual Studio and use Ranorex Studio)

nikivancic
Posts: 10
Joined: Sun Oct 05, 2008 5:31 pm
Location: Bellingham, WA, USA
Contact:

Some more

Post by nikivancic » Wed Oct 08, 2008 1:49 am

Hello

Hoping to accelerate the process of making me understand the issues related to the embedded WebBrowser control, here is a section of code which shows my current problem. I have created a Ranorex.Control instance using the handle of the original WebBrowser control, navigated it to the proper page, and then executed the code which works perfectly with the external (not embedded) instance of IE 7. The code below fails at the line

Code: Select all

WebElement webElement = webDoc.FindSingle(userIDTextXPath);
The webElement is not found.

If the you need to actually run the code below, please let me know, so I can send you the needed password (stated as "xxxx" in the code)

Code: Select all

        private void SetupEmailServerView(WebBrowser browser, Uri uri)
        {
            bool rememberMeFlag = false;
            LoginEmailAsAdministrator(browser, rememberMeFlag);
        }

        private void LoginEmailAsAdministrator(WebBrowser browser, bool rememberMe)
        {
            string userIDTextXPath = "//input[@id='ctl00_MPH_txtUserName']";
            string passwordTextXPath = "//input[@id='ctl00_MPH_txtPassword']";
            string languageComboXPath = "//select[@id='ctl00_MPH_LanguageList']";
            string rememberMeCheckBoxXPath = "//input[@id='ctl00_MPH_chkAutoLogin']";
            string loginButtonXPath = "//td[1]/div/a[@class='ButtonBarAnchor']";
            string viewUsersXPath = "http://www.congral.net:9998/Default.aspx#page=/DomainAdmin/frmUsers.aspx?|section=UserSettings";

            IntPtr bHandle = browser.Handle;
            Ranorex.Control rBrowser = new Ranorex.Control(bHandle);
            WebDocument webDoc = WebDocument.GetDocument(rBrowser);
            webDoc.Navigate("http://www.congral.net:9998", true);

            WebElement webElement = webDoc.FindSingle(userIDTextXPath);
            Mouse.ClickWebElement(webElement, MouseButtonType.LeftButton, Alignment.Center, 0);
            webElement.Value = "[email protected]";

            webElement = webDoc.FindSingle(passwordTextXPath);
            Mouse.ClickWebElement(webElement, MouseButtonType.LeftButton, Alignment.Center, 0);
            webElement.Value = "xxxx";

            if (rememberMe)
            {
                webElement = webDoc.FindSingle(rememberMeCheckBoxXPath);
                Mouse.ClickWebElement(webElement, MouseButtonType.LeftButton, Alignment.Center, 0);
            }

            webElement = webDoc.FindSingle(languageComboXPath);
            Mouse.ClickWebElement(webElement, MouseButtonType.LeftButton, Alignment.Center, 0);
            webElement.Value = "en";

            webElement = webDoc.FindSingle(loginButtonXPath);
            Mouse.ClickWebElement(webElement, MouseButtonType.LeftButton, Alignment.Center, 0);

            webDoc.Navigate(viewUsersXPath, true);
        }


User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Thu Oct 09, 2008 3:06 pm

Hi Nik,

As far as I understand from your description the attached code works fine within a standard browser but does not work within an embedded browser? Could you please clarify your problem.

Also, could you please provide me the login password at [email protected].

kind regards,

Christoph
Support Team

nikivancic
Posts: 10
Joined: Sun Oct 05, 2008 5:31 pm
Location: Bellingham, WA, USA
Contact:

Additional problem clarification

Post by nikivancic » Thu Oct 09, 2008 4:03 pm

I may have written incorrect code, guessing how to get the Ranorex.Control object from the instance of the Microsoft's embedded browser (WebControl) instance. So my current method to get the WebDocument for a given URL is

Code: Select all

IntPtr bHandle = browser.Handle;
            Ranorex.Control rBrowser = new Ranorex.Control(bHandle);
            WebDocument webDoc = WebDocument.GetDocument(rBrowser);
            webDoc.Navigate("http://www.congral.net:9998", true);
However, if instead of the above code I use:

Code: Select all

WebDocument webDoc = WebDocument.OpenDocument("http://www.congral.net:9998", true)
the code snippet I posted works fine - as you can verify yourself, since I sent the missing password to you via email

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Fri Oct 10, 2008 9:56 am

I just tried to run your code and everything worked perfect with both the external browser and the embedded one. With the embedded browser you just need to be sure that the browser is displaying some website before you invoke the WebDocument.GetDocument() method; otherwise the document can't be found.

Regards,
Alex
Ranorex Support Team

nikivancic
Posts: 10
Joined: Sun Oct 05, 2008 5:31 pm
Location: Bellingham, WA, USA
Contact:

Confirmed

Post by nikivancic » Sat Oct 11, 2008 1:17 am

My final code is:

Code: Select all

            //
            // Ensure that the WebBrowser control has page loaded
            //
            browser.Navigate(pageURL);

            //
            // Create the Ranorex.Control instance
            //
            IntPtr bHandle = browser.Handle;
            Ranorex.Control rBrowser = new Ranorex.Control(bHandle);

            //
            // Create the WebDoument instance
            //
            WebDocument webDoc = WebDocument.GetDocument(rBrowser);
            webDoc.Navigate(pageURL, true);
Can you please verify that this is indeed the correct sequence and that all this code is needed (the immediate concern is the need to invoke the Navigate method twice)

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Mon Oct 13, 2008 8:49 am

Well, there might be a fundamental problem in your code: It seems that you call browser.Navigate(...) from your main window thread, i.e. in an EventHandler to a Form event. When you do that, the browser won't load and display the page immediately, but is only instructed to load that page. It will do that not until the event handler has been completely executed (windows message loop). So, when you execute the WebDocument.GetDocument(...) method some lines later, the browser will still not have a page loaded and the web document won't be found.

Either you initialize your web browser control with some web page by setting the Url property in the initialization and then wait for the document to be loaded before invoking your SetupEmailServerView method, or invoke it in an event handler to the WebBrowser.DocumentCompleted event.

Regards,
Alex
Ranorex Support Team