c# - Hash files are different after taking same screen shot with Selenium -


i'm trying learn automated tests, using selenium. i'm working alone, , have docs, google , guys.

using selenium in vs-2105, save screen shot of website image file location, , stop debugging @ point. file becomes 'expected' result.

i comment line out, run again, taking screen shot saving different location. files, although of same size, have different hash values.

to eyes identical.

is there wrong approach?

this code run create 'master'

        _webdriver.navigate().gotourl(url);          var accept = _webdriver.switchto().alert();         accept.accept();          iwebelement menu = _webdriver.findelement(by.id("link"));          menu.click();         system.threading.thread.sleep(500);          var screenshot = _webdriver.getscreenshot();          var filename = "expandmenuinplan.png";         var origfile = _testimagespersistentpath + filename;         screenshot.saveasfile(origfile, openqa.selenium.screenshotimageformat.png); 

and code use compare

        _webdriver.navigate().gotourl(url);          var accept = _webdriver.switchto().alert();         accept.accept();          iwebelement menu = _webdriver.findelement(by.id("link"));          menu.click();         system.threading.thread.sleep(500);          var screenshot = _webdriver.getscreenshot();          var filename = "expandmenuinplan.png";         var origfile = _testimagespersistentpath + filename;         //screenshot.saveasfile(origfile, openqa.selenium.screenshotimageformat.png); commented out  //the above identical          var newfile = _testimagestempfortestrunpath + filename;         screenshot.saveasfile(newfile, openqa.selenium.screenshotimageformat.png);          string hashorig = getbytes(origfile);         string hashnew = getbytes(newfile);          if (hashorig != hashnew)         {             savefailedimage(origfile, newfile, filename);         } 

and getbytes method

    private string getbytes(string file)     {         using (sha1cryptoserviceprovider sha1 = new sha1cryptoserviceprovider())         {             var img = new bitmap(file);             imageconverter converter = new imageconverter();             var bytes = (byte[])converter.convertto(img, typeof(byte[]));             return convert.tobase64string(sha1.computehash(bytes));         }     } 

is using screenshots in manner not reliable or there wrong code?

hashing image file , comparing never right approach. single pixel off change hash. don't want to make tests brittle

you can use c# method imagecomparer.compare method (image, image, colordifference, image)

https://msdn.microsoft.com/en-in/library/hh191601.aspx?f=255&mspperror=-2147217396

or can use external tools https://applitools.com/ allow compare images smart in nature

ps: have no association applitools , 1 example, there might other services available well.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -