Я хотел, чтобы прочитать файл Excel, так что я написал эти слова в рамках робот:я получил список кортежей вместо словаря в рамках робота
*** Settings ***
Library ExcelLibrary
Library Collections
Resource ../Variables/ConstantVariables.robot
Resource ../Variables/ConfigVariables.robot
*** Variables ***
*** Keywords ***
Read Data From Excel
[Arguments] ${filename} ${sheetname}
open excel ${filename}
@{excel_result} = Create List
${rows} = get_row_count ${sheetname}
:FOR ${rindex} IN RANGE 1 ${rows}
\ ${ret_val} = Get Row Values ${sheetname} ${rindex}
\ Append To List ${excel_result} ${ret_val}
[Return] ${excel_result}
Get All Values of the Row
[Arguments] ${sheetname} ${rowindex}
${cols} = get_column_count ${sheetname}
${vals} = Create Dictionary
${titles} = get_row_values ${sheetname} 0
:FOR ${cindex} IN RANGE 0 ${cols}
\ ${val} = read_cell_data_by_coordinates ${sheetname} ${cindex} ${rowindex}
\ Set To Dictionary ${vals} ${titles[0]} ${val}
[Return] ${vals}
Это файл сценария:
*** Settings ***
Resource ../Variables/ConstantVariables.robot
Resource ../Variables/ConfigVariables.robot
Resource ../Keywords/CommonKeywords.robot
*** Test Cases ***
Test For Loop
@{ret_val} = Read Data From Excel ${jobfilename} ${jobsheetname}
log to console ${ret_val}
Это выход я получил:
PS D:\Abbasimazar\workspace\RobotTests\Scenarios> pybot .\scenario.robot
==============================================================================
Scenario
==============================================================================
Test For Loop .[[('A2', u't1'), ('B2', u'n1'), ('C2', u'c1')], [
('A3', u't2'), ('B3', u'n2'), ('C3', u'c2')], [('A4', u't3'), ('B4', u'n3'), ('C4', u'c3')]]
Test For Loop | PASS |
------------------------------------------------------------------------------
Scenario | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output: D:\Abbasimazar\workspace\RobotTests\Scenarios\output.xml
Log: D:\Abbasimazar\workspace\RobotTests\Scenarios\log.html
Report: D:\Abbasimazar\workspace\RobotTests\Scenarios\report.html
Но я ожидал, чтобы получить это:
[{'A2': u't1', 'B2': u'n1', 'C2': u'c1'}, {'A3': u't2', 'B3': u'n2', 'C3': u'c2'}, {'A4': u't3', 'B4': u'n3', 'C4': u'c3'}]
Любая идея, что я сделал не так?
EDIT 1:
Как @ ombre42 сказал, что была ошибка в запросе ключевых слов. Я должен изменить строку:
${ret_val} = Get Row Values ${sheetname} ${rindex}
в:
${ret_val} = Get All Values of the Row ${sheetname} ${rindex}
Но после этого я заметил линию
${titles} = get_row_values ${sheetname} 0
дает мне это:
==============================================================================
Scenario
==============================================================================
Test For Loop .[{('A1', u'test'): u'c1'}, {('A1', u'test'): u'c2
'}, {('A1', u'test'): u'c3'}]
Test For Loop | PASS |
------------------------------------------------------------------------------
Scenario | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Так что я написал новый метод получения названий:
Get Titles From Sheet
[Arguments] ${sheetname}
${cols} = get_column_count ${sheetname}
@{titles} = Create List
:FOR ${cindex} IN RANGE 0 ${cols}
\ ${val} = read_cell_data_by_coordinates ${sheetname} ${cindex} 0
\ Append To List ${titles} ${val}
[Return] ${titles}
и изменил эту строку на:
${titles} = Get Titles From Sheet ${sheetname}
Теперь результат, как я хотел:
==============================================================================
Scenario
==============================================================================
Test For Loop .[{u'test': u't1', u'name': u'n1', u'class': u'c1'
}, {u'test': u't2', u'name': u'n2', u'class': u'c2'}, {u'test': u't3', u'name': u'n3', u'class': u'c3'}]
Test For Loop | PASS |
------------------------------------------------------------------------------
Scenario | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Да, вы правы; это была ошибка при использовании ключевых слов, которые я не заметил. Спасибо. Я также изменил некоторые другие части моего кода. –