Robot Framework单条件For循环及CSV文件解析技术问询
Hey there! Let's break down your two Robot Framework questions with clear, actionable solutions:
Robot Framework's : FOR loop is built to iterate over a sequence, so to run it against a single condition (like executing the loop body once, or only when a specific check passes), you have two straightforward approaches:
Option 1: Use a single-element list
If you just need to run the loop once, create a list with one item and iterate over it:
@{single_run} Create List dummy_value : FOR ${item} IN @{single_run} \ Log Executing loop body once \ # Add your keyword logic here (e.g., Validate Single Condition)
Option 2: Control loop count with a condition
If you want the loop to run only when a condition is true, use IN RANGE with a dynamic count (1 if the condition passes, 0 otherwise):
${is_condition_met} Set Variable ${True} # Replace with your actual condition check : FOR ${dummy} IN RANGE ${1 if is_condition_met else 0} \ Log Running loop because condition is satisfied \ # Your conditional logic goes here
Looking at your CSV structure, there's a header row and some entries with missing EndTime (like Case_2). Your current code is on the right track, but we need to handle headers, whitespace, and missing fields. Here's an improved version:
First, let's recap your CSV content for clarity:
Case, StartTime, EndTime
Case_1=Covering 3 time-slots,T00:00:00,T05:00:00
Case_2=Covering multiple time-slots,T00:15:00
Case_3=Covering one time-slot,T00:18:00,T00:47:00
Improved Parsing Code
*** Settings *** Library String Library Collections *** Test Cases *** Parse Session Data # Read the CSV file into a list of lines ${raw_lines}= Process Data File ${CURDIR}/session_data.csv # Skip the header row (first line) to get only data rows ${data_rows}= Get Slice From List ${raw_lines} 1 -1 : FOR ${line} IN @{data_rows} \ Log Processing line: ${line} \ # Split line by commas, then clean up whitespace from each field \ @{raw_columns}= Split String ${line} separator=, \ @{clean_columns}= Create List \ : FOR ${col} IN @{raw_columns} \ \ ${clean_col}= Strip String ${col} \ \ Append To List ${clean_columns} ${clean_col} \ # Extract fields with fallback for missing EndTime \ ${test_case}= Get From List ${clean_columns} 0 \ ${start_time}= Get From List ${clean_columns} 1 \ # Check if EndTime exists; use "N/A" if missing \ ${end_time}= Run Keyword If ${len(clean_columns)} < 3 Set Variable N/A \ ... ELSE Get From List ${clean_columns} 2 \ Log Case: ${test_case} | Start: ${start_time} | End: ${end_time}
Key Improvements:
- Skip headers: We slice the raw lines list to exclude the first header row, so we only process actual data entries.
- Clean whitespace: Using
Strip Stringremoves extra spaces around fields (like the space after commas in the header line). - Handle missing fields: We check the length of the cleaned columns list to detect missing
EndTime, then assign a default value ("N/A") instead of throwing an index error.
内容的提问来源于stack exchange,提问作者S.C

