Artur Udod
Artur Udod

Reputation: 4743

Measure performance using SeleniumWebDriver vs native javascript

Let's assume that some action is performed when I click the button on a web-page. Let's say it takes X seconds and during these seconds a div is displayed in the center of the page. When action processing is finished the div is removed and focus goes to element E.

1) I've written a test in Selenium ( C# ):

stopwatch.Restart();
button.Click();
while (driver.FindElementsById(PopupDivId).Count != 0)
                {
                    Thread.Sleep(10);
                }
stopwatch.Stop();

2 ) And a test in javascript (inside my page). Simplified code:

OnClick button handler:

console.time('test');

GotFocus handler on textbox (element E):

console.log(document.getElementById('My_PopupDivId')); // just to be sure - it returns null
console.timeEnd('test');

For some reason Selenium measurements are ~2x bigger than direct javascript measurements. (400ms vs 800ms).

Is this something wrong with my code or it's just Selenium inaccuracy ( does it actually make sense to measure such things as javascript/DOM performance using Selenium?)

Upvotes: 0

Views: 551

Answers (1)

Louis
Louis

Reputation: 151380

John Koerner's comment is right on the money. You are comparing apples and oranges. Here are some factors affecting the difference:

  1. Selenium uses a wire protocol to talk to your browser. It has to take your parameters, marshal them, send them to the browser, wait for a response, unmarshal the return value and give it to your code. That's in addition to whatever DOM operations are performed.

  2. Your Selenium timing include the click operation as part of what is timed, whereas you start your JavaScript timing inside the event handler for the click. Selenium does some housekeeping work when you ask it to click on a button, like for instance check whether it is visible and bring it in to view if it is not visible. Your Selenium timing includes the cost of this, but your JavaScript timing does not.

  3. Your Selenium code is subject to whatever implicit wait value is set. I don't typically use implicit wait but I've just tested it and found that if I ask Selenium for a list of elements, it will wait until the implicit wait has elapsed before telling me there are none.

To get a better idea of how much Selenium is affecting your results, you should try your test with different delays between the appearance of the div and its removal. I really doubt that a case where your JavaScript timing would yield 2 seconds would translate to a 4 second timing in Selenium.

As for using Selenium for performance measurements, I'd say Selenium is a very blunt tool for such task. When I'm worried about code performance in a browser, I skip Selenium and use JavaScript directly. I have used Selenium to get an idea about performance, in the aggregate. For instance, there's been times I reworked some core part of an application and then ran a test suite that uses Selenium and found the time to run the whole test suite significantly improved. However, as I said, this is blunt. We're talking seconds of difference, not milliseconds.

Upvotes: 1

Related Questions