简体   繁体   中英

Robot: Backslash in EXECDIR, Windows path

Underlying problem: I want to enable running robot tests using selenium 2 in a portable Firefox that's stored within EXECDIR.

${firefox_binary}=  Evaluate    sys.modules['selenium.webdriver.firefox.firefox_binary'].FirefoxBinary('${EXECDIR}${/}Firefox${/}App${/}Firefox${/}Firefox.exe')    sys, selenium.webdriver 
${firefox_profile}= Evaluate    sys.modules['selenium.webdriver.firefox.firefox_profile'].FirefoxProfile('${EXECDIR}${/}Lib${/}SeleniumFirefoxProfile') sys, selenium.webdriver 
Create Webdriver    Firefox firefox_binary=${firefox_binary}    firefox_profile=${firefox_profile}  

That works fine if, instead of ${EXECDIR}, I use the actual path.

EXECDIR is something like C:\\Users\\bart.simpson\\workspace\\projectname here. The issue is that a backslash, when followed by the b, is converted to the ASCII backslash character. The test log then says:

Evaluating expression 'sys.modules['selenium.webdriver.firefox.firefox_profile'].FirefoxProfile('C:\Users\bart.simpson\workspace\projectname\Lib\SeleniumFirefoxProfile')' failed: OSError: [Errno 20047] Unknown error: 20047: 'C:\\Users\x08art.simpson\\workspace\\projectname\\Lib\\SeleniumFirefoxProfile'

Of course I've tried using ${fixedExecDir}= Replace String ${EXECDIR} '\\' '/' and such, but nothing ever changes the outcome.

Ideas? Thanks.

Try treating the path as a raw string literal, by putting an "r" before the quote immediately before ${EXECDIR} :

${firefox_binary}=  Evaluate    ....FirefoxBinary(r'${EXECDIR}${/}Firefox...')

This should work because the robot variables are substituted before the string is passed to python, so the python interpreter only ever sees the complete string.

If you are unfamiliar with python raw string literals, see this question:

What exactly do “u” and “r” string flags do in Python, and what are raw string literals?

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