I am new at selenium testing and am writing a bunch of tests for a webpage that relies heavily on javascript user interaction.
At first I wrote a lot of assertions of the style
If I press button A then
assert number of visible rows = x,
assert checkboxes checked are such
assert title = bar
.... [20 more]
and so on.
Then I switched to checksumming the HTML using MD5:
If I press button A then
assert md5(html) = 8548bccac94e35d9836f1fec0da8115c.
And it made my life a whole lot easier...
But is this a bad practice in any way?
example
- I know a HTML file a.htm is working correctly, I copy it as
a_test.htm as a testcase I make all checksums using selenium in
dictionary.txt ('show_all' : ' 8548bccac94e35d9836f1fec0da8115c, 'hide_all' :3fdec30c2731d22e2516b1cd1261a1e1, 'filter_by_id_click' : 3fdec30c2731d22e2516b1cd1261a1e1) and so on..
- The use cases are done in selenium
(driver.findbuttonShowAll.click(), assert(md5(html)==dict['show_all])
- Further development that doesn't brake expected html output is safe, when assertion fails I diff the htmls...
UPDATE : Note that taking this approach can check dynamic behaviour, because of the fact that the md5 strings are build browserspecific from html in memory. The html is extracted from a selenium webelement that in its turn is taken from the current webdriver state.
I am not changing the way selenium handles things, just comparing the underlaying html.
To get dynamic content, I pass the loaded webdriver to a helper method that extracts the outerHTML of a given xpath expression (python)
def get_md5_of_outerHTML_by_xpathget_outerHTML_by_xpath(p_driver, p_xpath_expression):
outerHTMLwebelement = get_outerHTML_by_xpathp_driver.find_element_by_xpath(p_xpath_expression)
outerHTML= p_driver.execute_script("return arguments[0].outerHTML" , p_xpath_expressionwebelement)
return get_md5_of_string(outerHTML)