How to dynamically generate the username of the execution machine

If you have a scenario which requires knowing which username the test will execute on (such as file verification), you can follow the steps below to dynamically generate the username:

 

Windows

1. Create a step with Execute_Command at the beginning of your test case 

2. Select Variable and enter a variable name (for example username)

2. Next to OS Native Commands, add 3 lines

3. Type the below commands

cmd.exe
/c
echo %username%

4. Your steps will look like this:

Commands Editor snippet that you can use by pasting in Advanced View. (on your test, click More > Advanced View)

EXECUTE COMMAND "cmd.exe","/c","echo %username%" RESULT_VARIABLE username HALT ON ERROR

 

Linux and macOS

1. Create a step with Execute_Command at the beginning of your test case

2. Select variable and enter a variable name (for example username)

3. Next to OS Native Commands, add only one line

4. Type the below command

whoami

5. Your step will look like this:

Commands Editor snippet that you can use by pasting in Advanced View. (on your test, click More > Advanced View)

EXECUTE COMMAND "whoami" RESULT_VARIABLE username HALT ON ERROR

 

All Operating Systems

If you want to use both of the above steps where the test will dynamically execute commands based on the operating system it runs on, you can use @system.os variable as follows:

1. Create a condition with IF command

2. In the Left Value, enter @system.os

3. In the Right Value, enter Windows

4. Inside the condition, add a step with Execute_Command same as in the Windows instructions above

5. Before End_IF, add a step with Else

6. After Else step, add a step with Execute_Command same as in the Linux and macOS instructions above

7. Your steps will look like this:

Commands Editor snippet that you can use by pasting in Advanced View. (on your test, click More > Advanced View)

IF @system.os EQUAL "Windows" AS TEXT 
   EXECUTE COMMAND "cmd.exe","/c","echo %username%" RESULT_VARIABLE username HALT ON ERROR
ELSE
   EXECUTE COMMAND "whoami" RESULT_VARIABLE username HALT ON ERROR
IF_END

 

Performing Dynamic File Verification

In order to verify files using the usernames which are dynamically generated as show above, you can pass the variable from the Execute_Command step to Verify_File_Download. 

 

Windows

1. Create a step with Verify_File_Download

2. In the Local Path, enter a path where the part of the username is passed from Execute_Command as below. Remember to modify file.pdf to the actual file name.

C:\Users\@username\Downloads\file.pdf

3. Your step will look like this:

Commands Editor snippet that you can use by pasting in Advanced View. (on your test, click More > Advanced View). Remember to change file.pdf to the actual file name.

VERIFY FILE_PATH "C:\\Users\\@username\\Downloads\\file.pdf" PATH_TYPE NORMAL EXISTS VALIDATE DELETE_AFTER_VERIFY TIMEOUT 0 HALT ON ERROR 

 

macOS

1. Create a step with Verify_File_Download

2. In the Local Path, enter a path where the part of the username is passed from Execute_Command as below. Remember to modify file.pdf to the actual file name.

/Users/@username/Downloads/file.pdf

3. Your step will look like this:

Commands Editor snippet that you can use by pasting in Advanced View. (on your test, click More > Advanced View). Remember to change file.pdf to the actual file name.

VERIFY FILE_PATH "/Users/@username/Downloads/file.pdf" PATH_TYPE NORMAL EXISTS VALIDATE DELETE_AFTER_VERIFY TIMEOUT 0 HALT ON ERROR 

 

Linux

1. Create a step with Verify_File_Download

2. In the Local Path, enter a path where the part of the username is passed from Execute_Command as below. Remember to modify file.pdf to the actual file name.

/home/@username/Downloads/file.pdf

3. Your step will look like this:

Commands Editor snippet that you can use by pasting in Advanced View. (on your test, click More > Advanced View). Remember to change file.pdf to the actual file name.

VERIFY FILE_PATH "/home/@username/Downloads/file.pdf" PATH_TYPE NORMAL EXISTS VALIDATE DELETE_AFTER_VERIFY TIMEOUT 0 HALT ON ERROR 

All Operating Systems

If you want to use all of the above steps where the test will dynamically verify based on the operating system it runs on, you can use @system.os variable as follows:

1. Create a condition with IF command

2. In the Left Value, enter @system.os

3. In the Right Value, enter Windows

4. Inside the condition, add a step with Verify_File_Download same as in the Windows instructions above

5. Before End_IF, add a step with Else_IF

6. In the Left Value, enter @system.os

7. In the Right Value, enter macOS

8. Inside the condition for Else_IF, add a step with Verify_File_Download same as in the macOS instructions above

9. After that, add a step with Else command

6. After Else step, add a step with Verify_File_Download same as in the Linux instructions above

7. Your steps will look like this:

Commands Editor snippet that you can use by pasting in Advanced View. (on your test, click More > Advanced View)

IF @system.os EQUAL "Windows" AS TEXT 
VERIFY FILE_PATH "C:\\Users\\@username\\Downloads\\file.pdf" PATH_TYPE NORMAL EXISTS VALIDATE DELETE_AFTER_VERIFY TIMEOUT 0 HALT ON ERROR
ELSE_IF @system.os EQUAL "macOS" AS TEXT
VERIFY FILE_PATH "/Users/@username/Downloads/file.pdf" PATH_TYPE NORMAL EXISTS VALIDATE DELETE_AFTER_VERIFY TIMEOUT 0 HALT ON ERROR
ELSE
VERIFY FILE_PATH "/home/@username/Downloads/file.pdf" PATH_TYPE NORMAL EXISTS VALIDATE DELETE_AFTER_VERIFY TIMEOUT 0 HALT ON ERROR
IF_END
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Please sign in to leave a comment.