Verify presence of GUI elements in console application - python

I am currently developing an automated test case which performs actions on a console application (very simple, just clicking buttons and inputting text).
I have been able to successfully click through the application and input data without many problems, which includes identifying elements and things like that.
I have not been able to verify the existence of GUI elements that aren't clickable, ie an image that is loaded into the console application. I have tried looking through the output when using print_control_identifiers() for any indication of where data about an image in the window might be stored. I have tried looking at the tree diagram of the windows before and after loading the image in to see if it changes (it does not).
I have also then looked into verifying basic things, like font type or other properties that exist in the VS development platform, without success. To do this I used handleprops.font among other things. I have also read through the handleprops class and tried to find anything in there that I can use, but none of it is working.
My question is: is there a way to access an image's information that is stored in the window? I am able to identify where it is stored (picturebox1) and get information about that window (handleprops.rectangle) but cannot get anything about whether the image is there (like a path to image for example). All I can access is 'controllable' items, those that show up in print_control_identifiers()
I'm not sure this is possible with pywinauto but I have been stuck on this for a while and wondered if there were any suggestions people had.
Thanks

Related

I need to hook HTML input into Python and don't know how

I am working on a team project for a capstone course. We have decided to produce a self-hosted calendar app for college students. One team member has created HTML webpages for said self-hosted server. I need to hook button and text box input and output into a python program. We are using Starlette, but I cannot figure out how to get information from text boxes.
To give an example of what I need to do, the user will input information into a selection of text boxes. That info needs to be read and converted into variables, then sent into an SQL statement. I have the SQL done, I just don't know how to read the info from the HTML boxes and detect button input.
Before answering it is necessary to know if you are using a python framework, ideally it would be enough to change the action of the form and connect it with a python function.
Here I leave you a tutorial to clarify a little bit the panorama.
https://www.youtube.com/watch?v=dlhg8HMZTOk

Interfacing a QR code recognition to a django database

I'm coming to you with the following issue:
I have a bunch of physical boxes onto which I still stick QR codes generated using a python module named qrcode. In a nutshell, what I would like to do is everytime someone wants to take the object contained in a box, he scans the qr code with his phone, then takes it and put it back when he is done, not forgetting to scan the QR code again.
Pretty simple, isn't it?
I already have a django table containing all my objects.
Now my question is related to the design. I suspect the easiest way to achieve that is to have a POST request link in the QR code which will create a new entry in a table with the name of the object that has been picked or put back, the time (I would like to store this information).
If that's the correct way to do, how would you approach it? I'm not too sure I see how to make a POST request with a QR code. Would you have any idea?
Thanks.
PS: Another alternative I can think of would be to a link in the QR code to a form with a dummy button the user would click on. Once clicked the button would update the database. But I would fine a solution without any button more convenient...
The question boils down to a few choices: (a) what data do you want to encode into the QR code; (b) what app will you use to scan the QR code; and (c) how do you want the app to use / respond to the encoded data.
If you want your users to use off-the-shelf QR code readers (like free smartphone apps), then encoding a full URL to the appropriate API on your backend makes sense. Whether this should be a GET or POST depends on the QR code reader. I'd expect most to use GET, but you should verify that for your choice of app. That should be functionally fine, if you don't have any concerns about who should be able to scan the code.
If you want more control, e.g. you'd like to keep track of who scanned the code or other info not available to the server side just from a static URL request, you need a different approach. Something like, store the item ID (not URL) in the QR code; create your own simple QR code scanner app (many good examples exist) and add a little extra logic to that client, like requiring the user to log in with an ID + password, and build the URL dynamically from the item ID and the user ID. Many security variations possible (like JWT token) -- how you do that won't be dictated by the contents of the QR code. You could do a lot of other things in that QR code scanner / client, like add GPS location, ask the user to indicate why or where they're taking the item, etc.
So you can choose between a simple way with no controls, and a more complex way that would allow you to layer in whatever other controls and extra data you need.
If security is not a big concern: an API with a simple get method that takes as argument the object id and I will presume you have the code to make sure if the object is given as taken it will be switched to returned.
And why not post? POST needs headers that you can't include in qr unless you have a dedicated app, so GET and the ability to use example.com/api/leaseandret?id=12345 is a better alternative that allows for better usage with a QR.
A summary of the methods*
* A note here is that GET is not forbidden from being used to modify data and send data to a server GET is exclusively for getting data from a REST purist standpoint.

Is it possible to make sequential tests in Python-Selenium tests?

Edited Question:
I guess I worded my previous question improperly, I actually want to get away from "unit tests" and create automated, modular system tests that build off of each other to test the application as whole. Many parts are dependent upon the previous pages and subsequent pages cannot be reached without first performing the necessary steps on the previous pages.
For example (and I am sorry I cannot give the actual code), I want to sign into an app, then insert some data, then show that the data was sent successfully. It is more involved than that, however, I would like to make the web driver portion, 'Module 1.x'. Then the sign in portion, 'Module 2.x'. The data portion, 'Module 3.x'. Finally, success portion, 'Module 4.x'. I was hoping to achieve this so that I could eventually say, "ok, for this test, I need it to be a bit more complicated so let's do, IE (ie. Module 1.4), sign in (ie. Module 2.1), add a name (ie Module 3.1), add an address (ie. Module 3.2), add a phone number (ie Module 3.3), then check for success (ie Module 4.1). So, I need all of these strung together. (This is extremely simplified and just an example of what I need to occur. Even in the case of the unit tests, I am unable to simply skip to a page to check that the elements are present without completing the required prerequisite information.) The issue that I am running into with the lengthy tests that I have created is that each one requires multiple edits when something is changed and then multiplied by the number of drivers, in this case Chrome, IE, Edge and Firefox (a factor of 4). Maybe my approach is totally wrong but this is new ground for me, so any advice is much appreciated. Thank you again for your help!
Previous Question:
I have found many answers for creating unit tests, however, I am unable to find any advice on how to make said tests sequential.
I really want to make modular tests that can be reused when the same action is being performed repeatedly. I have tried various ways to achieve this but I have been unsuccessful. Currently I have several lengthy tests that reuse much of the same code in each test, but I have to adjust each one individually with any new changes.
So, I really would like to have .py files that only contain a few lines of code for the specific task that I am trying to complete, while re-using the same browser instance that is already open and on the page where the previous portion of the test left off. Hoping to achieve this by 'calling' the smaller/modular test files.
Any help and/or examples are greatly appreciated. Thank you for your time and assistance with this issue.
Respectfully,
Billiamaire
You don't really want your tests to be sequential. That breaks one of the core rules of unit tests where they should be able to be run in any order.
You haven't posted any code so it's hard to know what to suggest but if you aren't using the page object model, I would suggest that you start. There are a lot of resources on the web for this but the basics are that you create a single class per page or widget. That class would hold all the code and locators that pertains to that page. This will help with the modular aspect of what you are seeking because in your script you just instantiate the page object and then consume the API. The details of interacting with the page, the logic, etc. all lives in the page object is exposed via the API it provides.
Changes/updates are easy. If the login page changes, you edit the page object for the login page and you're done. If the page objects are properly implemented and the changes to the page aren't severe, many times you won't need to change the scripts at all.
A simple example would be the login page. In the login class for that page, you would have a login() method that takes username and password. The login() method would handle entering the username and password into the appropriate fields and clicking the sign in button, etc.

Search/Filter/Select/Manipulate data from a website using Python

I'm working on a project that basically requires me to go to a website, pick a search mode (name, year, number, etc), search a name, select amongst the results those with a specific type (filtering in other words), pick the option to save those results as opposed to emailing them, pick a format to save them then download them by clicking the save button.
My question is, is there a way to do those steps using a Python program? I am only aware of extracting data and downloading pages/images, but I was wondering if there was a way to write a script that would manipulate the data, and do what a person would manually do, only for a large number of iterations.
I've thought of looking into the URL structures, and finding a way to generate for each iteration the accurate URL, but even if that works, I'm still stuck because of the "Save" button, as I can't find a link that would automatically download the data that I want, and using a function of the urllib2 library would download the page but not the actual file that I want.
Any idea on how to approach this? Any reference/tutorial would be extremely helpful, thanks!
EDIT: When I inspect the save button here is what I get:
Search Button
This would depend a lot on the website your targeting and how their search is implemented.
For some websites, like Reddit, they have an open API where you can add a .json extension to a URL and get a JSON string response as opposed to pure HTML.
For using a REST API or any JSON response, you can load it as a Python dictionary using the json module like this
import json
json_response = '{"customers":[{"name":"carlos", "age":4}, {"name":"jim", "age":5}]}'
rdict = json.loads(json_response)
def print_names(data):
for entry in data["customers"]:
print(entry["name"])
print_names(rdict)
You should take a look at the Library of Congress docs for developers. If they have an API, you'll be able to learn about how you can do search and filter through their API. This will make everything much easier than manipulating a browser through something like Selenium. If there's an API, then you could easily scale your solution up or down.
If there's no API, then you have
Use Selenium with a browser(I prefer Firefox)
Try to get as much info generated, filtered, etc. without actually having to push any buttons on that page by learning how their search engine works with GET and POST requests. For example, if you're looking for books within a range, then manually conduct this search and look at how the URL changes. If you're lucky, you'll see that your search criteria is in the URL. Using this info you can actually conduct a search by visiting that URL which means your program won't have to fill out a form and push buttons, drop-downs, etc.
If you have to use the browser through Selenium(for example, if you want to save the whole page with html, css, js files then you have to press ctrl+s then click "save" button), then you need to find libraries that allow you to manipulate the keyboard within Python. There are such libraries for Ubuntu. These libraries will allow you to press any keys on the keyboard and even do key combinations.
An example of what's possible:
I wrote a script that logs me in to a website, then navigates me to some page, downloads specific links on that page, visits every link, saves every page, avoids saving duplicate pages, and avoids getting caught(i.e. it doesn't behave like a bot by for example visiting 100 pages per minute).
The whole thing took 3-4 hours to code and it actually worked in a virtual Ubuntu machine I had running on my Mac which means while it was doing all that work I could do use my machine. If you don't use a virtual machine, then you'll either have to leave the script running and not interfere with it or make a much more robust program that IMO is not worth coding since you can just use a virtual machine.

Is this possible to write a program to inter-act with existing software?

For example, if the program did not provide an API, and I need it to do something from Python script, is this possible to do so? Let's give an example:
I would like to copy the Product ID from the Internet Explorer's product ID everyday and new a text file named in this format: ddmmyyyy, and store on desktop. It is stupid, so, I would like to handle it to the machine to do it. (Yes, it is useless, but just say, it is an example.)
Creating a text file, with a string is easy to implement, but the REAL question is, how can I get the product ID from the program that don't have API provided.
So, the IE didn't provide the API for developer to access the Product ID, and I think the value can be shown on the screen or registry. Assume that I don't know the register location or they simply didn't store it in the registry, what I can get this product ID is manually, click, and check. For this process, is this possible to make it automatic? Thanks.
If the Info ur looking for is showed by IE you can obtain it by using xpath. If you want to make it automatic you could use selenium which works with webs as well.
If the program doesnt provide an api then its not possible to interact with the program unless you get the output from a browser or you may use programs like Sikuli and obtain the info for example by image processing

Categories

Resources