简体   繁体   中英

How to remove white spaces and capitalize every first letter in the string in the robotramework

How can I remove white spaces and capitalize every first letter in the string in the robotframework, so later I use the result in the Selenium library calls?

Test to Unlock the Service Account:

  Open Browser    ${URL}    ${Browser}
  ${string_1} =      get text    ${question_1}
  ${temp_answer} =   set variable     ${string_1}.title()
  ${answer}=  evaluate       ${string_1}.replace(" ","")
  Input Text    ${Answer_1}    ${answer}
  sleep    5s

Input:

Legal business name

Output:

LegalBusinessName?

You were close to achieving it, but made two crucial mistakes. The first one is you used Set Variable and tried calling the python's title() string method in the argument - but that doesn't work for the keyword. It is a straightforward assignment - synonymous to the = operator; so what you ended up with as value was the string "Legal business name.title()". You should use the Evaluate keyword like in the second call, which does python's code eval.

The other mistake was to use two different variables - you store the capitalized version in the var ${temp_answer} , but then you don't remove the whitespace from it, but from the original one - ${string_1} . So even if the capitalization worked, you still wouldn't get the desired end result in the ${answer} var.

Here's a one-liner how to achieve what you need:

${answer}=    evaluate       """${string_1}""".title().replace(" ","")

The 2 methods are chained - replace() works on the result of title() , and the value of string_1 is in triple quotes so python works with its sting representation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM