Click here to Skip to main content
14,032,416 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

7.7K views
2 bookmarked
Posted 12 Feb 2017
Licenced Ms-PL

Use Selenium WebDriver UI Tests for Load Testing in the Cloud

, 12 Feb 2017
Rate this:
Please Sign up or sign in to vote.
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

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

License

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

Share

About the Author

Anton Angelov
CEO Automate The Planet
Bulgaria Bulgaria
Anton Angelov is an IT Consultant and Quality Assurance Architect at Innovative Lab. He is passionate about automation testing and designing test harness and tools, having the best industry development practices in mind. In addition, he is an active blogger and the founder of Automate The Planet. He strives to make the site one of the leading authorities in Automation Testing by presenting compelling articles, inspiring ardent discussions amongst the community. He is also one of the most-rated-answer authors of questions about Test Automation Frameworks (WebDriver) on Stack Overflow.

You may also be interested in...

Pro
Pro

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
memberndaian_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
memberAnton Angelov12-Jun-17 3:23 
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
memberndaian_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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190419.4 | Last Updated 12 Feb 2017
Article Copyright 2017 by Anton Angelov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid