Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / testing

Use Selenium WebDriver UI Tests for Load Testing in the Cloud

5.00/5 (2 votes)
12 Feb 2017Ms-PL3 min read 14.1K  
Use Selenium WebDriver UI tests to create load tests executed in the Azure cloud. We will use PhantomJS and configure a load test through Visual Studio IDE.

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

Image 1

2. Assert the main headline

3. Click 'Go to the blog' button

Image 2

4. Wait for the subscribe widget to show up

Image 3

5. Assert the blog page's title

Image 4

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

C#
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

C#
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

C#
public partial class HomePage
{
    public void AssertHeadline()
    {
        Assert.IsTrue(this.MainHeadline.Text.Contains("Taking Software Quality to"));
    }
}

BlogPage

C#
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

C#
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.

Image 5

Unit Test Execution the Use Case

C#
[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.

Image 6

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

Image 7

2. Download and install Visual Studio Enterprise. You can get a free 90 days trial.

Image 8

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

Image 9

4. Use Cloud-based Load Test with Visual Studio Team Services

Image 10

5. Specify a location. You can use the default.

Image 11

6. Set up the load test duration

Image 12

7. Select a think time profile. You can use the default one.

Image 13

8. Select a load pattern. You can choose a constant or an increasing load.

Image 14

9. Select a test mix model. You can use the default settings.

Image 15

10. Add tests to a load test scenario. Choose the WebDriver test.

Image 16

11. Open the load test file and run it.

Image 17

12. Open the test results online

Image 18

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.

C#
[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

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)