def test_no_next_snapshots(self): """ check that we do not look at ourself when searching next snapshots """ s1 = Snapshot(stepResult=self.tsr1, image=None, refSnapshot=None, pixelsDiff=None) s1.save() s2 = Snapshot(stepResult=self.tsr2, image=None, refSnapshot=s1, pixelsDiff=None) s2.save() self.assertEqual(s2.snapshotsUntilNextRef(s2.refSnapshot), [], "No next snapshot should be found")
def test_is_ko_with_snapshots_ko_and_result_ok(self): """ Step is KO when at least one snapshot is KO even if step result is OK """ tcs = TestCaseInSession.objects.get(pk=5) step = TestStep.objects.get(pk=2) step_result = StepResult(step=step, testCase=tcs, result=True) # step without snapshots step_result.save() # one snapshot with comparison error snapshot1 = Snapshot(stepResult=step_result, image=None, refSnapshot=None, pixelsDiff=b"12345", tooManyDiffs=True) snapshot1.save() # one snapshot without comparison error snapshot2 = Snapshot(stepResult=step_result, image=None, refSnapshot=None, pixelsDiff=None, tooManyDiffs=False) snapshot2.save() # Step is KO because one snapshot has comparison error self.assertFalse(step.isOkWithSnapshots(tcs))
def test_delete_old_sessions_with_no_reference(self): """ Delete sessions that have snapshots (but no reference, with a ttl > 0, which are older than the ttl """ s1 = TestSession(sessionId="1234", date=timezone.now() - datetime.timedelta(days=4), browser="firefox", environment=TestEnvironment.objects.get(pk=1), version=Version.objects.get(pk=1), ttl=datetime.timedelta(days=0)) s1.save() step = TestStep(name="step1") step.save() tc1 = TestCase(name="case1", application=Application.objects.get(pk=1)) tc1.save() tcs1 = TestCaseInSession(testCase=tc1, session=s1) tcs1.save() tcs1.testSteps.set([step]) tcs1.save() tsr1 = StepResult(step=step, testCase=tcs1, result=True) tsr1.save() # add a snapshot without reference => its a reference itself sn0 = Snapshot(stepResult=StepResult.objects.get(pk=1), image=None, refSnapshot=None, pixelsDiff=None) sn0.save() sn1 = Snapshot(stepResult=tsr1, image=None, refSnapshot=sn0, pixelsDiff=None) sn1.save() s1.ttl = datetime.timedelta(days=3) s1.save() # s1 should have been deleted self.assertRaises(TestSession.DoesNotExist, TestSession.objects.get, pk=s1.id)
def test_snapshot_deletion_of_last_snapshot(self): """ Test that nothing happens when the deleted snapshot is the last one Here, S1 is a reference S2 has a reference on S1 S3 has a reference on S1 After deletion of S1, S2 becomes a reference S3 should have reference on S2 """ s1 = Snapshot(stepResult=self.tsr1, image=None, refSnapshot=None, pixelsDiff=None) s1.save() s2 = Snapshot(stepResult=self.tsr2, image=None, refSnapshot=s1, pixelsDiff=None) s2.save() s2.delete() self.assertEqual(Snapshot.objects.get(pk=s1.id).refSnapshot, None)
def test_is_ok_with_all_snapshot_all_computing_error(self): """ 2 snapshots, all have computing errors Result is undefined, we return None """ tcs = TestCaseInSession.objects.get(pk=10) st1 = StepResult.objects.get(pk=12) st2 = StepResult.objects.get(pk=13) initial_ref_snapshot = Snapshot.objects.get(id=1) s1 = Snapshot(stepResult=st1, refSnapshot=initial_ref_snapshot, pixelsDiff=None) s1.computingError = 'some error' s1.save() s2 = Snapshot(stepResult=st2, refSnapshot=initial_ref_snapshot, pixelsDiff=None) s2.computingError = 'some error' s2.save() self.assertIsNone(tcs.isOkWithSnapshots())
def test_is_ok_with_all_snapshot_ok(self): tcs = TestCaseInSession.objects.get(pk=10) st1 = StepResult.objects.get(pk=12) st2 = StepResult.objects.get(pk=13) initial_ref_snapshot = Snapshot.objects.get(id=1) s1 = Snapshot(stepResult=st1, refSnapshot=initial_ref_snapshot, pixelsDiff=None) s1.save() s2 = Snapshot(stepResult=st2, refSnapshot=initial_ref_snapshot, pixelsDiff=None) s2.save() self.assertTrue(tcs.isOkWithSnapshots())
def test_compute_now_no_save(self): """ Check snapshot is not saved if it's not requested """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile, name="img.png") s1 = Snapshot(stepResult=StepResult.objects.get(id=1), image=img, refSnapshot=None, pixelsDiff=None) # as we do not save any snapshot, image has to be copied manually where DiffComputer expects it shutil.copyfile("snapshotServer/tests/data/test_Image1.png", settings.MEDIA_ROOT + os.sep + "img.png") s2 = Snapshot(stepResult=StepResult.objects.get(id=2), image=img, refSnapshot=None, pixelsDiff=None) self.assertFalse(s1.computed) self.assertFalse(s2.computed) DiffComputer.get_instance().compute_now(s1, s2, save_snapshot=False) # something has been computed self.assertIsNotNone(s2.pixelsDiff) self.assertEqual(s2.refSnapshot, s1, "refSnapshot should have been updated") self.assertIsNone(s1.id) self.assertIsNone(s2.id) self.assertFalse( s1.computed) # reference picture, computed flag remains False self.assertTrue(s2.computed) # diff computed, flag changed
def test_is_ok_with_all_snapshot_ko_and_one_computing_error(self): """ 2 snapshots whose one has computing error, other is ko Result is ko, we return False """ tcs = TestCaseInSession.objects.get(pk=10) st1 = StepResult.objects.get(pk=12) st2 = StepResult.objects.get(pk=13) initial_ref_snapshot = Snapshot.objects.get(id=1) s1 = Snapshot(stepResult=st1, refSnapshot=initial_ref_snapshot, pixelsDiff=pickle.dumps([(1, 1)])) s1.save() s2 = Snapshot(stepResult=st2, refSnapshot=initial_ref_snapshot, pixelsDiff=None) s2.computingError = 'some error' s2.save() self.assertFalse(tcs.isOkWithSnapshots())
def test_ref_image_is_none(self): """ Check no error is raised when image data is missing """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) s1 = Snapshot(stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) s1.save() s2 = Snapshot(stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=b'') s2.save() s2.image.save("img", img) s2.save() DiffComputer.get_instance().compute_now(s1, s2) # something has been computed self.assertIsNone(s2.pixelsDiff) self.assertEqual(s2.refSnapshot, s1, "refSnapshot should have been updated")
def test_is_ok_with_all_snapshot_ok_2(self): """ Same as above but content of pixelDiffs is an empty list """ tcs = TestCaseInSession.objects.get(pk=5) s1 = StepResult.objects.get(pk=5) s2 = StepResult.objects.get(pk=6) initial_ref_snapshot = Snapshot.objects.get(id=1) s1 = Snapshot(stepResult=s1, refSnapshot=initial_ref_snapshot, pixelsDiff=pickle.dumps([])) s1.save() s2 = Snapshot(stepResult=s2, refSnapshot=initial_ref_snapshot, pixelsDiff=pickle.dumps([])) s2.save() self.assertTrue(tcs.isOkWithSnapshots())
def test_error_while_computing(self): """ Check that if an error occurs during computing, """ s1 = Snapshot(stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) s1.save() s2 = Snapshot(stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=None) s2.save() diff_computer = DiffComputer.get_instance() diff_computer._compute_diff = MagicMock( side_effect=Exception("error while computing")) diff_computer.add_jobs(s1, s2, check_test_mode=False) time.sleep(0.7) self.assertIsNotNone(DiffComputer._instance, "thread should still be running")
def test_is_ok_with_all_snapshot_ko(self): """ Test case when at least one snapshot comparison is KO """ tcs = TestCaseInSession.objects.get(pk=5) s1 = StepResult.objects.get(pk=5) s2 = StepResult.objects.get(pk=6) initial_ref_snapshot = Snapshot.objects.get(id=1) # some diffs for first picture s1 = Snapshot(stepResult=s1, refSnapshot=initial_ref_snapshot, pixelsDiff=pickle.dumps([(1, 1)])) s1.save() s2 = Snapshot(stepResult=s2, refSnapshot=initial_ref_snapshot, pixelsDiff=pickle.dumps([])) s2.save() self.assertFalse(tcs.isOkWithSnapshots())
def test_ref_is_none(self): """ Check no error is raised when reference is None. Nothing happens for the stepSnapshot """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) s2 = Snapshot(stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=b'') s2.save() s2.image.save("img", img) s2.save() DiffComputer.get_instance().compute_now(None, s2) # something has been computed self.assertIsNone(s2.pixelsDiff) self.assertIsNone(s2.refSnapshot, "refSnapshot should have not been updated")
def test_add_jobs(self): """ Check we can add jobs and they get computed """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) s1 = Snapshot(stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) s1.save() s1.image.save("img", img) s1.save() s2 = Snapshot(stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=None) s2.save() s2.image.save("img", img) s2.save() s3 = Snapshot(stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) s3.save() s3.image.save("img", img) s3.save() diff_computer = DiffComputer.get_instance() diff_computer.add_jobs(s1, s2, check_test_mode=False) diff_computer.add_jobs(s1, s3, check_test_mode=False) time.sleep(1) diff_computer.stopThread() # something has been computed self.assertIsNotNone(s2.pixelsDiff) self.assertIsNotNone(s3.pixelsDiff)
def test_remove_ref(self): """ From a picture which is a reference (snapshot_ref_same_env), remove the reference flag. Next snpashots (snapshot_same_env) should then refere to the last reference available """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) snapshot_ref_same_env = Snapshot(stepResult=self.sr1, refSnapshot=None, pixelsDiff=None) snapshot_ref_same_env.save() snapshot_ref_same_env.image.save("img", img) snapshot_ref_same_env.save() snapshot_same_env = Snapshot(stepResult=self.step_result_same_env, refSnapshot=snapshot_ref_same_env, pixelsDiff=None) snapshot_same_env.save() snapshot_same_env.image.save("img", img) snapshot_same_env.save() response = self.client.get( reverse('pictureView', kwargs={ 'testCaseInSessionId': self.tcs1.id, 'testStepId': 1 }) + "?makeRef=False&snapshotId=" + str(snapshot_ref_same_env.id)) # check display self.assertEqual(response.context['captureList'][0]['reference'], self.initialRefSnapshot, "new reference should be the first snapshot") self.assertEqual( response.context['captureList'][0]['stepSnapshot'].refSnapshot, self.initialRefSnapshot, "new reference should be the first snapshot") self.assertIsNotNone( response.context['captureList'][0]['stepSnapshot'].pixelsDiff) self.assertEqual( response.context['captureList'][0]['diffPercentage'], 0.0) DiffComputer.stopThread() # check snapshot_same_env ref as been changed self.assertEqual( Snapshot.objects.get(id=snapshot_same_env.id).refSnapshot, self.initialRefSnapshot, "ref snapshot for snapshot_same_env should have changed to first snapshot" )
def compare_or_store_snapshot(self, form, step_result): image = form.cleaned_data['image'] # the image file name = form.cleaned_data['name'] # name of the image store_snapshot = form.cleaned_data[ 'storeSnapshot'] # do we store the image or not. If False, only comparison is returned diff_tolerance = form.cleaned_data.get( 'diffTolerance', 0.0) # percentage of admissible error compare_option = form.cleaned_data.get('compare', 'true') # how we compare image # check if a reference exists for this step in the same test case / same application / same version / same environment / same browser / same name most_recent_reference_snapshot = Snapshot.objects.filter( stepResult__step=step_result.step, # same step in the test case stepResult__testCase__testCase__name=step_result.testCase.testCase. name, # same test case stepResult__testCase__session__version=step_result.testCase. session.version, # same version stepResult__testCase__session__environment=step_result.testCase. session.environment, # same environment stepResult__testCase__session__browser=step_result.testCase. session.browser, # same browser refSnapshot=None, # a reference image name=name).order_by('pk').last() # same snapshot name # check for a reference in previous versions if not most_recent_reference_snapshot: for app_version in reversed( step_result.testCase.session.version.previousVersions()): most_recent_reference_snapshot = Snapshot.objects.filter( stepResult__step=step_result.step, stepResult__testCase__testCase__name=step_result.testCase. testCase.name, stepResult__testCase__session__version=app_version, stepResult__testCase__session__environment=step_result. testCase.session.environment, stepResult__testCase__session__browser=step_result. testCase.session.browser, refSnapshot=None, name=name).order_by('pk').last() if most_recent_reference_snapshot: break diff_pixels_percentage = 0.0 if most_recent_reference_snapshot: step_snapshot = Snapshot( stepResult=step_result, image=image, refSnapshot=most_recent_reference_snapshot, name=name, compareOption=compare_option, diffTolerance=diff_tolerance) if store_snapshot: step_snapshot.save() # compute difference if a reference already exist DiffComputer.get_instance().add_jobs( most_recent_reference_snapshot, step_snapshot) else: try: # as we don't save step_snapshot (it's temp computation), we still save the image data temporary at the location it's expected with open(step_snapshot.image.path, 'wb') as img: img.write(image.read()) img.flush() DiffComputer.get_instance().compute_now( most_recent_reference_snapshot, step_snapshot, save_snapshot=False) finally: try: os.remove(step_snapshot.image.path) except Exception as e: pass if step_snapshot.pixelsDiff is not None: with io.BytesIO(step_snapshot.pixelsDiff) as input: diff_pixels_percentage = 100 * ( sum(list(Image.open(input).getdata(3))) / 255) / (step_snapshot.image.width * step_snapshot.image.height) else: # snapshot is marked as computed as this is a reference snapshot step_snapshot = Snapshot(stepResult=step_result, image=image, refSnapshot=None, name=name, compareOption=compare_option, computed=True, diffTolerance=diff_tolerance) if store_snapshot: step_snapshot.save() return HttpResponse(json.dumps({ 'id': step_snapshot.id, 'computed': step_snapshot.computed, 'computingError': step_snapshot.computingError, 'diffPixelPercentage': diff_pixels_percentage, 'tooManyDiffs': step_snapshot.tooManyDiffs }), content_type='application/json', status=201)
def test_next_snapshots_with_no_ref(self): """ Search for next snapshot that reference ourself """ s1 = Snapshot(stepResult=self.tsr1, image=None, refSnapshot=None, pixelsDiff=None) s1.save() s2 = Snapshot(stepResult=self.tsr2, image=None, refSnapshot=s1, pixelsDiff=None) s2.save() # s3 should not be found has it does not reference s1 s3 = Snapshot(stepResult=self.tsr2, image=None, refSnapshot=s2, pixelsDiff=None) s3.save() self.assertEqual(s1.snapshotsUntilNextRef(s1), [s2], "One snapshot should be found")
def test_next_snapshots_with_ref(self): """ Check that the next reference snapshot (s4) is not rendered but pictures from the next version are """ # snapshots on app v1 s1 = Snapshot(stepResult=self.tsr1, image=None, refSnapshot=None, pixelsDiff=None) s1.save() s2 = Snapshot(stepResult=self.tsr2, image=None, refSnapshot=s1, pixelsDiff=None) s2.save() # snapshots on app v2 s3 = Snapshot(stepResult=self.tsr3, image=None, refSnapshot=s1, pixelsDiff=None) s3.save() s4 = Snapshot(stepResult=self.tsr4, image=None, refSnapshot=None, pixelsDiff=None) s4.save() self.assertEqual(s1.snapshotsUntilNextRef(s1), [s2, s3], "2 snapshots should be found")
def test_delete_old_sessions_with_reference(self): """ Do not delete sessions that have reference snapshots, with a ttl > 0, which are older than the ttl """ s1 = TestSession(sessionId="1234", date=timezone.now() - datetime.timedelta(days=4), browser="firefox", environment=TestEnvironment.objects.get(pk=1), version=Version.objects.get(pk=1), ttl=datetime.timedelta(days=0)) s1.save() step1 = TestStep(name="step1") step1.save() step2 = TestStep(name="step2") step2.save() tc1 = TestCase(name="case1", application=Application.objects.get(pk=1)) tc1.save() tcs1 = TestCaseInSession(testCase=tc1, session=s1) tcs1.save() tcs1.testSteps.set([step1]) tcs1.save() tsr1 = StepResult(step=step1, testCase=tcs1, result=True) tsr1.save() tsr2 = StepResult(step=step2, testCase=tcs1, result=True) tsr2.save() # add a snapshot without reference => its a reference itself sn1 = Snapshot(stepResult=tsr1, image=None, refSnapshot=None, pixelsDiff=None) sn1.save() sn2 = Snapshot(stepResult=tsr2, image=None, refSnapshot=sn1, pixelsDiff=None) sn2.save() s1.ttl = datetime.timedelta(days=3) s1.save() # old session without reference s2 = TestSession(sessionId="1235", date=timezone.now() - datetime.timedelta(days=4), browser="firefox", environment=TestEnvironment.objects.get(pk=1), version=Version.objects.get(pk=1), ttl=datetime.timedelta(days=0)) s2.save() tcs2 = TestCaseInSession(testCase=tc1, session=s2) tcs2.save() tcs2.testSteps.set([step1]) tcs2.save() tsr2 = StepResult(step=step1, testCase=tcs2, result=True) tsr2.save() # add a snapshot with reference sn2 = Snapshot(stepResult=tsr2, image=None, refSnapshot=sn1, pixelsDiff=None) sn2.save() s2.ttl = datetime.timedelta(days=3) s2.save() # session1 should not be deleted TestSession.objects.get(pk=s1.id) # session2 should be deleted self.assertRaises(TestSession.DoesNotExist, TestSession.objects.get, pk=s2.id)
def test_make_new_ref(self): """ From a picture which is not a reference (snapshot_future_ref_same_env), make it a new ref 'snapshot_same_env' should then have 'snapshot_future_ref_same_env' as reference because it has a higher id than 'initialRefSnapshot' and same name / browser / environment / version """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) snapshot_future_ref_same_env = Snapshot( stepResult=self.sr1, refSnapshot=self.initialRefSnapshot, pixelsDiff=None) snapshot_future_ref_same_env.save() snapshot_future_ref_same_env.image.save("img", img) snapshot_future_ref_same_env.save() exclusion1 = ExcludeZone(x=0, y=0, width=10, height=10, snapshot=self.initialRefSnapshot) exclusion1.save() exclusion2 = ExcludeZone(x=10, y=10, width=10, height=10, snapshot=self.initialRefSnapshot) exclusion2.save() self.assertEqual( len( ExcludeZone.objects.filter( snapshot=self.initialRefSnapshot)), 2) self.assertEqual( len( ExcludeZone.objects.filter( snapshot=snapshot_future_ref_same_env)), 0) snapshot_same_env = Snapshot(stepResult=self.step_result_same_env, refSnapshot=self.initialRefSnapshot, pixelsDiff=None) snapshot_same_env.save() snapshot_same_env.image.save("img", img) snapshot_same_env.save() response = self.client.get( reverse('pictureView', kwargs={ 'testCaseInSessionId': self.tcs1.id, 'testStepId': 1 }) + "?makeRef=True&snapshotId=" + str(snapshot_future_ref_same_env.id)) # check display self.assertIsNone(response.context['captureList'][0]['reference'], "new reference should be the snapshot itself") self.assertIsNone( response.context['captureList'][0]['stepSnapshot'].refSnapshot, "new reference should be the snapshot itself") self.assertIsNone( response.context['captureList'][0]['stepSnapshot'].pixelsDiff) DiffComputer.stopThread() # check snapshot_same_env ref as been changed self.assertEqual( Snapshot.objects.get(id=snapshot_same_env.id).refSnapshot, snapshot_future_ref_same_env, "ref snapshot for snapshot_same_env should have changed to snapshot_future_ref_same_env" ) self.assertEqual( Snapshot.objects.get(id=2).refSnapshot, self.initialRefSnapshot, "snapshot previous to snapshot_future_ref_same_env should not have change" ) # check 'initialRefSnapshot' has kept its references self.assertEqual( len( ExcludeZone.objects.filter( snapshot=self.initialRefSnapshot)), 2) # check new ref 'snapshot_future_ref_same_env' has got a copy of the exclusion zones self.assertEqual( len( ExcludeZone.objects.filter( snapshot=snapshot_future_ref_same_env)), 2)
def test_compute_diff_without_tolerance(self): """ Check difference is found between two different images """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as reference: with open("snapshotServer/tests/data/test_Image1Mod.png", 'rb') as step: img_reference = ImageFile(reference) img_step = ImageFile(step) ref_snapshot = Snapshot( stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) ref_snapshot.save() ref_snapshot.image.save("img", img_reference) ref_snapshot.save() step_snapshot = Snapshot( stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=None) step_snapshot.save() step_snapshot.image.save("img", img_step) step_snapshot.save() DiffComputer.get_instance().compute_now( ref_snapshot, step_snapshot) # something has been computed self.assertIsNotNone(step_snapshot.pixelsDiff) self.assertTrue(step_snapshot.tooManyDiffs) self.assertEqual(step_snapshot.refSnapshot, ref_snapshot, "refSnapshot should have been updated") self.assertTrue(step_snapshot.computed) self.assertTrue(step_snapshot.computingError == '')
def test_too_high_diff_tolerance(self): """ tolerance > 100 should be refused """ s1 = Snapshot(stepResult=self.tsr1, image=None, refSnapshot=None, pixelsDiff=None, diffTolerance=100.1) self.assertRaises(IntegrityError, s1.save)
def test_error_message_reset(self): """ Check that if an error occurs during _compute_diff() method, 'computed' flag is still set to 'True' whatever the result is, and 'computingError' is filled If an other computing is done, and no error occurs, then, 'computingError' is reset to an empty string """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as reference: with open("snapshotServer/tests/data/test_Image1Mod.png", 'rb') as step: img_reference = ImageFile(reference) img_step = ImageFile(step) ref_snapshot = Snapshot( stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) ref_snapshot.save() ref_snapshot.image.save("img", img_reference) ref_snapshot.save() step_snapshot = Snapshot( stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=None) step_snapshot.save() step_snapshot.image.save("img", img_step) step_snapshot.save() diff_computer = DiffComputer.get_instance() diff_computer.mark_diff = MagicMock( side_effect=Exception("error while computing")) diff_computer.add_jobs(ref_snapshot, step_snapshot, check_test_mode=True) time.sleep(1) self.assertIsNotNone(DiffComputer._instance, "thread should still be running") # check error has been saved self.assertTrue( Snapshot.objects.get(id=step_snapshot.id).computed) self.assertEqual( Snapshot.objects.get(id=step_snapshot.id).computingError, "error while computing") # check error has been removed as computing is ok DiffComputer.stopThread() # reset DiffComputer instance diff_computer_ok = DiffComputer.get_instance() diff_computer_ok.add_jobs(ref_snapshot, step_snapshot, check_test_mode=True) time.sleep(1) self.assertTrue( Snapshot.objects.get(id=step_snapshot.id).computed) self.assertEqual( Snapshot.objects.get(id=step_snapshot.id).computingError, "")
def test_multiple_snapshots_per_step(self): """ Test that all snapshots are returned when there are multiple in one test """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) snapshot_ref_same_env = Snapshot( stepResult=self.sr1, refSnapshot=self.initialRefSnapshot, pixelsDiff=None, name='cap1') snapshot_ref_same_env.save() snapshot_ref_same_env.image.save("img", img) snapshot_ref_same_env.save() snapshot_same_env = Snapshot(stepResult=self.sr1, refSnapshot=self.initialRefSnapshot, pixelsDiff=None, name='cap2') snapshot_same_env.save() snapshot_same_env.image.save("img", img) snapshot_same_env.save() response = self.client.get( reverse('pictureView', kwargs={ 'testCaseInSessionId': self.tcs1.id, 'testStepId': 1 })) self.assertEqual(len(response.context['captureList']), 2, "2 snapshots should be returned") self.assertEqual(response.context['captureList'][0]['name'], 'cap1') self.assertEqual(response.context['captureList'][1]['name'], 'cap2')
def test_make_new_ref_diff_image(self): """ Compare 2 pictures (force computation to be sure we have diff data) Check we get the diff percentage """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: with open("snapshotServer/tests/data/test_Image1Mod.png", 'rb') as img_file_mod: img = ImageFile(imgFile) img_mod = ImageFile(img_file_mod) snapshot_future_ref_same_env = Snapshot( stepResult=self.sr1, refSnapshot=self.initialRefSnapshot, pixelsDiff=None) snapshot_future_ref_same_env.save() snapshot_future_ref_same_env.image.save("img", img) snapshot_future_ref_same_env.save() exclusion1 = ExcludeZone(x=0, y=0, width=10, height=10, snapshot=self.initialRefSnapshot) exclusion1.save() exclusion2 = ExcludeZone(x=10, y=10, width=10, height=10, snapshot=self.initialRefSnapshot) exclusion2.save() self.assertEqual( len( ExcludeZone.objects.filter( snapshot=self.initialRefSnapshot)), 2) self.assertEqual( len( ExcludeZone.objects.filter( snapshot=snapshot_future_ref_same_env)), 0) snapshot_same_env = Snapshot( stepResult=self.step_result_same_env, refSnapshot=self.initialRefSnapshot, pixelsDiff=None) snapshot_same_env.save() snapshot_same_env.image.save("img", img_mod) snapshot_same_env.save() # force computing self.client.get( reverse('pictureView', kwargs={ 'testCaseInSessionId': self.tcs1.id, 'testStepId': 1 }) + "?makeRef=True&snapshotId=" + str(snapshot_future_ref_same_env.id)) DiffComputer.stopThread() # ask for the step snapshot and look for data response = self.client.get( reverse('pictureView', kwargs={ 'testCaseInSessionId': self.tcs_same_env.id, 'testStepId': 1 }) + "?snapshotId=" + str(snapshot_same_env.id)) self.assertIsNotNone(response.context['captureList'][0] ['stepSnapshot'].pixelsDiff) self.assertTrue(response.context['captureList'][0] ['diffPercentage'] > 0.086)
def test_compute_diff_with_tolerance_lower_than_difference(self): """ Check that tooManyDiffs is True as % of differences is higher than tolerance """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as reference: with open("snapshotServer/tests/data/test_Image1Mod.png", 'rb') as step: img_reference = ImageFile(reference) img_step = ImageFile(step) ref_snapshot = Snapshot( stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) ref_snapshot.save() ref_snapshot.image.save("img", img_reference) ref_snapshot.save() step_snapshot = Snapshot( stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=None, diffTolerance=0.005) step_snapshot.save() step_snapshot.image.save("img", img_step) step_snapshot.save() DiffComputer.get_instance().compute_now( ref_snapshot, step_snapshot) # something has been computed self.assertIsNotNone(step_snapshot.pixelsDiff) self.assertTrue(step_snapshot.tooManyDiffs)
def test_compute_now(self): """ Check thread is not started when computing is requested to be done now """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) s1 = Snapshot(stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) s1.save() s1.image.save("img", img) s1.save() s2 = Snapshot(stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=None) s2.save() s2.image.save("img", img) s2.save() self.assertFalse(s1.computed) self.assertFalse(s2.computed) DiffComputer.get_instance().compute_now(s1, s2) # something has been computed self.assertIsNotNone(s2.pixelsDiff) self.assertEqual(s2.refSnapshot, s1, "refSnapshot should have been updated") self.assertFalse( s1.computed) # reference picture, computed flag remains False self.assertTrue(s2.computed) # diff computed, flag changed
def test_compute_using_exclude_zones_from_step(self): """ issue #57: Check that computing takes into account exclude zone from step snapshot that could be added by seleniumRobot """ with patch.object(DiffComputer.picture_comparator, 'get_changed_pixels', wraps=DiffComputer.picture_comparator. get_changed_pixels) as wrapped_get_changed_pixels: with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) ref_snapshot = Snapshot( stepResult=StepResult.objects.get(id=1), refSnapshot=None, pixelsDiff=None) ref_snapshot.save() ref_snapshot.image.save("img", img) ref_snapshot.save() step_snapshot = Snapshot( stepResult=StepResult.objects.get(id=2), refSnapshot=None, pixelsDiff=None) step_snapshot.save() step_snapshot.image.save("img", img) step_snapshot.save() exclusion1 = ExcludeZone(x=0, y=0, width=10, height=10, snapshot=step_snapshot) exclusion1.save() DiffComputer.get_instance().compute_now( ref_snapshot, step_snapshot) # check exclude zone has been used for comparison wrapped_get_changed_pixels.assert_called_with( ref_snapshot.image.path, step_snapshot.image.path, [exclusion1.toRectangle()])
def test_snapshot_deletion_of_first_reference(self): """ Test that when first reference is deleted, other pictures have their references changed Here, S1 is a reference S2 has a reference on S1 S3 has a reference on S1 After deletion of S1, S2 becomes a reference S3 should have reference on S2 """ s1 = Snapshot(stepResult=self.tsr1, image=None, refSnapshot=None, pixelsDiff=None) s1.save() s2 = Snapshot(stepResult=self.tsr2, image=None, refSnapshot=s1, pixelsDiff=None) s2.save() s3 = Snapshot(stepResult=self.tsr2, image=None, refSnapshot=s1, pixelsDiff=None) s3.save() s1.delete() self.assertEqual(Snapshot.objects.get(pk=s2.id).refSnapshot, None) self.assertEqual(Snapshot.objects.get(pk=s3.id).refSnapshot, s2)
def test_remove_ref_with_different_browser(self): """ Test the case where we remove a ref a we want to make sure that the new reference is searched with the same browsert """ with open("snapshotServer/tests/data/test_Image1.png", 'rb') as imgFile: img = ImageFile(imgFile) # snapshot associated to same version, same test case, but other environment as 'initialRefSnapshot' snapshot_other_browser = Snapshot( stepResult=self.step_result_other_env, refSnapshot=None, pixelsDiff=None) snapshot_other_browser.save() snapshot_other_browser.image.save("img", img) snapshot_other_browser.save() # reference snapshot associated to same version / test case / environment / browser as 'initialRefSnapshot' snapshot_ref_same_env = Snapshot(stepResult=self.sr1, refSnapshot=None, pixelsDiff=None) snapshot_ref_same_env.save() snapshot_ref_same_env.image.save("img", img) snapshot_ref_same_env.save() # snapshot associated to same version / test case / environment / browser as 'snapshot_ref_same_env' snapshot_same_env = Snapshot(stepResult=self.step_result_same_env, refSnapshot=snapshot_ref_same_env, pixelsDiff=None) snapshot_same_env.save() snapshot_same_env.image.save("img", img) snapshot_same_env.save() response = self.client.get( reverse('pictureView', kwargs={ 'testCaseInSessionId': self.tcs1.id, 'testStepId': 1 }) + "?makeRef=False&snapshotId=" + str(snapshot_ref_same_env.id)) # check display self.assertEqual(response.context['captureList'][0]['reference'], self.initialRefSnapshot, "new reference should be the first snapshot") self.assertEqual( response.context['captureList'][0]['stepSnapshot'].refSnapshot, self.initialRefSnapshot, "new reference should be the first snapshot") self.assertIsNotNone( response.context['captureList'][0]['stepSnapshot'].pixelsDiff) DiffComputer.stopThread() # check 'snapshot_same_env' ref as been changed and its reference snapshot is 'initialRefSnapshot' becaus it's the same environment / test case / version self.assertEqual( Snapshot.objects.get(id=snapshot_same_env.id).refSnapshot, self.initialRefSnapshot, "ref snapshot for 'snapshot_same_env' should have changed to first snapshot, not 'snapshot_other_browser'" )