Friday, July 25, 2014

Speeding up your test setup by launching your WebDriver asynchronously

There seems to be a much higher overhead to starting new instances of Browsers/WebDriver than it does to perform simple Rest and Database queries typically used in test setup.

Implementing a async wrapper method for instantiating new webdriver

C# offers the keyword 'async' and 'await'.  these 2 keywords allow you to turn methods into methods that return task wrappers that act like promises.

Here's how I put mine async webdriver init together

     public async Task<IWebDriver> NewWebDriverAsync()  
     {  
       var task = Task.Run(() => NewDriver());  
       return await task;  
     }  


Before (5 seconds on IE10):
     [SetUp]  
     public void SetUp()  
     {  
       //This will perform the register instance along with the first upgrade transaction data.  
       var regBuilder =  
         new RestfulRegistrationBuilder().WithApplication(ApplicationEnum.ConciergeService);  
       _registrationData = regBuilder.Build();  
       var upgradeBuilder =  
         new RestfulUpgradeTransactionBuilder().WithHandleAndAppIdFromRegistrationData(_registrationData);  
       _upgradeTransactionData = upgradeBuilder.Build();  
       _driver = NewWebDriver();      
     }  


After (2 seconds on IE10):
     [SetUp]  
     public void SetUp()  
     {  
       var driverInitTask = NewWebDriverAsync();  
       //This will perform the register instance along with the first upgrade transaction data.  
       var regBuilder =  
         new RestfulRegistrationBuilder().WithApplication(ApplicationEnum.ConciergeService);  
       _registrationData = regBuilder.Build();  
       Console.WriteLine("Registered Instance " + _registrationData);  
       var upgradeBuilder =  
         new RestfulUpgradeTransactionBuilder().WithHandleAndAppIdFromRegistrationData(_registrationData);  
       _upgradeTransactionData = upgradeBuilder.Build();  
       Console.WriteLine("Performed Upgrade " + _upgradeTransactionData);  
       _driver = driverInitTask.Result;        
     }  

No comments: