Robot: Backslash in EXECDIR, Windows path

1.7k views Asked by At

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.

1

There are 1 answers

0
Bryan Oakley On BEST ANSWER

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?