How to get the output from a kyword using robot.api? - python

I hope you can help me, I am quite stuck with this issue :(
I am trying to create all the tests using the robot api with python, I followed the example in the documentation, but I need to capture the output from a keyword and I dont find how can I do it
I tried as usual in rf-ride syntax:
test.keywords.create('${greps}= grep file', args=['log.txt', 'url:', 'encoding_errors=ignore'])
It says: No keyword with name '${grep}= grep file' found.
I tried:
output = test.keywords.create('grep file', args=['log.txt', 'url:', 'encoding_errors=ignore'])
but the variable output is having just the keyword name, not the output from kw
I dont know where to look for more info, all the examples are creating kw which dont return any value...

The call to test.keywords.create(...) doesn't call the keyword, it merely creates one to be called later. If you want the results to be assigned to a variable, use the assign attribute when calling create. This argument takes a list of variable names.
For example, given this line in plain text format:
${greps}= grep file log.txt url: encoding_errors=ignore
... you would create it like this using the API:
test.keywords.create('grep file',
args=['log.txt', 'url:', 'encoding_errors=ignore'],
assign=['${greps}'])

Related

Python Variable For Get Request?

I am trying to move over some API calls I had working over to python from postman, I am having some issues making a variable callable by my next get request. I've found a few things while searching but never found a 100% answer on how to call the environment variable in the get request...is it correct to use the {{TEST}} to call that var. Example below.
Test = Myaccoount
Json_Response_Test = requests.get('https://thisisjustatesttoaccessmyaccount/{{Test}}')
How can I carry over Test into the request?
Your code will almost work as you have it if you use the feature of newer version of Python called "format strings". These are denoted by a f at the beginning of the string. This works like this in such versions of Python:
Test = Myaccoount
Json_Response_Test = requests.get(f'https://thisisjustatesttoaccessmyaccount/{Test}')
as long as Myaccoount is a valid value that can be expanded by Python into the format string.
If you're using an older version of Python, you could do something like this:
Test = Myaccoount
Json_Response_Test = requests.get('https://thisisjustatesttoaccessmyaccount/{}'.format(Test))
BTW, it's not good form to use uppercase first character names for variables. The convention is to use uppercase only for class and type names, and use lowercase for variable and field names.

How to substitute a query value into a command argument

I need to call a shell process but pass a value in from a database query for the fourth argument, I've tried everything under the sun to get this to work including using % and {{, etc. This works if I hardcode the key in the last argument:
document = get_object_or_404(Document, document_id=buid)
stream = subprocess.check_output(['multichain-cli','chain1','liststreamkeyitems','stream1','14fee98fd60e42afbcb0c5bb3e57847c'])
But if I try and pass it in using various methods such as:
stream = subprocess.check_output(['multichaincli','chain1','liststreamkeyitems','stream1','%i' % document.document_id])
It wont work. Any help would be much appreciated, I've spent all afternoon trying to google the solution but I have a feeling I am phrasing the question wrongly.

How to insert values inside latex document using python script?

I have latex document in which there are various fields and there values are to be generated dynamically. So, what i am planning is to have python script which will generate values related to field and then insert it inside the latex document. The document looks as follows:
Project = ABC
Version = 1.0.0
Date = xyz
Now the values of project , version and date are to be filled by using python script. So, please help me how can i have the values inside latex document. I searched and have got generating the whole latex document from python but i want the two processes to be seperate. So, please help. I have with me the latex document code so, i realy don't want to play around with the code as its completly new to me, i just want to feed values inside the various fields using python.
If I understand your intention, I would just replace the values within the LaTeX source by named variables, such as $project instead of ABC, $version instead of 1.0.0 etc. Then you can run the following Python script to substitute these named variables by their actual values. This assumes that the LaTeX source doesn't contain other occurrences of text conflicting with the variable syntax $xy. If it is not the case, other syntax can be chosen.
You didn't specify how you get the values into the python program. Here I assume you can define them statically within the python code.
The program will fail when an undefined variable name (not present in the dictionary) is found within the source file. It could also be changed to leave such text unchanged or replace it by empty string depending on your needs.
#!/usr/bin/env python
import sys
import re
variables = {
'project': 'ABC',
'version': '1.0.0',
'date': 'xyz',
}
def run(args):
if len(args) == 1:
filename = args[0]
else:
sys.stderr.write("Filename must be passed as argument.\n")
sys.exit(1)
regex = re.compile(r"\$([a-zA-Z][a-zA-Z_]*)")
with open(filename) as f:
for line in f:
sys.stdout.write(regex.sub(lambda m: variables[m.group(1)], line))
if __name__ == '__main__':
run(sys.argv[1:])

Working with Parameters containing Escaped Characters in Python Config file

I have a config file that I'm reading using the following code:
import configparser as cp
config = cp.ConfigParser()
config.read('MTXXX.ini')
MT=identify_MT(msgtext)
schema_file = config.get(MT,'kbfile')
fold_text = config.get(MT,'fold')
The relevant section of the config file looks like this:
[536]
kbfile=MT536.kb
fold=:16S:TRANSDET\n
Later I try to find text contained in a dictionary that matches the 'fold' parameter, I've found that if I find that text using the following function:
def test (find_text)
return {k for k, v in dictionary.items() if find_text in v}
I get different results if I call that function in one of two ways:
test(fold_text)
Fails to find the data I want, but:
test(':16S:TRANSDET\n')
returns the results I know are there.
And, if I print the content of the dictionary, I can see that it is, as expected, shown as
:16S:TRANSDET\n
So, it matches when I enter the search text directly, but doesn't find a match when I load the same text in from a config file.
I'm guessing that there's some magic being applied here when reading/handling the \n character pattern in from the config file, but don't know how to get it to work the way I want it to.
I want to be able to parameterise using escape characters but it seems I'm blocked from doing this due to some internal mechanism.
Is there some switch I can apply to the config reader, or some extra parsing I can do to get the behavior I want? Or perhaps there's an alternate solution. I do find the configparser module convenient to use, but perhaps this is a limitation that requires an alternative, or even self-built module to lift data out of a parameter file.

How can I use faker in conjunction with Robot Framework?

I've been trying to use the faker library to generate data without having it in my test cases as static data.
I have tried calling fake.md5(raw_output=False) directly from my keyword and also by creating a variable and assigning it this value, but neither has the intended effect. It seems that no matter what I do, the only output I'm getting during my test is fake.md5(raw_output=False).
What am I doing wrong?
Edit: My keyword (it writes to a specific field, this is just a test keyword to make sure I can use faker) -
Write username
${md5}= MD 5
${my data}= log md5: ${md5}
Input Text a11y-username ${my data}
Edit #2 - I realized I had missed out the log keyword, I have updated my code
The problem is in this statement:
${my data}= md5: ${md5}
Robot expects the first cell (or the first cell after a variable name) to be a keyword. So, in this case it thinks md5: ${md5} is a keyword, which it obviously is not. That is why you get the error No keyword with name 'md5: ${md5}' found.
I don't know what you're expecting to do with that line of code. Your value is already in a variable, are you trying to copy it to another variable, or simply print it out?
If your intention was to log the value, use the Log keyword:
Write username
${md5}= MD 5
log md5: ${md5}
If you instead want to copy the value to another variable, you can use the Set Variable keyword:
write username
${md5}= MD 5
${my data}= set variable ${md5}
Input Text a11y-username ${my data}

Categories

Resources