Click here to Skip to main content
15,884,425 members
Articles / DevOps / Testing
Technical Blog

Use Selenium WebDriver UI Tests for Load Testing in the Cloud

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Feb 2017Ms-PL3 min read 13.6K   2   3
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)


Written By
CEO Automate The Planet
Bulgaria Bulgaria
CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.

Comments and Discussions

 
QuestionOpenQA.Selenium.DriverServiceNotFoundException: The PhantomJS.exe file does not exist in the current directory or in a directory on the PATH environment variable Pin
ndaian_0089-Jun-17 12:20
ndaian_0089-Jun-17 12:20 
AnswerRe: OpenQA.Selenium.DriverServiceNotFoundException: The PhantomJS.exe file does not exist in the current directory or in a directory on the PATH environment variable Pin
Anton Angelov12-Jun-17 3:23
Anton Angelov12-Jun-17 3:23 
I guess if you use MSTest to run your tests, you need to use settings file and in it there is a section deployment items and you need to add this one there to be copied. After it is copied in the bin I guess you need to use the overloaded constructor of the PhantomJS pointing the path where the executable is copied instead of getting it from the PATH variables since I don't know how you can change them on the cloud machines.

I hope that this helps you somehow. I don't have any other suggestions for now. Please, share when you manage to fix it and I will update the article accordingly.

Thanks,
Anton
GeneralRe: OpenQA.Selenium.DriverServiceNotFoundException: The PhantomJS.exe file does not exist in the current directory or in a directory on the PATH environment variable Pin
ndaian_00812-Jun-17 13:11
ndaian_00812-Jun-17 13:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.