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

How to faster close a browser tab when using CodedUI

Last week I got contacted via twitter (@marcelv) with the a question regarding my Pluralsight CodedUI course and a previous blog post that shows how to use CodedUI to test applications that use multiple tabs in the browser.

This particular question was, how can I close the browser tab faster since using the BrowserWindow.Close() function takes a lot of time.

To give some more context, in this blog post you can read how to access a newly created browser tab in your test.

Now when you have found the new tab, you have a reference to that tab. This reference is a BrowserWindow class. This class has the close method on it, but if you call the method to close the tab, this can take quite some time. On my machine times varied between 8 and 14 seconds before the tab was closed. If you have an application that has a lot of tabs that you need to test, you can imagine test times add up quickly.

So how does the code look like if I close the tab using the BrowserWindow class


So what is the alternative, and how can I close the browser window in just a matter of milliseconds?

Well this is the area that CodedUI shines in my opinion (Besides the point that the close takes way to much time, which is of course not cool at all!). With CodedUI I can very easily switch UI technologies while still using the API. What I mean by that is that in stead of using the browser window and HTML controls that use the web technology driver to find element, I can just switch to using Windows Controls, that uses MSAA to access controls on a windows desktop,  and manipulate those in order to achieve my goal.

In this particular case, you need to find the tab strip in the browser window and in the tab strip find the tab you want to close. If you run your test in internet explorer, the tab contains a close button that you can then find and click.

The following lines of code achieve this goal (assuming the tab has the title “New Tab” as with the example in my previous post):


So what is the difference in speed her?

Well using the BrowserWindow class it took between 8 – 14 seconds to close the tab, with the alternative approach using windows controls it takes about 500ms.

there is a downside to this approach that you need to be aware of, when you run multi browser tests with the selenium plugin that MS provides via the visual studio code gallery, you need to be aware of the fact that using the BrowserWindow class works cross browser and using the alternative only works for the browser you design the close for. So if I want to achieve the same on Chrome or Firefox, then I need to figure out which windows controls they use for their tabs and how I can click the close button there as well.

Hope this helps


1 Comment

  1. Vishal

    Hi Marcel

    That is a great article. Very Useful.

    Just one doubt, the title of my newly opened tab is same as that of main tab. I need to validate some data on newly opened tab. How can I do that as I can not use LOCATE() method here.


Leave a Reply

Your email address will not be published. Required fields are marked *

© 2020 Fluent Bytes

Theme by Anders NorenUp ↑