0

I'm trying to log in to the database server with applications run there. I can login without any problems, however when I try to log the working directory on the server it shows me the default directory after logging in and not the one i changed.

Whole testcase looks like this:

*** Settings ***
Library  SSHLibrary

*** Variables ***
${IP}  IP
${user}  user
${password}  password
*** Test Cases ***
CMD
    Open connection  ${IP}
    login  ${user}  ${password}
    execute command  cd gtms/bin
    ${pwd}  execute commanrd  pwd
    log  ${pwd}

And I am expecting to get info about the directory I am in when I use pwd, but it is not working. I get this in LOG:

KEYWORD BuiltIn . Log ${pwd}
Documentation:  
Logs the given message with the given level.
Start / End / Elapsed:  20170807 16:07:14.266 / 20170807 16:07:14.267 /         
00:00:00.001
16:07:14.267    INFO    /home/ollie

Can anyone tell me what I'm doing wrong?

Thanks in advance.

Mateusz
  • 171
  • 1
  • 1
  • 12
  • 1
    What is result code of that `cd` command? You can have it with `return_rc=True` for `Execute Command`. – Jan Kovařík Aug 07 '17 at 15:05
  • Is this your actual code? I ask that because `execute commanrd` is spelled wrong and should throw an error. – Bryan Oakley Aug 07 '17 at 17:15
  • Added return_rc=True and got in the LOG: KEYWORD SSHLibrary . Execute Command cd gtms/bin, return_rc=True Documentation: Executes `command` on the remote machine and returns its outputs. Start / End / Elapsed: 20170808 08:51:19.274 / 20170808 08:51:19.305 / 00:00:00.031 08:51:19.274 INFO Executing command 'cd gtms/bin'. 08:51:19.305 INFO Command exited with return code 1. – Mateusz Aug 08 '17 at 06:50

1 Answers1

0

Execute Command always runs the specified command(s) in a new shell - thus the cd in the first call is not persisted, as visible by the pwd in the second call. This actually is the example in its documentation :

The command is always executed in a new shell. Thus possible changes to the environment (e.g. changing working directory) are not visible to the later keywords:
${pwd}= Execute Command pwd
Should Be Equal ${pwd}  /home/johndoe
Execute Command cd /tmp 
${pwd}= Execute Command pwd
Should Be Equal ${pwd}  /home/johndoe

In order to achieve what you want, just chain the target commands, and execute once:

${output}=    Execute Command  cd gtms/bin; pwd
Log    ${output}    # will log the output of the executed command(s) - in this case, pwd
Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
  • That is really helpful. How can I then log pwd from the chained commands to check if that really works? – Mateusz Aug 09 '17 at 09:39
  • That's not only helpful, that's the answer ;) The `Execute Command` returns the output of whatever is executed - in this case, the chained commands, so you just assign it to a variable, and do whatever is needed. I've updated the answer to include it. – Todor Minakov Aug 09 '17 at 11:12
  • Added what you suggested but the output is still wrong: KEYWORD ${output} = SSHLibrary . Execute Command cd gtms/bin; pwd Executes `command` on the remote machine and returns its outputs. Start / End / Elapsed: 20170809 13:14:24.004 13:14:24.004 INFO Executing command 'cd gtms/bin; pwd'. 13:14:24.280 INFO Command exited with return code 0. 13:14:24.280 INFO ${output} = /home/ollie 00:00:00.000KEYWORD BuiltIn . Log ${output} Logs the given message with the given level. Start / End / Elapsed: 20170809 13:14:24.281 13:14:24.281 INFO /home/ollie – Mateusz Aug 09 '17 at 11:16
  • The system doesn't cd - are you sure there is a directory `/home/ollie/gtms/bin`? Go to the shell with the user ollie, and execute `cd`, and then `cd gtms/bin; pwd` - what is the output? If the directory doesn't exist, it will most probably be "/home/ollie" on 1 line - that's the stdout, and something like "-bash: cd: gtms/bin: No such file or directory" - that's the error for the non-existing directory, in stderr. – Todor Minakov Aug 09 '17 at 11:39
  • 1
    It works now. Seems like during my vacation there was a change in structure and gtms/bin was moved to another directory. Sorry for the confusion and thank you very much for answering my question. – Mateusz Aug 09 '17 at 12:07