How to get the error message in robot framework? - python

I have created a script to get the error output
***Settings***
Resource importsLib.robot
Suite Setup Run Keywords
... Initialize Test AND
... Register Keyword To Run On Failure Failure Callback
Variables OMG.yaml
and the keywords
***Keywords***
Failure Callback
Capture Page Screenshot
Log Source loglevel=WARN
but the point is I need to get HTML error message when Back-end didn't send the value to Front-end element sometimes and I need to track the root causes of faults or problems.
Can you offer the best solution for this case?

You can use HttpLibrary but you should also code BE tests for it, there is no way to check it under your FE tests if you are not check parallelly BE http methods.
You can create your own custom libraries or keywords for it.
And here is a example of how you can use custom libraries on the Robot Framework :
How to create a custom Python code library for the Robot Framework

Related

How we can automate python client application which is used an an interface for user to call APIs

We have made an python client which is used as an interface for user. some function is defined in the client which internally calls the APIs and give output to users.
My requirement is to automate the python client - functions and validate the output.
Please suggest tools to use.
There are several ways to do that:
You can write multiple tests for your application as the test cases which are responsible to call your functions and get the result and validate them. It calls the "feature test". To do that, you can use the python "unittest" library and call the tests periodically.
If you have a web application you can use "selenium" to make automatic test flows. (Also you can run it in a docker container virtually)
The other solution is to write another python application to call your functions or send requests everywhere you want to get the specific data and validate them. (It's the same with the two other solutions with a different implementation)
The most straightforward way is using Python for this, the simplest solution would be a library like pytest. More comprehensive option would be something like Robot framework
Given you have jmeter in your tags I assume that at some point you will want to make a performance test, however it might be easier to use Locust for this as it's pure Python load testing framework.
If you still want to use JMeter it's possible to call Python programs using OS Process Sampler

how to run control m job through python script?

i have a control m job instead of triggering it through control m application i want to trigger through my python script but i don't know how to perform this action is there any module available in python which can trigger control m job and also how can I pass user-id , DNS details and password into it..
It can be done using Control-M Automation API.
You can use the following link as a reference for the python code:
https://github.com/controlm/automation-api-quickstart/tree/master/control-m/201-call-rest-api-using-python
You can use the following link to browse the Control-M Swagger documentation:
http://aapi-swagger-doc.s3-website-us-west-2.amazonaws.com/#/run/runNow
It seems like /run/runNow endpoint will do the trick

Django Hostname in Testing

I am wondering if there is a way to obtain the hostname of a Django application when running tests. That is, I would like the tests to pass both locally and when run at the staging server. Hence a need to know http://localhost:<port> vs. http://staging.example.com is needed because some tests query particular URLs.
I found answers on how to do it inside templates, but that does not help since there is no response object to check the hostname.
How can one find out the hostname outside the views/templates? Is it stored in Django settings somewhere?
Why do you need to know the hostname? Tests can run just fine without it, if you use the test client. You do not need to know anything about the system they're running on.
You can also mark tests with a tag and then have the CI system run the tests including that tag.
And finally there is the LiveServerTestCase:
LiveServerTestCase does basically the same as TransactionTestCase with one extra feature: it launches a live Django server in the background on setup, and shuts it down on teardown. This allows the use of automated test clients other than the Django dummy client such as, for example, the Selenium client, to execute a series of functional tests inside a browser and simulate a real user’s actions.
The live server listens on localhost and binds to port 0 which uses a free port assigned by the operating system. The server’s URL can be accessed with self.live_server_url during the tests.
Additional information from comments:
You can test if the URL of an image file is present in your response by testing for the MEDIA_URL:
self.assertContains(response, f'{settings.MEDIA_URL}/default-avatar.svg')
You can test for the existence of an upload in various ways, but the easiest one is to check if there's a file object associated with the FileField. It will throw ValueError if there is not.

Create New Model Instance with API Call in Django

I'm unsure how to approach this problem in general in my Django app:
I need to make a call to an API every n days. I can make this call and fetch the data required via Python, but where exactly should I put the code?
Do I put the code in a specific view and then map the view to a URL and have that URL called whenever I want to create new model instances based on the API call?
Or am I approaching this the wrong way?
The way I usually do this is with a combination of custom Django-admin commands, and then run them wit a scheduled Cron job
You can run your custom commands in the same way as you would run the default ones:
python manage.py <your_command_name> <your_command_arguments>
Sounds like you are trying to have a schedule-able job. Celery works well for this sort of situation.
You would create a task that runs every N days. In that task, you would put your code that calls the API and processing the response as necessary.
Reference:
Celery Periodic Tasks
Do I put the code in a specific view
a django view is a callable that must accept an HTTP request and return an HTTP response, so unless you need to be able to call your code thru HTTP there's no point in using a view at all, and even if you want to have a view exposing this code it doesn't mean the code doing the API call etc has to live in the view.
Remember that a "django app" is basically a Python package, so beside the django-specific stuff (views, models etc) you can put any module you want and have your views, custom commands etc call on these modules. So just write a module for your API client etc with a function doing the fetch / create model instance / whatever job, and then call this function from where it makes sense (view, custom command called by a cron job, celery task, whatever).

Write to robot framework console from Python

I am a newbie using python and I wanted to ask for your help in showing me how can I print messages from Python into robot framework console.
There are several ways for a python function to send information to the robot logs or to the console. These are all documented in the Robot framework user's guide, in the section titled Logging information.
The cleanest way is to use the logging API, which gives specialized functions for various kinds of logging. For example, to send information to the console you would use logger.console(message).
Using the logging API
Here is a library file that uses this method:
# ExampleKeywords.py
from robot.api import logger
def write_to_console(s):
logger.console(s)
You can use this library in the following manner:
*** Settings ***
| Library | ExampleKeywords.py
*** Test Cases ***
| Use a custom keyword to write to the console
| | Write to console | Hello, world
This will appear in the console only, and will not show up in the logs. If you want the information to show up in the logs you can use logger methods info, warn, debug, or trace. To log an error you would simply throw an exception.
Calling built-in keywords
There are other ways for your custom keywords to send information to the logs. For example, you can get a reference to the BuiltIn library, and directly call the log or log to console keywords like this:
from robot.libraries.BuiltIn import BuiltIn
def write_to_console(s):
BuiltIn().log_to_console("Hello, world")
Using print statements
Finally, you can write information to the logs (but not only to the console) using print statements. You can prefix the string with *<level>* to affect the log level. For example, to print a warning you can do:
print "*WARN* Danger Will Robinson"
Summary
Using the API is arguably the best method to log information from your keywords. However, this is a fairly new API only available since Robot Framework 2.6, so if you are using an older version of Robot you may need to use one of the other techniques.
What it sounds like you're asking for is a way to write a library such that your testcase can print to the console during test execution.
This is pretty easy, you can find it in the RF Manual under Logging Information. The short version is you can log a "WARN" that will appear in both the log and on screen with a print statement, like so:
print "*WARN* This text will show up on the console."
This command can be used to print any message or content on robot console.
from robot.libraries.BuiltIn import BuiltIn
BuiltIn().log_to_console("Message that needs to be printed")

Categories