• Blog
  • Info Support
  • Career
  • Training
  • International Group
  • Info Support
  • Blog
  • Career
  • Training
  • International Group
  • Search
logo InfoSupport
  • Latest blogs
  • Popular blogs
  • Categories
      • .NET
      • Advanced Analytics
      • Agile
      • Akka
      • Alexa
      • Algorithms
      • Api's
      • Architectuur
      • Artificial Intelligence
      • ATDD
      • Augmented Reality
      • AWS
      • Azure
      • Big Data
      • Blockchain
      • Business Intelligence
      • Cloud
      • Code Combat
      • Cognitive Services
      • Communicatie
      • Containers
      • Continuous Delivery
      • CQRS
      • Cyber Security
      • Data
      • Data & Analystics
      • Data Science
      • Data Warehousing
      • Databricks
      • DevOps
      • Docker
      • eHealth
      • Enterprise Architecture
      • Hacking
      • Infrastructure & Hosting
      • Innovatie
      • Integration
      • Internet of Things
      • Java
      • Machine Learning
      • Microservices
      • Microsoft
      • Microsoft Bot Framework
      • Microsoft Data Platform
      • Mobile Development
      • Mutation Testing
      • Open source
      • Pepper
      • Power BI
      • Privacy & Ethiek
      • Python
      • Quality Assistance & Test
      • Quality Assurance & Test
      • Requirements Management
      • Scala
      • Scratch
      • Security
      • SharePoint
      • Software Architecture
      • Software development
      • Software Factory
      • SQL Server
      • SSL
      • Start-up
      • Startup thinking
      • Test Quality
      • Testing
      • TLS
      • TypeScript
      • Various
      • Web Development
      • Web-scale IT
      • Xamarin
  • Experts
      • Alles
      • Bloggers
      • Speakers
  • Meet us
  • About us
    • nl
    • en
    • .NET
    • Advanced Analytics
    • Agile
    • Akka
    • Alexa
    • Algorithms
    • Api's
    • Architectuur
    • Artificial Intelligence
    • ATDD
    • Augmented Reality
    • AWS
    • Azure
    • Big Data
    • Blockchain
    • Business Intelligence
    • Cloud
    • Code Combat
    • Cognitive Services
    • Communicatie
    • Containers
    • Continuous Delivery
    • CQRS
    • Cyber Security
    • Data
    • Data & Analystics
    • Data Science
    • Data Warehousing
    • Databricks
    • DevOps
    • Docker
    • eHealth
    • Enterprise Architecture
    • Hacking
    • Infrastructure & Hosting
    • Innovatie
    • Integration
    • Internet of Things
    • Java
    • Machine Learning
    • Microservices
    • Microsoft
    • Microsoft Bot Framework
    • Microsoft Data Platform
    • Mobile Development
    • Mutation Testing
    • Open source
    • Pepper
    • Power BI
    • Privacy & Ethiek
    • Python
    • Quality Assistance & Test
    • Quality Assurance & Test
    • Requirements Management
    • Scala
    • Scratch
    • Security
    • SharePoint
    • Software Architecture
    • Software development
    • Software Factory
    • SQL Server
    • SSL
    • Start-up
    • Startup thinking
    • Test Quality
    • Testing
    • TLS
    • TypeScript
    • Various
    • Web Development
    • Web-scale IT
    • Xamarin
    • Alles
    • Bloggers
    • Speakers
Home » How to reuse your browser window between tests in CodedUI
  • How to reuse your browser window between tests in CodedUI

    • By Marcel de Vries
    • Various 7 years ago
    • Various 0 comments
    • Various Various
    How to reuse your browser window between tests in CodedUI

    *Moved to: http://fluentbytes.com/how-to-reuse-your-browser-window-between-tests-in-codedui/

    When you run automated test scenario’s against web applications, it often takes a lot of time to get to the initial state of your application from where you want to run a test scenario.

    When you run a standard CodeUI test and use the BrowserWindow class to start the web browser, it will automatically close the browser when the test method is done. This is nice an clean and great if you don’t want previous tests to potentially influence your test run. So the fact that this is the default makes a lot of sense.

    A question that I get asked rather often is, if there is a way to reuse the browser window across different test methods. The answer is yes! this is possible although not immediately apparent how.

    so CodedUI uses a Playback Engine that gets initialized when you run the first test and that get’s cleaned up when you are done. this is done implicit by MSTest when it sees the [CodedUITest] attribute on top of your test class. This is important to know, because when the Playback Engine is not initialized and you use the CodedUI object model like for instance the BrowserWindow class, you will get error messages like:

    Microsoft.VisualStudio.TestTools.UITest.Extension.TechnologyNotSupportedException: The browser  is currently not supported.

    This even happens when you use a plain vanilla BrowserWindow.Launch call.

    So why is this important to know? Well when you want to start the BrowserWindow once for all tests, it is required to start the browser window prior to your test method. So for that you might want to leverage the [ClassInitialize] attribute. This can be placed on  a static method that has one parameter TestContext. this method will be called only once for your test class and is therefore ideal to start the Browser Window. The issue is that when this method is called the playback engine is not initialized yet, so therefore we need to initialize it ourselves. This is simple to do, by just calling the method Initialize() on the Playback object.

    But there are some more issues that make a simple and obvious solution unusable. (e.g. Create an instance of the BrowserWindow in the Initialize and then reuse it in every method) The Issue is that the Test Engine will run each test method potentially on another Thread. When you start the BrowserWindow in the initialize and then use it in every test method you would access the BrowserWindow class instance from different threads and apparently this results in unexpected behavior of the BrowserWindow class. So what we need to do is initialize a new BrowserWindow class for each test method while still reusing the same browser instance. This is possible, but the way to achieve this is by using the Process class of the BrowserWindow.

    What you do is you initialize the browser once in the Initialize method of your class and you then get the process class for that instance of the browser. Next in each test method you create a new instance of the BrowserWindow class but there we use the FromProcess() method in stead of the Launch Method. This will wrap a new Instance of the BrowserWindow on the current process, hence reusing the browser session.

    There is one final thing we need to fix, before this works. That is that after a test method completes the Browser Window is cleaned up automatically. This can be prevented by setting the property CloseOnPlaybackCleanup to false on the initial instance of the BrowserWindow we create.

    So to conclude you can have a look at the code sample below, where you can see the steps you need to take to reuse the browser window cross multiple test methods in your test class. I created a very simple test that reuses the browser that browses to the Bing home page and searches for a search term.

    [CodedUITest]
    public class ReuseTheBrowser
    {
        static Process proc = null;
    
        [ClassInitialize]
        public static void initializeTest(TestContext context)
        {
            Playback.Initialize();
            var _bw = BrowserWindow.Launch(new Uri("http://www.bing.com"));
            proc = _bw.Process;
            _bw.CloseOnPlaybackCleanup = false;
        }
    
        [ClassCleanup]
        public static void CleanUp()
        {
            BrowserWindow _bw = BrowserWindow.FromProcess(proc);
            _bw.Close();
        }
    
        [TestMethod]
        public void CodedUITestMethodFirstTest()
        {
            BrowserWindow _bw = BrowserWindow.FromProcess(proc);
            _bw.NavigateToUrl(new Uri("http://www.bing.com"));
            EnterSearchValue(_bw, "Pluralsight CodedUI training");
            ClickSearch(_bw);
        }
    
    
        [TestMethod]
        public void CodedUITestMethodSecondTest()
        {
            BrowserWindow _bw = BrowserWindow.FromProcess(proc);
            _bw.NavigateToUrl(new Uri("http://www.bing.com"));
            EnterSearchValue(_bw, "Reuse BrowserWindow cross tests");
            ClickSearch(_bw);
        }
    
        [TestMethod]
        public void CodedUITestMethodThirdTest()
        {
            BrowserWindow _bw = BrowserWindow.FromProcess(proc);
            _bw.NavigateToUrl(new Uri("http://www.bing.com"));
            EnterSearchValue(_bw, "CodedUI without UIMap");
            ClickSearch(_bw);
        }
    
        private static void ClickSearch(BrowserWindow _bw)
        {
            HtmlInputButton searchButton = new HtmlInputButton(_bw);
            searchButton.SearchProperties.Add(HtmlInputButton.PropertyNames.Id, "sb_form_go");
            Mouse.Click(searchButton);
        }
    
        private static void EnterSearchValue(BrowserWindow _bw, string value)
        {
            HtmlEdit searchBox = new HtmlEdit(_bw);
            searchBox.SearchProperties.Add(HtmlEdit.PropertyNames.Id, "sb_form_q");
            searchBox.Text = value;
        }
    }
    

    Enjoy!

    Marcel

    Follow my new blog on http://fluentbytes.com

    Share this

Marcel de Vries

View profile

IT Training at Info Support

Which training fits you?

Consultancy

Consultancy

Related blogs

  • Video Conferencing en OBS Studio koppelen: online prese…

    Video Conferencing en OBS Studio koppelen: online prese… Maaike Brouwer - 5 months ago

  • Verantwoordelijkheid pakken in jouw eigen persoonlijke …

    Verantwoordelijkheid pakken in jouw eigen persoonlijke … Stephan Versteegh - 6 months ago

  • Tips voor als je gaat afstuderen

    Tips voor als je gaat afstuderen Bart Renders - 10 months ago

Related downloads

  • Beslisboom voor een rechtmatig ‘kopietje productie’

  • Klantreferentie: Remmicom zet wetgeving om in intellige…

  • Klantreferentie RDW: Samenwerken voor veilig en vertrou…

  • Klantreferentie BeFrank: Strategische IT voor een innov…

  • Wie durft te experimenteren met data in de zorg?

Related videos

  • mijnverzekeringenopeenrij.nl

    mijnverzekeringenopeenrij.nl

  • Winnaar | Innovation Projects 2017

    Winnaar | Innovation Projects 2017

  • Explore | Info Support & HAN & Poliskluis

    Explore | Info Support & HAN & Poliskluis

  • LifeApps bij HagaZiekenhuis

    LifeApps bij HagaZiekenhuis

  • Info Support | Bedrijfsfilm

    Info Support | Bedrijfsfilm

Tagcloud

.NET .NET 3.5 .NET development Analysis and Design Anything else ASP.NET Azure BI Business Intelligence & DWH C# Coding Design Eclipse Future Stuff General .NET Development Hyper-V Java Microsoft Microsoft general Mobile Office System 2007 Off topic Other PerformancePoint PowerShell reporting services Requirements Management rup SharePoint Portal Server Silverlight software development Software Engineering SQL Server System Center Team System Virtual Server Vista Visual Studio 2008 Visual Studio Team System WCF Windows Server 2008 Windows Server 2008 (aka Longhorn) Windows Sharepoint Services v3 Windows Workflow Foundation WPF

Nieuwsbrief

* verplichte velden

Contact

  • Head office NL
  • Kruisboog 42
  • 3905 TG Veenendaal
  • T +31 318 552020
  • Call
  • Mail
  • Directions
  • Head office BE
  • Generaal De Wittelaan 17
  • bus 30 2800 Mechelen
  • T +32 15 286370
  • Call
  • Mail
  • Directions

Follow us

  • Twitter
  • Facebook
  • Linkedin
  • Youtube

Newsletter

Sign in

Extra

  • Media Library
  • Disclaimer
  • Algemene voorwaarden
  • ISHBS Webmail
  • Extranet