Webdriver Advanced Applications-accurate comparison of page screenshot images

Source: Internet
Author: User

Determine if the two graphs are exactly the same, and if there is any inconsistency, the picture does not match and the code is as follows:

#Encoding=utf-8 fromSeleniumImportWebdriverImportUnitTest, time fromPILImportImageclassImagecompare (object):" "This class implements the algorithm of pixel alignment for two pictures, gets the number of pixels in the file and then uses a loop to compare all items of two pictures by one by one, and calculates the percentage of similarity between the results." "    defMake_regalur_image (Self, IMG, size= (256, 256)):        #force the picture size to the specified size        #and then convert it to an RGB value        returnImg.resize (size). CONVERT ('RGB')    defSplit_image (Self, IMG, part_size= (64, 64)):        #Slice a picture by a given sizeW, h =img.size PW, Ph=part_sizeassertW pw = = h% ph = =0return[Img.crop (I, J, i + PW, j +ph)). Copy () forIinchXrange (0, W, PW) forJinchxrange (0, H, ph)]defhist_similar (Self, LH, RH):#similarity frequency curves for each part of the picture after statistical segmentation        assertLen (LH) = =Len (RH)returnSUM (1-(0ifL = = RElseFloat (ABS (L-R))/Max (L, R)) forL, RinchZip (LH, RH))/Len (LH)defcalc_similar (self, Li, RI):#calculate the similarity of two pictures        returnsum (self.hist_similar (L.histogram (), R.histogram ()) forL, RinchZip (Self.split_image (LI), Self.split_image (RI)))/16.0defCalc_similar_by_path (self, LF, RF): Li, RI=self.make_regalur_image (Image.open (LF)), Self.make_regalur_image (Image.open (RF) )returnself.calc_similar (Li, RI)classTestdemo (unittest. TestCase):defsetUp (self): self. IC=Imagecompare ()#start the Firefox browserSelf.driver = Webdriver. Ie (Executable_path ="E:\\iedriverserver")    defTest_imagecomparison (self):#url = "http://doc.outofmemory.cn/python/webpy-cookbook/"URL ="http://www.sohu.com"        #Visit Sogou homepageself.driver.get (URL) time.sleep (3)        #interception first Visit Sogou homepage pictures, and save in localSelf.driver.save_screenshot ("E:\\sogou1.png") self.driver.get (URL) time.sleep (3)        #interception of a second visit to Sogou homepage pictures, and saved in the localSelf.driver.save_screenshot ("E:\\sogou2.png")        #print two pairs of similarity, 100 means exact match        PrintSelf. Ic.calc_similar_by_path ('E:\\sogou1.png','E:\\sogou2.png') * 100defTearDown (self):#Exit IE Browserself.driver.quit ()if __name__=='__main__': Unittest.main ()

Webdriver Advanced Applications-accurate comparison of page images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.