This blog is an introduction to how users can execute Selenium tests using Cloud-based Load Testing from Visual Studio Online.
Introduction
Let’s get familiar with Cloud-based Load Testing (CLT) from Visual Studio Online (VSO) and Selenium.
- Cloud-base Load Testing:
- A good introduction to CLT with VSO and key feature can be found here – Introducing Cloud-based Load Testing
- To get started quickly follow the instructions at this link – Load Testing in cloud
- Selenium
- Selenium automates browsers Quote: Selenium Official Website
- Please visit the official web site to get an overall idea of Selenium
- For this tutorial we will be specifically using a Selenium Nuget Package and Phantomjs Nuget Package in Visual Studio
Let’s get started.
Authoring Selenium unit tests in Visual Studio
- Let’s start by creating a Unit Test Project in Visual Studio. File -> New Project -> Templates -> Visual C# -> Test -> Unit Test Project
- Once the library is created, install Selenium Nuget Package. Right Click on the project -> Manage Nuget Packages… -> “Search for Selenium” -> Install
- After installing the Nuget package, the references would include WebDriver reference.
- Download required Web drivers. For the sake of this example let’s use PhantomJs. You can search for Phantomjs in the Nuget Package Manager and install it just as you did for Selenium. Change the property of phantomjs.exe “Copy to Output Directory” as “Copy if Newer”
- You can create a CS file to get started with writing Unit Tests. Let’s add a small unit test using Selenium
using Microsoft.VisualStudio.TestTools.UnitTesting;<br /> using OpenQA.Selenium;<br /> using OpenQA.Selenium.PhantomJS;<br /> using System;<br /> using System.Text;<br /> <br /> namespace SeleniumSample<br /> {<br /> [TestClass]<br /> public class SeleniumTests<br /> {<br /> [TestMethod]<br /> public void TheBingSearchTest()<br /> {<br /> TestContext.BeginTimer("BingSearchTest_Navigate");<br /> _driver.Navigate().GoToUrl("http://www.bing.com/");<br /> TestContext.EndTimer("BingSearchTest_Navigate");<br /> <br /> TestContext.BeginTimer("BingSearchTest_SearchBHarry");<br /> _driver.FindElement(By.Id("sb_form_q")).SendKeys("Brian harry blog");<br /> _driver.FindElement(By.Id("sb_form_go")).Click();<br /> TestContext.EndTimer("BingSearchTest_SearchBHarry");<br /> <br /> var elementText = _driver.FindElement(By.XPath("//ol[@id='b_results']/li/h2/a"));<br /> Assert.IsTrue(elementText.Text.Equals("Brian Harry's blog - Site Home - MSDN Blogs"), "Verified title of the blog page");<br /> }<br /> <br /> public TestContext TestContext { get; set; }<br /> <br /> #region Additional test attributes<br /> <br /> [TestInitialize]<br /> public void SetupTestSuite()<br /> {<br /> Console.WriteLine("Test init called: {0}");<br /> _driver = new PhantomJSDriver();<br /> }<br /> <br /> [TestCleanup]<br /> public void CleanupTestSuite()<br /> {<br /> _driver.Quit();<br /> }<br /> #endregion<br /> private IWebDriver _driver;<br /> }<br /> }
- Right click on the solution and add a “Test Settings” File. This is important* in order to run the above code using Test Explorer (Test -> Windows -> Test Explorer)
- Configure TestSettings to include phantomjs.exe as a deployment item
- Build the solution.
- From Test -> Test Settings -> Select Test Settings File, choose the testsettings file that is created in the previous step.
- Verify this runs on your local machine using Test Explorer (Test> Windows> Test Explorer) to check if things are working fine.
Adding Load Test for the unit tests
- Add a loadtest to the project (Right click -> Add -> Load Test …). In the load test configuration wizard, in the “Text Mix”, click add and choose unit test of your interest, for example: TheBingSearchTest
- Verify the load runs on your local machine.
Running Selenium Tests on CLT On VSO
- Congratulations you are just one click to run your tests on Microsoft Azure cloud!
- From Test -> Test Settings -> Select Test Settings File, choose the Local.testsettings file that is created when the load test project is added.
- Open this file change the option to “Run Tests using Visual Studio Online”
For more information please visit this blog to get a good understanding of how you can run your tests on Cloud – http://www.visualstudio.com/get-started/load-test-your-app-vs
- Once the run is completed and you have downloaded the report. Click on View report as mentioned in the above link.
- You would be able get a lot of insights into how the test execution happened. You can get a view of the transactions that happened in your tests (BingSearchTest_Navigate is a transaction in your test). It would appear like
Please give this a try and do let us know your feedback :). Feel free to reach us at vsoloadtest@microsoft.com
Issues
- If you try running the unit test without adding the Load Test Project you would run into System.NotSupported Exception at TestContext.BeginTimer(). It’s a known issue
- A connect bug tracking it – https://connect.microsoft.com/VisualStudio/feedback/details/774712/unit-test-testcontext-begintimer-throws-notsupportedexception
- An alternative that is blogged – http://karlz.net/blog/index.php/2012/11/17/where-is-testcontext-begintimer/
By adding a testsettings file the above issue could be avoided 🙂
0 comments