I would like to find some word after convert PDF.
1)I have 2 PDF at path C:\TRM\PDF 1.pdf and 2.pdf
1.pdf has word "ICG00058"
2.pdf has word "ICG00065"
2) Convert Pdf To Txt at ${detail_1} already.
3) Suppose, I don't know the word in 1.pdf and I want to check that
1.pdf has ICG00058 or ICG00065.
I'm so sorry,If the question is not clear.
Please investigate this issue for me cause It's paramount important for my work.
*** Settings ***
Library Selenium2Library
Library String
Library Pdf2TextLibrary
*** Test Cases ***
Read PDF
${detail_1} Convert Pdf To Txt C:\\TRM\\PDF\\1.pdf
LOG ${detail_1}
${ID_1} Get Regexp Matches ${detail_1} ICG00058
${ID_2} Get Regexp Matches ${detail_1} ICG00065
Run Keyword And Ignore Error $ID_1[0] in $detail_1 LOG ${ID_1}
Run Keyword If $ID_2[0] in $detail_1 LOG ${ID_2}
ERROR:
Evaluating expression 'RF_VAR_ID_2 [0 ]in RF_VAR_detail_2' failed: IndexError: list index out of range
I used pdfgrep just like grep you can search for regex expressions in the pdf without any intermediate step.
I used it to look up ISBN numbers in the pdfs and automatically rename the filename to include the found ISBNs or write the filename and ISBN to an MySQL database.
If you don’t know how to write regex there are some online tools to test regex online until you find the right one to use.
You need to call the Evaluate keyword if you want to run python within your robot script, that is why you are seeing the error you have mentioned.
In your case though you could use the Get Index From List and List Should Contain Value keywords:
${matched_id_1}= Get Index From List ${ID_1} 0
Run Keyword And Ignore Error List Should Contain Value ${detail_1} ${matched_id_1}
${matched_id_2}= Get Index From List ${ID_2} 0
Run Keyword And Ignore Error List Should Contain Value ${detail_1} ${matched_id_2}
If you want to use the List Should Contain Value keywords as assertions just remove the Run Keyword And Ignore Error keywords in the answer
Related
I've seen that since 4.0.0, R supports raw strings using the syntax r"(...)". Thus, I could do:
r"(C:\THIS\IS\MY\PATH\TO\FILE.CSV)"
#> [1] "C:\\THIS\\IS\\MY\\PATH\\TO\\FILE.CSV"
While this is great, I can't figure out how to make this work with a variable, or better yet with a function. See this comment which I believe is asking the same question.
This one can't even be evaluated:
construct_path <- function(my_path) {
r"my_path"
}
Error: malformed raw string literal at line 2
}
Error: unexpected '}' in "}"
Nor this attempt:
construct_path_2 <- function(my_path) {
paste0(r, my_path)
}
construct_path_2("(C:\THIS\IS\MY\PATH\TO\FILE.CSV)")
Error: '\T' is an unrecognized escape in character string starting ""(C:\T"
Desired output
# pseudo-code
my_path <- "C:\THIS\IS\MY\PATH\TO\FILE.CSV"
construct_path(path)
#> [1] "C:\\THIS\\IS\\MY\\PATH\\TO\\FILE.CSV"
EDIT
In light of #KU99's comment, I want to add the context to the problem. I'm writing an R script to be run from command-line using WIndows's CMD and Rscript. I want to let the user who executes my R script to provide an argument where they want the script's output to be written to. And since Windows's CMD accepts paths in the format of C:\THIS\IS\MY\PATH\TO, then I want to be consistent with that format as the input to my R script. So ultimately I want to take that path input and convert it to a path format that is easy to work with inside R. I thought that the r"()" thing could be a proper solution.
I think you're getting confused about what the string literal syntax does. It just says "don't try to escape any of the following characters". For external inputs like text input or files, none of this matters.
For example, if you run this code
path <- readline("> enter path: ")
You will get this prompt:
> enter path:
and if you type in your (unescaped) path:
> enter path: C:\Windows\Dir
You get no error, and your variable is stored appropriately:
path
#> [1] "C:\\Windows\\Dir"
This is not in any special format that R uses, it is plain text. The backslashes are printed in this way to avoid ambiguity but they are "really" just single backslashes, as you can see by doing
cat(path)
#> C:\Windows\Dir
The string literal syntax is only useful for shortening what you need to type. There would be no point in trying to get it to do anything else, and we need to remember that it is a feature of the R interpreter - it is not a function nor is there any way to get R to use the string literal syntax dynamically in the way you are attempting. Even if you could, it would be a long way for a shortcut.
My requirement is to check whether the input is list or not using robot framework.
I tried using type(${temp}).name with evaluate function, which works in case of list and fails for string type.
Below is the error message -
Evaluating expression 'type(testdata).name' failed: SyntaxError: invalid token (, line 1)
Tried to use regex, but no luck.:(
Code :-
testRegEx
${match} Run Keyword Should Match Regexp ["swerwv","sfsdfdsf","edsfdf"] \[\s\S\]
Log To Console ${match}
output:-
FAIL : '["swerwv","sfsdfdsf","edsfdf"]' does not match '[\s\S]'
I'm new to robotframework. Any help would be appreciated.
My requirement is to check whether the input is list or not using robot framework.
You can use robot's special syntax for evaluate and the various keywords which accept expressions, where you omit the brackets in the variable reference to pass the actual variable to the expression (versus passing the value of the variable).
Example:
*** Variables ***
#{a_list} one two three
*** Test Cases ***
Test that variable is a list
run keyword unless type(a_list) == list
... Fail not a list
This feature is mentioned in the Evaluating expressions section of the built-in library.
Situation.. I have two tags defined, then I try to output them to the console. What comes out seems to be similar to an array, but I'd like to remove the formatting and just have the actual words outputted.
Here's what I currently have:
[Tags] ready ver10
Log To Console \n#{TEST TAGS}
And the result is
['ready', 'ver10']
So, how would I chuck the [', the ', ' and the '], thus only retaining the words ready and ver10?
Note: I was getting [u'ready', u'ver10'] - but once I got some advice to make sure I was running Python3 RobotFramework - after uninstalling robotframework via pip, and now only having robotframework installed via pip3, the u has vanished. That's great!
There are several ways to do it. For example, you could use a loop, or you could convert the list to a string before calling log to console
Using a loop.
Since the data is a list, it's easy to iterate over the list:
FOR ${tag} IN #{Test Tags}
log to console ${tag}
END
Converting to a string
You can use the evaluate keyword to convert the list to a string of values separated by a newline. Note: you have to use two backslashes in the call to evaluate since both robot and python use the backslash as an escape character. So, the first backslash escapes the second so that python will see \n and convert it to a newline.
${tags}= evaluate "\\n".join($test_tags)
log to console \n${tags}
I have written below code in robot framework. How to verify if that ${args} is in ${Mylist}
Create Article
[Arguments] ${args}
${MyList}= Create List 'Federal News' 'EY News'
Run Keyword If ${args} in ${MyList} Run keywords Click Element ${tab1_Xpath} AND Wait Until Page Contains Federal News
And I am calling Create Article ${arg} from another file. If I use the above script I am getting the error.
Any help would be appreciated
The first argument to Run keyword if must be a valid expression. Because you have more than one space around "in", robot thinks ${args} is the expression, and in and ${MyList} are additional arguments. TO fix this you need to have a single space on either side of "in".
The second problem is that you're creating a list of strings that includes quote marks. I'm guessing that you don't actually want that. If you want your list to contain the string Federal News you need to omit the quotes.
Finally, the expression needs to be valid python syntax after variable substitution. Since you're comparing strings, you need to either quote the arguments or use the special variable syntax $args. Using the special syntax is usually the best solution, since you don't h ave to worry about whether the variable itself contains quotes.
Finally, I would recommend using ${arg} rather than ${args} since it's a scalar rather than a list.
Assuming your data does not actually include the quote marks, here's how I would do it:
Create article
[Arguments] ${arg}
${MyList}= Create List Federal News EY News
Run Keyword If $arg in $MyList Run keywords
... Click Element ${tab1_Xpath}
... AND Wait Until Page Contains Federal News
With that, you can run the keyword like this:
Create article Federal News
Bryan has the perfect answer, here is a workaround to do the same task by using below keywords from collections library
1)Run Keyword And Return Status - Returns True or False for a keyword success or failure
2)List Should Contain Value - Check items in list
Create article
[Arguments] ${arg}
${MyList}= Create List Federal News EY News
${status}= Run Keyword And Return Status List Should Contain Value ${mylist} ${arg}
Run Keyword If ${status}==True Run keywords
... Click Element ${tab1_Xpath}
... AND Wait Until Page Contains Federal News
How Can we use value returned from one keyword as input argument to other keyword directly (without assigning return value to variable)
Currently I am maintaining all Web Elements or Variable names and corresponding xpath in an Excel Sheet. I get XPath using keyword read_xpath by passing web element/variable name as argument.
I store the xpath in separate variable then make use of it for other or Next line keyword. Since I need to use a variable for each XPath access, i am trying to find out, whether is there any way to directly use one keyword output as input to other keyword without assigning it to variable.
The main purpose of storing XPath in Excel sheet is to avoid multiple test cases change with single Element XPath change, making just single change on Excel Sheet should be sufficient.
For Example:
read_xpath reads values from a Excel sheet, Excel sheet has two columns, one variable name and second one is xpath. Function read_xpath(element) takes variable name as input and gives back xpath.
xlread.py file looks like xlread.py code Excel Sheet look like Excel sheet
sample.robot file looks as below
${login_user_textctrl}= read_xpath username_textctrl
clear element text ${login_user_textctrl}
Input text ${login_user_textctrl} admin
Now let us check how I used read_xpath keyword in my robot file
I called Keyword read_xpath with argument username_texctrl, which returns xpath for username text Control which is stored in variable ${login_user_textctrl}. Now Input Text ${login_user_textctrl} admin works fine.
For below code
clear element text read_xpath username_textctrl
I am getting, clear element text requires 1 argument but two arguments provided, is there any way i can use the the value returned from Keyword(read_xpath) as input to other keyword directly, without assigning it to any variable?
Thanks in advance.
Separating your locators from your robot code through an Object Repository is often done when the locators are reused a lot. An alternative implementation paradigm is to use the Page Object Model. An example of such an implementation can be found in the Robotframework-PageObjectLibrary.
In case you still prefer the Object Repository approach, then using the Selenium2Library keyword Add Location Strategy might be of interest to you. Below is a working example which uses a YAML file as it's OR to fetch the search input box and search button from Google.
ObjectRepo.yaml
OR:
searchbox: '//input[#id="lst-ib"]'
searchbutton: '//button[#id="_fZl"]'
Robot Script
*** Settings ***
Library Selenium2Library
Variables ObjectRepo.yaml
*** Test Cases ***
Yaml Object Repository
[Setup] Add Location Strategy yro Yaml Locator Strategy
Open Browser http://www.google.com Chrome
Input Text yro=searchbox Robot Framework
Click Element yro=searchbutton
Sleep 3s
[Teardown] Close All Browsers
*** Keywords ***
Yaml Locator Strategy
[Arguments] ${browser} ${criteria} ${tag} ${constraints}
${xpath}= Set Variable ${OR.${criteria}}
${retVal}= Get Webelement xpath=${xpath}
[Return] ${retVal}
As you can see the abstraction through a custom locator keyword allows for clearner code and not require you to fetch into a variable for later reuse.
You can use Evaluate to call python functions directly. Example:
${my element to clear}= Evaluate read_xpath("username_textctrl") xlread
See the documentation here
(Note: Example is untested and you should check if xlread is in PYTHONPATH, or found at test run time)