I have created a session and located the QR code element ID using endpoint mentioned in Appium documentation.
This is the value of text I'm getting from QR code; I expect the actual decode of QR code which can be further used in applications for automation. I was wondering if other application needs to be used or it is possible in postman itself.
The issue was solved by using python's cv2 library
filename = "img.png"
# read the QRCODE image
image = cv2.imread(filename)
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()
# detect and decode
data, vertices_array, binary_qrcode = detector.detectAndDecode(image)
if vertices_array is not None:
print("QRCode data:")
print(data)
else:
print("There was some error")
Well, in fact, there are several questions here, is the QR code value static or dynamic, that is, does the QR code change during each test or can you use the same QR for all tests
If the value does not change(static), you can create a file, say .yaml, in the resources or test data directory, where your value will be stored in an encoded form, then you read this value from the file, decode it and pass it to the test script
If the QR code changes(dynamic value) for each test, you can create an API call, for python you can use the requests library or Java RestAssured, you take the encoded value from the response, decode it and pass it to the test script
the implementation depends on the above, but the whole process must be implemented within your framework
So you don't need to use additional tools like Postman etc to get value and decode it, if you want to automate your case properly combine API and UI
To decode value in Python I use Fernet, Java bouncycastle
Related
I'm trying to decode a QR Code using Python, or any other language to be honest. I am also familiar with Javascript or PHP, but Python seemed to be the most appropriate one for this task.
This is part of a bigger piece of code that I am writing for a little challenge. I need to extract a password from the QR Code. I've tried using a QR Code reader on my phone and I can get the password so I can confirm that there is no issue with the QR Code itself.
Here is the QRCode:
And the string to retrieve is "The key is /qrcod_OMevpf".
So far I've tried using two different python libraries. Open CV and Pyzbar, with the following codes:
OpenCV
image = cv2.imread(imgAbsolutePath)
qrCodeDetector = cv2.QRCodeDetector()
decodedText, points, _ = qrCodeDetector.detectAndDecode(image)
if points is not None:
# QR Code detected handling code
print("QR code detected")
print(decodedText)
else:
print("QR code not detected")
Which prints "QR code detected" and then an empty string.
Pyzbar
qr = decode(Image.open('result.png'), symbols=[ZBarSymbol.QRCODE])
print(qr)
Which prints "[]"
Do you know why these don't work or can you suggest any other libraries that works ?
Thanks
I finally got it to work using zxing :
from zxing import BarCodeReader
def decode_qr_code(image_path):
reader = BarCodeReader()
barcode = reader.decode(image_path)
return barcode.parsed
qr_code = decode_qr_code("result.png")
print(qr_code)
I had sent an image that is encoded it to Base64 in Java to python, and when I got the data from python, this is the data
'iVBORw0KGgoAAAANSUhEUgAAAMMAAAEECAIAAAAahxdFAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4\nnEy86Zokx5EkKCKq5h6ZlVUFEAfBAyR7e4+emd33f5jdb6enDzZJNIBCHZkRbqYq+8OjOFv5p77I\nyPBwdzNVudT59PqtRRlgrwaAPbKqig0gmbbby4wIotBacJK0LSmpgolM0WKXIwVTEhSku2GS5NAo\ngfSgGgAgyRFBqfhqzxv66H4IbtuF7kA8090dhAm1RrCIRmVeCgX0oKoBIJEB2j64HKmi5fLakCZa\nJpmNEVlVB1udy2vLdHODXl4+bU+vUwFWmwGaC+Jaa1MytOhwuTPUAdySGzjn5Mgwt2279W3BMnZx\ncRR6QOg2lVRm3tZxGfuiVREah4ok6S3yyg73o/eIKHelDJiKLUhqQhlKpmKSzBzLEVHBQbfiIca1\nnZnoYqTgLS43eWNQ3qVFWxwaCw7mPmLLUTX3HIBIjlSj9xgmbG8KAA0omRFs577d1hS4jWEIouAS\nhkaKjRqxxX55cLdFmiFVNwxJBKlYaGaQsEGKhqFMVXVEDMVEg5TQtIIRoQhkZAy6tWXkuIwLhKf9\nAdK+XbaRQ7HleNovr/fLY2jfwxLJ3LTlWG0EDjvEEPdIODNjYi40SAKCGZq1QnRXKpa60ZHZbQ0Y\nvYmRPFYTkjoZXTXljWECdqGINjCNMQbICK4+JAUH6RxjoocZ1NZw2oAFQaYjo23JMK+er7ClsguR\ng+gIJzWrLq/GS63LPkBameShSqsJiTQywqAFB4vMzAiOMdZtXRL7uExXaLixKzcxIj+xLxagBgqU\nNEIBltrJkEcTUUy1QGBEZGhwF93VewjdETF7DSXQoWii3SCGIhXFtp0UQrYzQhLQCCR13nECBKhw\ndWzbXnAylwvtEQnAdhMkKdo2qAgDBkhWLaVEwR6RCu4xmnjICzMQytid2jI1MuxLjKfHi+VLjJAJ\nxBiwPaJRL25TDiG'
How do I convert this to either bitmap or an image in python? If the code snippets are needed, please tell
I had tried using base64.b64decode and it gives the error incorrect padding
Based on one of the comments, I tried base64.b64decode(base64_str + '===') and gotten this
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xc3\x00\x00\x01\x04\x08\x02\x00\x00\x00\x1a\x87\x17E\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00
\x00IDATx\x9c\x94\xbc\xddrdI\x92\xa4\xa7j\xe6~"\x90Y5US5\xdd=?|\x02^S\xf6\xfd\x1feeE\xb8\xec\xed\xee\xea\xaa\xcaLD\xc4q7S\xe5\xc5A\xf3\x86\xe4\x92\x03\x88\x00\x02\x08
q"\xdc\x8e\xfd\xa8~\x16\xfc_\xff\xcb\xff\x06gD0\x14\x11\x89\x1b3\x8a\xfd9'\xc9\xceAL\x04oG\x94\xf6m\xde\xe7q\xd8\xbc\xe7-#t?\x9e\xd4\xcfyS\xceO\xe3\xf0\xc1\xdb\xedv\x8c\xf1\xc3\xdbQo\xf3\x9e7\xc13\x8f\x18\xbe3s\x1c\x9f\x8e\x999\x01\x1co\xc7\x18\xe36\xe6\x88\x9c\xb7q\xbf\xddn\xb7\xe3\xed\xed\xed\xc7\x1f\xbe\xff\x97\x1f~\xf8tL\xfc'?L\xfc\xf2\xfb\xb7\xbf\xfc\xed\xef\x7f\xf9\xdb\xdf\xaa\xca\x80\xa4\x86\x01\x10S\x92\x89\xcclX6\x1d\x0f\xed\xbd\x95\x99\x92\x8e1\x11\xb2a3\xc2\x00nd[\x81\xdcP\x18\xc8\x904\x83[\xb6\r
a\x07mJ\n\x8b$\x00\xb7\x00\xb4E\xa8\xbb'!
\x80\xdd\x1d#\x80I\xad2\xc9\xe1n\x17I\xb7(7:\x15\xee\x06\xa4\x8e\xd5u\x9e\xfbQ\xfb\xf1<\xdf_\xef_\x1e\xef:\xd9\xddR-\x00r\x9a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x86\xcc\x18s\x9e\xbd\xe7\x18s\x1eB\xde8+|\x8b\xe0`r\xb4\xd6\xdb\xed\xd3#\x1c\x11\xe3\x00\x88\xc1\xf8<\xde\xc0\xb0tv!'
I'm playing around in python trying to download some images from imgur. I've been using the urrlib and urllib.retrieve but you need to specify the extension when saving the file. This isn't a problem for most posts since the link has for example .jpg in it, but I'm not sure what to do when the extension isn't there. My question is if there is any way to determine the image format of the file before downloading it. The question is mostly imgur specific, but I wouldn't mind a solution for most image-hosting sites.
Thanks in advance
You can use imghdr.what(filename[, h]) in Python 2.7 and Python 3 to determine the image type.
Read here for more info, if you're using Python 2.7.
Read here for more info, if you're using Python 3.
Assuming the picture has no file extension, there's no way to determine which type it is before you download it. All image formats sets their initial bytes to a particular value. To inspect these 'magic' initial bytes check out https://github.com/ahupp/python-magic - it matches the initial bytes against known image formats.
The code below downloads a picture from imgur and determines which file type it is.
import magic
import requests
import shutil
r = requests.get('http://i.imgur.com/yed5Zfk.gif', stream=True) ##Download picture
if r.status_code == 200:
with open('~/Desktop/picture', 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
print magic.from_file('~/Desktop/picture') ##Determine type
## Prints: 'GIF image data, version 89a, 360 x 270'
I am creating image that I would like to embed in the e-mail. I cannot figure out how to create image as binary and pass into MIMEImage. Below is the code I have and I have error when I try to read image object - the error is "AttributeError: 'NoneType' object has no attribute 'read'".
image=Image.new("RGBA",(300,400),(255,255,255))
image_base=ImageDraw.Draw(image)
emailed_password_pic=image_base.text((150,200),emailed_password,(0,0,0))
imgObj=emailed_password_pic.read()
msg=MIMEMultipart()
html="""<p>Please finish registration <br/><img src="cid:image.jpg"></p>"""
img_file='image.jpg'
msgText = MIMEText(html,'html')
msgImg=MIMEImage(imgObj)
msgImg.add_header('Content-ID',img_file)
msg.attach(msgImg)
msg.attach(msgText)
If you look at line 4 - I am trying to read image so that I can pass it into MIMEImage. Apparently, image needs to be read as binary. However, I don't know how to convert it to binary so that .read() can process it.
FOLLOW-UP
I edited code per suggestions from jsbueno - thank you very much!!!:
emailed_password=os.urandom(16)
image=Image.new("RGBA",(300,400),(255,255,255))
image_base=ImageDraw.Draw(image)
emailed_password_pic=image_base.text((150,200),emailed_password,(0,0,0))
stream_bytes=BytesIO()
image.save(stream_bytes,format='png')
stream_bytes.seek(0)
#in_memory_file=stream_bytes.getvalue()
#imgObj=in_memory_file.read()
imgObj=stream_bytes.read()
msg=MIMEMultipart()
sender='xxx#abc.com'
receiver='jjjj#gmail.com'
subject_header='Please use code provided in this e-mail to confirm your subscription.'
msg["To"]=receiver
msg["From"]=sender
msg["Subject"]=subject_header
html="""<p>Please finish registration by loging into your account and typing in code from this e-mail.<br/><img src="cid:image.png"></p>"""
img_file='image.png'
msgText=MIMEText(html,'html')
msgImg=MIMEImage(imgObj) #Is mistake here?
msgImg.add_header('Content-ID',img_file)
msg.attach(msgImg)
msg.attach(msgText)
smtpObj=smtplib.SMTP('smtp.mandrillapp.com', 587)
smtpObj.login(userName,userPassword)
smtpObj.sendmail(sender,receiver,msg.as_string())
I am not getting errors now but e-mail does not have image in it. I am confused about the way image gets attached and related to in html/email part. Any help is appreciated!
UPDATE:
This code actually works - I just had minor typo in the code on my PC.
There are a couple of conceptual errors there, both in using PIL and on what format an image should be in order to be incorporated into an e-mail.
In PIL: the ImageDraw class operates inplace, not like the Image class calls, which usually return a new image after each operation. In your code, it means that the call to image_base.text is actually changing the pixel data of the object that lies in your image variable. This call actually returns None and the code above should raise an error like "AttributeError: None object does not have attribute 'read'" on the following line.
Past that (that is, you should fetch the data from your image variable to attach it to the e-mail) comes the second issue: PIL, for obvious reasons, have images in an uncompressed, raw pixel data format in memory. When attaching images in e-mails we usually want images neatly packaged inside a file - PNG or JPG formats are usually better depending on the intent - let's just stay with .PNG. So, you have to create the file data using PIL, and them attach the file data (i.e. the data comprising a PNG file, including headers, metadata, and the actual pixel data in a compressed form). Otherwise you'd be putting in your e-mail a bunch of (uncompressed) pixel data that the receiving party would have no way to assemble back into an image (even if he would treat the data as pixels, raw pixel data does not contain the image shape so-)
You have two options: either generate the file-bytes in memory, or write them to an actual file in disk, and re-read that file for attaching. The second form is easier to follow. The first is both more efficient and "the right thing to do" - so let's keep it:
from io import BytesIO
# In Python 2.x:
# from StringIO import StringIO.StringIO as BytesIO
image=Image.new("RGBA",(300,400),(255,255,255))
image_base=ImageDraw.Draw(image)
# this actually modifies "image"
emailed_password_pic=image_base.text((150,200),emailed_password,(0,0,0))
stream = BytesIO()
image.save(stream, format="png")
stream.seek(0)
imgObj=stream.read()
...
(NB: I have not checked the part dealing with mail and mime proper in your code - if you are using it correctly, it should work now)
Instead of using directories to reference an image, is it possible to code an image into the program directly?
You can use the base64 module to embed data into your programs. From the base64 documentation:
>>> import base64
>>> encoded = base64.b64encode('data to be encoded')
>>> encoded
'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
'data to be encoded'
Using this ability you can base64 encode an image and embed the resulting string in your program. To get the original image data you would pass that string to base64.b64decode.
Try img2py script. It's included as part of wxpython (google to see if you can dl seperately).
img2py.py -- Convert an image to PNG format and embed it in a Python
module with appropriate code so it can be loaded into a program at runtime. The benefit is that since it is Python source code it can be delivered as a .pyc or 'compiled' into the program using freeze, py2exe, etc.
Usage:
img2py.py [options] image_file python_file
There is no need to base64 encode the string, just paste it's repr into the code
If you mean, storing the bytes that represent the image in the program code itself, you could do it by base64 encoding the image file, and setting a variable to that string.
You could also declare a byte array, where the contents of the array are the bytes that represent the image.
In both cases, if you want to operate on the image, you may need to decode the value that you have included in your source code.
Warning: you may be treading on a performance minefield here.
A better way might be to store the image/s in the directory structure of your module, and the loading it on demand (even caching it). You could write a generalized method/function that loads the right image based on some identifier which maps to the particular image file name that is part and parcel of your module.