Home » Continuous Integration » Selenium Performance Improvements

Selenium Performance Improvements

loading…

As many of you know, I’ve been using Selenium to do my website testing.  And, if you’ve done any testing with Selenium yourself, you know that Selenium can be even slower if you are using Selenium Grid.

There are several things you might do today to achieve Selenium Performance improvements in order to increase the speed that your of your test run.

CacheLookup attribute (or Annotation if you are using Java)

This is probably the most obvious place for improvement.  Assuming you are using the Page Model pattern in your testing, your page model should have properties that already look something like:

[FindsBy(How = How.Id, Using = "radioButton")]
private IWebElement RadioButton{ get; set;};

What you may not know is that the way this works is that EVERY time you access the property named RadioButton it will lookup the location of the radio button.  The RadioButton property is actually a proxy for the real IWebElement that it will lookup on the fly.

So, if you are using the element more than once in your test, it will perform the lookup each time you access the RadioButton property.

By adding the CacheLookup attribute to the RadioButton property,

[FindsBy(How = How.Id, Using = "radioButton"), CacheLookup]
private IWebElement RadioButton{ get; set;};

you can force the RadioButton to be resolved once and only once.

Most of my test ask for elements once per test, so I didn’t see any major performance gains by adding this.  Your mileage may vary.

CacheLookup Warning

Be careful when adding CacheLookup to your properties because any time the HTML is recreated, the element will become “stale” and you’ll need to look it up again.

For example, in one of the pages I’m testing, I have two modal windows that get created as part of the test.  Each time they are created, they have to be resolved again because the old ones no longer exist.

It would be like trying to reference a pointer to an object that no longer exist because something else deleted it.  Actually, that’s exactly what is happening.

CacheLookup Only Solves One of Your Problems

But if all you do to your site is add CacheLookup, you are wasting your time.  You have to start thinking like your code.

Did you know that every time you access a property on an IWebElement, Selenium has to make a call to the browser to access the current value?

Yep.

Actually, if you think about it, this is what you hope it does.  It is what makes code like this work:

Wait.Until(x => Page.RadioButton.GetAttrinbute("disabled"));

But how many times to we write code that goes after a property multiple times when we know the value, or at least we assume the value, hasn’t changed?

Or how many of you are doing the same thing multiple times in the same test?

For example, in the code I’m testing currently, I have a header and a footer that I need to hide so that when an element is scrolled into view by Selenium, it doesn’t scroll under the header or the footer. I’m currently hiding and showing every time I perform some action on an element.  But now that I’m starting to think about performance, I’m asking myself, “What was I thinking?!”

In this case, I would save a lot of time by turning the header off once a the beginning of my test.  I bet my test end up running twice as fast simply by doing that.

So, keep in mind that every Selenium call you make is probably going to have to access the server in order to perform the action or retrieve the value you are looking for and try to write your code so that you are only making that call to the server one time per test.

I bet you see some performance improvements.

 

Other post in Continuous Integration

Related Post

  • Using JavaScript to Drive Selenium TestsUsing JavaScript to Drive Selenium Tests I’ve written about using Selenium to test web applications before.  But all of those articles have assumed you are using C#.  I’ve realized that Selenium has multiple language bindings […]
  • Running Selenium In Parallel With Any .NET Unit Testing ToolRunning Selenium In Parallel With Any .NET Unit Testing Tool Running Selenium in parallel from .NET seems to be a problem because, as of the time of this writing, I’ve yet to find a viable way of running selenium test on multiple browsers using […]
  • Testing EXTjs with Selenium using C#Testing EXTjs with Selenium using C# For the last two years, about 99% of the work I’ve been doing has been JavaScript programming using EXTjs.  The problems I’ve encountered along the way are primarily related to […]
  • What Browser Is Selenium Currently RunningWhat Browser Is Selenium Currently Running This probably doesn’t happen all that often, but this last week I came across the need to know which browser I was running my selenium test against.  I figured that buried deep in the […]
  • Why Johnny Can’t do Test Driven DevelopmentWhy Johnny Can’t do Test Driven Development Last week we looked at a few excuses developers give for not testing their code as they develop it (Excuses For Not Testing).  We finished that by mentioning that most of the code you […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.

One Pingback/Trackback

    31 May 2015 at 11:05am
    […] Selenium Performance Improvements – Dave M Bush Word ...
  • Automate the Planet