Fluent Bytes

"The only source of knowledge is experience" - Albert Einstein-

How to find controls in new Tabs of your browser with CodedUI

One question I got recently is if it is possible to find controls on secondary tabs that are created by an application when using CodedUI. the answer is yes and I thought it would be handy to write up how to make this happen.

For purpose of the sample I create a simple ASP.NET MVC website and changed the default page to have a link button that creates a new page in a second tab.

The page looks as follows:

image

When I click the button, then a new tab is opened and that will show the following page:

image

As you can see I have done a lot of work to make the page look pretty Smile but that was not the purpose, it is to get the point across how to find the Html on the second tab.

The way you do this is as follows:

  • You first start your test, by using the BrowserWindow class and call Launch to start the browser.
  • Next you find the HtmlHyperlink to execute the click on the button.
  • Now you have the second tab show up, and to locate that tab, you can use the BrowserWindow class again. this time you use the static method Locate and pass it in the title of the document you try to find. In my case that new tab has the Title “New Tab”
  • This locate method, returns a new BrowserWindow object that I can use in my subsequent searches for controls. So to find the text on the page I can create an HtmlDiv object, pass it in the SearchProperty Id with the value of “itemToFind” and get the InnerText value of the control to assert in my test.

The code looks as follows:

And that is it, this is the way to create tests that can work with with multiple tabs in your browser

I have the sample project up here for you to download and test yourself.

If you want to learn more about CodedUI and how this technology works, you might want to check out my course at Pluralsight on CodedUI. You can find it here and even watch it for free if you don’t have an account, by applying for a Trial.

hope this helps,
Marcel

CTO at Xpirit, Microsoft Regional Director, Visual studio ALM MVP, Speaker, Pluralsight Author and IT Architect Consultant

4 Comments

  1. I have a web table where the first row (headers) have two controls in each cell. One control controls sorting and the other control opens a popup. The first control is easy to access and click. The second control has me stumped. I can access it using TagInstance but that is not best way.
    Can you demonstrate how to access two controls in one cell of a web table?
    Thanks

    • can you provide me with a small snippet of the HTML of the table, so I can provide a simple sample?
      Has the second control any way to uniquely identify it, or is it always just the 2nth control in the cell?

      • Marcel, thanks very much for replying. The second control has no unique identifier except for the TagInstance which is not ideal. I was able to figure how to use getChildren and FindMatchingControls to successfully click on the second control. I am hoping the developers will have unique IDs for all future controls. We enjoyed your presentation at KPMG and learned lots.

        Thanks,
        Garland

  2. I should have added that I am using hand code and no UIMap.

Leave a Reply

Your email address will not be published.

*

© 2017 Fluent Bytes

Theme by Anders NorenUp ↑