Giter VIP home page Giter VIP logo

Comments (5)

Snooz82 avatar Snooz82 commented on July 30, 2024 2

MS Excel and typed cells
Microsoft Excel xls or xlsx file have the possibility to type thair data cells. Numbers are typically of the type float. If these data are not explicitly defined as text in Excel, pandas will read it as the type that is has in excel. Because we have to work with strings in Robot Framework® these data are converted to string. This leads to the situation that a European time value like "04.02.2019" (4th January 2019) is handed over to Robot Framework® in Iso time "2019-01-04 00:00:00". This may cause unwanted behavior. To mitigate this risk you should define Excel based files explicitly as text within Excel.

Alternatively you may deactivate that string conversion. To do so, you have to add the option preserve_xls_types to True. In that case, you will get str, float, boolean, int, datetime.time, datetime.datetime and some others.

*** Settings ***
Library    DataDriver    file=my_data_source.xlsx    preserve_xls_types=True

from robotframework-datadriver.

Snooz82 avatar Snooz82 commented on July 30, 2024

Hallo Jörg,

Nice that you like it!

Regarding the DataTypes:
At the moment the XLS(X) reader are configured so that they do convert all stuff to strings.
But this could be made configurable.

So that this test keyword:

*** Keywords ***
Check Variables
    [Arguments]    ${var_1}    ${var_2}    ${var_name}   ${var_doc}   ${var_tags}
    Log To Console    \n
    Log To Console    \${var_1}: ${{type($var_1)}} - ${var_1}
    Log To Console    \${var_2}: ${{type($var_2)}} - ${var_2}
    Log To Console    \${var_name}: ${{type($var_name)}} - ${var_name}
    Log To Console    \${var_doc}: ${{type($var_doc)}} - ${var_doc}
    Log To Console    \${var_tags}: ${{type($var_tags)}} - ${var_tags}

With this excel file:
image

results in that output:

==============================================================================
Defaults Xlsx Data Type                                                       
==============================================================================
Test 123 :: €‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ                               

${var_1}: <class 'float'> - 1.0
${var_2}: <class 'str'> - Hello
${var_name}: <class 'datetime.time'> - 08:00:30
${var_doc}: <class 'int'> - 1
${var_tags}: <class 'str'> - ['tag1', 'tag2']
Test 123 :: €‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ                               | PASS |
------------------------------------------------------------------------------
default 3.41423 True :: ÖÄ?Üß!)=§$                                    

${var_1}: <class 'float'> - 3.41423
${var_2}: <class 'bool'> - True
${var_name}: <class 'str'> - WAHR
${var_doc}: <class 'float'> - 0.02
${var_tags}: <class 'str'> - ['tag1']
default 3.41423 True :: ÖÄ?Üß!)=§$                                    | PASS |
------------------------------------------------------------------------------
My Test Name                                                          

${var_1}: <class 'float'> - 13.0
${var_2}: <class 'bool'> - False
${var_name}: <class 'str'> - 123
${var_doc}: <class 'str'> - 
${var_tags}: <class 'str'> - []
My Test Name                                                          | PASS |
------------------------------------------------------------------------------
default 1.23 2012-10-05 00:00:00                                      

${var_1}: <class 'float'> - 1.23
${var_2}: <class 'datetime.datetime'> - 2012-10-05 00:00:00
${var_name}: <class 'str'> - 05.06.21
${var_doc}: <class 'str'> - 
${var_tags}: <class 'str'> - []
default 1.23 2012-10-05 00:00:00                                      | PASS |
------------------------------------------------------------------------------
Defaults Xlsx Data Type                                               | PASS |
4 tests, 4 passed, 0 failed
==============================================================================

If you would like to have the option preserve_xls_types=True for xlsx_reader could you please raise an issue? on Github?

Anyway:
You can also already now use the RF type notation for int ${1}, float ${1.0}, and boolean ${true}.
or Python evaluations ${{2+int("3")}}

Cheers

from robotframework-datadriver.

Snooz82 avatar Snooz82 commented on July 30, 2024

Original Discussion here:
https://forum.robotframework.org/t/get-a-cells-data-type-from-a-datadriver-excel-input-sheet-based-on-its-cell-format/2331

from robotframework-datadriver.

joergschultzelutter avatar joergschultzelutter commented on July 30, 2024

Works like a charm - thanks for applying this change!

from robotframework-datadriver.

joergschultzelutter avatar joergschultzelutter commented on July 30, 2024

Minor addendum:

When preserve_xls_types=True is used in combination with empty Excel cells, the cell's variable type is always returned as str. If you need to detect these empty Excel cells in Robot and rather prefer a fake None data type over a str data type, the following workaround might be helpful:

Get Data Type
    [Arguments]    ${object}
    Return From Keyword If    not "${object}"    None

    ${VARTYPE}=     Evaluate     type($object).__name__
    [Return]        ${VARTYPE}

from robotframework-datadriver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.