Search code examples
excelconcatenationrobotframework

Robot Framework and Excel: Constructing full path to Excel-file of different parts of the path (Open Pyxl Library)


I am trying to test a short Robot Framework-code to ask user the name of Excel-file as a popup-window provided by Robot Framework's Dialogs-library. The idea is that the base of the file path (that stays the same to the folder where Excel-files would be located) is hard-coded to one variable named ${EXCEL BASE PATH} then user would give the name of Excel-file to the popup-window (as name of the file would change between different files) and that changing part of the path would be stored to ${EXCEL NAME} and third part would be hard-coded file-extension .xlsx stored to ${EXCEL FILE EXTENSION} because only files with .xlsx extension would be opened and there are not any changes to file extension.

Library used for Excel handling is OpenPyxlLibrary as that supports xlsx. ExcelLibrary only supports xls.

I got this close to working but the problem is that when concatenating the full path of those three parts written above the result is not the same as the result I am looking for.

Expected result for the full path to Excel-file to be opened would be:

C:\\Users\\developer\\Robot Framework\\Excel\\TestExcel.xlsx

But instead now when logged to console it is:

C:\Users\developer\Robot Framework\Excel\ TestExcel .xlsx

So there are missing second backslashes (required by Open Pyxl Library for path) and also spare space before .xlsx

Is there any other way to construct path used by Open Pyxl Library of different parts like that than by using Catenate-keyword like tested here? If the full Excel path is hard-coded to a single variable instead that has been working great already but the problem later would be to need to change the value of that variable when Excel-file would be changed so that is why I am trying to test a solution for user to give the name of Excel-file to be opened. Because name of the file is the changing part from file to file and other parts stay the same.

Excel-file is not yet actually opened in this simplified code and I am first trying to just log the full path of Excel-file to see if that logs correctly. When the path would be correct I could add code to actually open the Excel-file.

CommonResources.robot-file (resource-file):

*** Settings ***
Library           String
Library           BuiltIn
Library           OpenPyxlLibrary
Library           Dialogs

*** Variables ***
${EXCEL BASE PATH}    C:\\Users\\developer\\Robot Framework\\Excel\\
${EXCEL FILE EXTENSION}    .xlsx
${EXCEL NAME}     ${EMPTY}
${EXCEL SHEET}    Sheet

Provide Excel Name-file (test case-file):

Resource          Resources/CommonResources.robot

*** Test Cases ***
Provide Excel Name
    ${EXCEL NAME}    Get Value From User    Please provide name of Excel-file
    Log to console    ${EXCEL NAME}
    ${FULL EXCEL PATH}    Catenate    ${EXCEL BASE PATH}    ${EXCEL NAME}    ${EXCEL FILE EXTENSION}
    Log to console    ${FULL EXCEL PATH}

Solution

  • What you're seeing in the console for the backslashes is actually not a problem at all. The backslash character is "special" in the sense it is used to add control symbols in strings (think of \n for new line), and when it has to be used as the literal character \, it needs to be escaped - by itself :).
    So your write as a variable value C:\\Users\\developer when you want the end result to be C:\Users\developer.

    The extra whitespace - that is because of the default behavior of Catenate - it catenates the arguments with that character by default. This can be changed with the SEPARATOR argument:

    ${FULL EXCEL PATH}=   Catenate    SEPARATOR=${EMPTY}    ${EXCEL BASE PATH}    ${EXCEL NAME}    ${EXCEL FILE EXTENSION}
    

    ${EMPTY} is a special variable, equal to "" - an empty string.

    By the way, there is another way to get the end string, just use Set Variable passing the 3 other variables - the framework will substitute them with their actual values:

    ${FULL EXCEL PATH}=   Set Variable   ${EXCEL BASE PATH}${EXCEL NAME}${EXCEL FILE EXTENSION}