Needless to say that I am a huge fan of system testing through Selenium WebDriver. You can find lots of useful information in my WebDriver Series. Usually, we use WebDriver to create GUI automated tests. However, we can go far beyond that and use our already created UI tests for load testing. In the article, I will show you how to do that using PhantomJS driver and Visual Studio. The tests will be executed in the Azure cloud using Visual Studio Team Services.
Create the UI Test for Load Testing
Use Case
The scenario that I want to load test is pretty simple. I will open the home page of Automate The Planet and assert the main headline. After that, the test will click on the 'Got to the blog' button and navigate to the blog page. There we will wait for the subscribe widget to show up and assert the title of the page.
1. Open the home page
2. Assert the main headline
3. Click 'Go to the blog' button
4. Wait for the subscribe widget to show up
5. Assert the blog page's title
Automate the Use Case through Selenium WebDriver C#
My code splits the logic into a couple of classes utilizing the Page Object Pattern. There is a page object for the home page and another for the blog. We have three files for each one of them- page, map and asserter.
HomePage
public partial class HomePage
{
private readonly IWebDriver driver;
private readonly string url = @"http://automatetheplanet.com";
public HomePage(IWebDriver browser)
{
this.driver = browser;
PageFactory.InitElements(browser, this);
}
public void Navigate()
{
this.driver.Navigate().GoToUrl(this.url);
}
public void GoToBlog()
{
this.GoToTheBlogLink.Click();
}
}
HomePage.Map
public partial class HomePage
{
[FindsBy(How = How.XPath, Using = "//*/h1")]
public IWebElement MainHeadline { get; set; }
[FindsBy(How = How.LinkText, Using = "Go to the blog")]
public IWebElement GoToTheBlogLink { get; set; }
}
HomePage.Asserter
public partial class HomePage
{
public void AssertHeadline()
{
Assert.IsTrue(this.MainHeadline.Text.Contains("Taking Software Quality to"));
}
}
BlogPage
public partial class BlogPage
{
private readonly IWebDriver driver;
private readonly string url = @"http://automatetheplanet.com/blog";
public BlogPage(IWebDriver browser)
{
this.driver = browser;
PageFactory.InitElements(browser, this);
}
public void WaitForSubscribeWidget()
{
new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementExists((By.ClassName("subscribe"))));
}
}
BlogPage.Asserter
public partial class BlogPage
{
public void AssertTitle()
{
Assert.AreEqual(this.driver.Title, "Blog - Automate The Planet");
}
}
Below you can find how the project's file structure should look like.
Unit Test Execution the Use Case
[TestClass]
public class AutomateThePlanetTest
{
private IWebDriver driver;
public TestContext TestContext { get; set; }
[TestInitialize]
public void SetupTest()
{
this.driver = new PhantomJSDriver();
this.driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
}
[TestCleanup]
public void TeardownTest()
{
this.driver.Quit();
}
[TestMethod]
public void TestInTheCloud()
{
var homePage = new HomePage(this.driver);
homePage.Navigate();
homePage.AssertHeadline();
homePage.GoToBlog();
var blogPage = new BlogPage(this.driver);
blogPage.WaitForSubscribeWidget();
blogPage.AssertTitle();
}
}
The test will be executed in a headless browser using PhantomJS.
Required NuGet Packages
To be able to run the above test you need to install three NuGet packages- Selenium.PhantomJS.WebDriver, Selenium.WebDriver and Selenium.Support.
Create a Load Test using the Selenium WebDriver UI Test
We are going to use Visual Studio Team Services to create the load test.
1. Create a free account
2. Download and install Visual Studio Enterprise. You can get a free 90 days trial.
Usually, I use the free community edition of Visual Studio. However, the load testing is not included in it. With you free account for Visual Studio Team Service, you will get 20000 virtual user minutes.
3. Create a new load test
4. Use Cloud-based Load Test with Visual Studio Team Services
5. Specify a location. You can use the default.
6. Set up the load test duration
7. Select a think time profile. You can use the default one.
8. Select a load pattern. You can choose a constant or an increasing load.
9. Select a test mix model. You can use the default settings.
10. Add tests to a load test scenario. Choose the WebDriver test.
11. Open the load test file and run it.
12. Open the test results online
Add Additional Metrics
You can add additional measurements into your tests through the usage of the MSTest TestContext. The context property will be populated automatically once the test is run.
[TestClass]
public class AutomateThePlanetTest
{
private IWebDriver driver;
public TestContext TestContext { get; set; }
[TestInitialize]
public void SetupTest()
{
this.driver = new PhantomJSDriver();
this.driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
}
[TestCleanup]
public void TeardownTest()
{
this.driver.Quit();
}
[TestMethod]
public void TestInTheCloud()
{
var homePage = new HomePage(this.driver);
this.TestContext.BeginTimer("Automate The Planet Home Page- Navigate");
homePage.Navigate();
this.TestContext.EndTimer("Automate The Planet Home Page- Navigate");
homePage.AssertHeadline();
this.TestContext.BeginTimer("Automate The Planet- Go to Blog");
homePage.GoToBlog();
var blogPage = new BlogPage(this.driver);
blogPage.WaitForSubscribeWidget();
this.TestContext.EndTimer("Automate The Planet- Go to Blog");
blogPage.AssertTitle();
}
}
So Far in the 'Pragmatic Automation with WebDriver' Series
The post Use Selenium WebDriver UI Tests for Load Testing in the Cloud appeared first on Automate The Planet.
All images are purchased from DepositPhotos.com and cannot be downloaded and used for free.
License Agreement