Search code examples
javacssrubyselenium-webdriversite-prism

Performance advantage between defining Css Selector using symbols or not using symbols in Selenium Test Automation?


In Automation, while we use CSS to define page elements, We can define as below ways for a sample page element:

element :fld_page_element, "#uniqueID .some-class-name"

OR,

element :fld_page_element, "[id='uniqueID'] [class='some-class-name']"

Where # symbol represents ID and . represents class attribute name.

What's the difference between them in performance wise while Test Automation comes to the picture? Or is there any advantage using over other? If it is, Please share with me.


Solution

  • I think, you are trying to do premature optimization.

    When you are designing tests, you should aim for the best "user" (i.e. developer) experience - write the readable tests which, when fail, present a clear message. As said in comments, you can spend a lot of time debugging the strangely written test, which can be a tenfold of time the "slow but readable" version of the same test would run.

    If you find your test suite to be too slow, find out the most slow tests using the tools provided by your testing framework (rspec can show the list of the slowest tests, minitest, if I remember correctly, contains tools to benchmark something, etc.) and optimize them. Most of the time, though, you would find that these tests are slow for other reasons than one you've stated.

    UPD: Take a loot at this: http://www.shouldioptimize.com/

    It allowes you to check how much computing hours of AWS EC2 instance time you can buy for what you're paid by your customer/company per hour.

    From their "Why" page:

    Microbenchmarks and language wars are fun and all, but does it really matter whether it takes 5 seconds or 0.5 seconds to calculate a million things? Maybe. Most of the time, probably not. If you're wondering whether you should spend time thinking about how to make your code faster or maybe spend that time making your code easier to read and change later, you might consider how much it actually costs to simply throw processing time at the problem.

    Related image from XKCD issue

    (Note the "across FIVE YEARS" stuff in the header of the image)