When writing web automation test cases, how to write assertions makes it difficult for novices to understand. Strictly speaking, automated scripts without assertions cannot be called test cases. Just like functional testing, after the tester does some operations, he will inevitably determine whether the actual result is equal to the expected result, but this process is completed by the tester's eyes. The automated test script must use this information to determine whether the use case is successful.
The three commonly used information are:
title: The pages are different or displayed differently often the title will change.
url: Similar to title, when the page changes, the url will also change.
text: It is more widely used than the former because it can obtain any identifier text on the page, which is used to "prove" the use case execution is successful. For example, the user name of the person after logging in, the query results, etc.
However, in some cases, what should I do if this information is not available to prove that the use case is successful? Of course, the worst thing is not to write assertions, and the script run without an error to prove that the use case is successfully executed, which is of course a helpless move. In addition, you can also choose to assert two images, take a screenshot of the current page when the use case is executed correctly, and take a screenshot again during the use case execution. By comparing the two images, we can determine whether the use case is running successfully.
Pillow Download:/pypi/Pillow/3.0.0
Choose download according to your operating system and python version.
Install:
> python3 -m pip install Pillow-3.0.0-cp35-none-win_amd64.whl
Processing c:\selenium\pillow-3.0.0-cp35-none-win_amd64.whl
Installing collected packages: Pillow
Successfully installed Pillow-3.0.0
Note that because I installed Python 2.7 and Python 3.5 at the same time, I specially specified that I installed it under Python 3.
from PIL import Image import math import operator from functools import reduce def image_contrast(img1, img2): image1 = (img1) image2 = (img2) h1 = () h2 = () result = (reduce(, list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) ) return result if __name__ == '__main__': img1 = "./" # Specify the image path img2 = "./" result = image_contrast(img1,img2) print(result)
If the two pictures are exactly equal, the result is returned as floating point type "0.0". If it is not the same, the result value will be returned.
This allows the method to be called in the automated test case to assert the execution result.
Detailed documentation about the Pillow library:
/en/latest/
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.