locked
Cast a ApplicationUnderTest to BrowserWindow RRS feed

  • Question

  • HI,

    How can I convert an ApplicationUnderTest to a BrowserWindow.

    Process[] BrowserProcesses = Process.GetProcessesByName("iexplore");
    foreach (Process process in BrowserProcesses)
    {
       ApplicationUnderTest aut = ApplicationUnderTest.FromProcess(process);
       BrowserWindow browser = (BrowserWindow)aut;
       Reporter.Log(browser.FriendlyName, Logging.Log);
    }

    I tried the above but saw:

    System.InvalidCastException: Unable to cast object of type 'Microsoft.VisualStudio.TestTools.UITesting.ApplicationUnderTest' to type 'Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow'.

     

    Thanks,

    Graham

    Thursday, June 24, 2010 3:37 PM

Answers

All replies

  • As a followup - I'm also sometimes seeing an error on the below line:

    ApplicationUnderTest aut = ApplicationUnderTest.FromProcess(process);

    Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: Unable to find the main window of the underlying process

     

    This is despite the fact that the process being passed in looks fine and the browser is definitely open?

    Thursday, June 24, 2010 3:51 PM
  • You can't cast a ApplicationUnderTest to a BrowserWindow.

    If you please explain why you need to do that, we can help better.

    Isn't "BrowserWindow.Locate(<<Title>>)" working for you?


    Please mark this post as answer if this answers your question
    Thursday, June 24, 2010 4:19 PM
    Moderator
  • Well I won't know for certain what the Title would be. I assumed that since from what I can see BrowserWindow extends AutomationUnderTest I should be able redefine an aut as a browser... 

    I originally looked at this because http://blogs.msdn.com/b/mathew_aniyan/archive/2009/02/12/hand-coding-a-coded-ui-test.aspx mentions it "You can attach to an existing browser window using the static FromProcess method."

    Thursday, June 24, 2010 4:39 PM
  • Hi Graham,

    BrowserWindow.FromProcess("iexplore") does not work due to multiple iexplore.exe process architecture in IE8.

    Can you try the following code instead:

          BrowserWindow bWindow = new BrowserWindow();
          bWindow.SearchProperties.Add(UITestControl.PropertyNames.ClassName, "IEFrame");
          UITestControlCollection coll = bWindow.FindMatchingControls();
          foreach (UITestControl uiWindow in coll)
          {
            BrowserWindow browser = uiWindow as BrowserWindow;
            Reporter.Log(browser.FriendlyName, Logging.Log);
          }

    Thanks,

    Neeraja 

    Friday, June 25, 2010 5:21 PM
    Moderator
  • Hi Neeraja,

     

    That code throws an error also on

    UITestControlCollection coll = bWindow.FindMatchingControls();

     

    System.NullReferenceException: Object reference not set to an instance of an object.
      at Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow..ctor(UITestControl control)
      at Microsoft.VisualStudio.TestTools.UITesting.BrowserWindowPropertyProvider.GetPropertyValue(UITestControl uiTestControl, String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestPropertyProvider.GetPropertyValueWrapper(UITestControl uiControl, String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValue(String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyInternal[T](String propertyName)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_WindowHandle()
      at Microsoft.VisualStudio.TestTools.UITesting.IEBrowserService.Locate(PropertyExpressionCollection searchProperties, PropertyExpressionCollection filterProperties, Boolean findAll)
      at Microsoft.VisualStudio.TestTools.UITesting.IEBrowserFactory.LocateAll(PropertyExpressionCollection searchProperties, PropertyExpressionCollection filterProperties)
      at Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow.LocateAll(PropertyExpressionCollection searchProperties, PropertyExpressionCollection filterProperties)
      at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.SearchAll(ISearchArgument searchArg)
      at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindMatchingControls()
      at QA_Automation.AUT_Actions.Actions.CloseBrowser() in C:\Users\gabell\documents\visual studio 2010\Projects\QA_Automation\QA_Automation\AUT_Actions\Actions.cs:line 80

     

    Thanks,

    Graham

    Monday, June 28, 2010 9:14 AM
    • Marked as answer by Graham Abell Monday, June 28, 2010 11:27 AM
    Monday, June 28, 2010 11:27 AM