Robot Framework + AutoIt:字典不包含键

2 投票
1 回答
3036 浏览
提问于 2025-04-18 15:39

我现在正在玩一个叫做AutoIt的库,它是用来和Robot Framework一起使用的。所以我在做一个典型的“我的第一个”Robot GUI测试,测试计算器!不幸的是,我下面的代码出现了一个错误:字典中没有键 'View Scientific'。。这个错误发生在启动计算器这个步骤中。似乎每当我调用选择计算器菜单项时,就会出现错误。

*** Settings ***
| Documentation    | Tests AutoIt Library
| Suite Setup      | Start Calculator
| Library          | AutoItLibrary
| Library          | Collections
| Library          | String
| Variables        | CalculatorGUIMap.py


*** Test Cases ***
| Integer Addition
| | Click Buttons | 2 2 + 2 =
| | Win Wait | Calculator | 24
| | ${Ans}= | Get Answer
| | Should Be Equal As Numbers | ${Ans} | 24


*** Keywords ***
| Start Calculator
| | Run | calc.exe
| | Wait For Active Window | Calculator
| | Get Calculator Version
| | Select Calculator Menu Item | View Scientific
| | Send | 12345
| | ${Result} | ${ErrMsg} = | Run Keyword And Ignore Error | Win Wait
| | ...       | Calculator  | 12345
| | Run Keyword If | "${Result}"=="FAIL" | Select Calculator Menu Item | View Digit grouping
| | Win Wait | Calculator | 12345
| | Click Button | Clear

| Click Button
| | [Arguments]     | ${Button Text}
| | ${ButtonName} = | Get From Dictionary | ${GUIMAP} | ${ButtonText}
| | Control Click   | Calculator          | ${EMPTY}  | ${ButtonName}

| Click Buttons
| | [Arguments] | ${ButtonNames}
| | @{Buttons}= | Split String  | ${ButtonNames}
| | :FOR        | ${ButtonName} | IN             | @{Buttons}
| |             | Click Button  | ${ButtonName}

| Select Calculator Menu Item
| | [Arguments] | ${MenuItem}
| | ${AltKeys}= | Get From Dictionary | ${MENUMAP} | ${MenuItem}
| | Send  | {ALTDOWN}
| | Sleep | 1
| | Send  | ${AltKeys}
| | Send  | {ALTUP}

| Get Calculator Version
| | Send  | {ALTDOWN}
| | Sleep | 1
| | Send  | ha
| | Send  | {ALTUP}
| | Win Wait Active | About Calculator | Version
| | ${WinText}= | Control Get Text | About Calculator | Version | 13579
| | ${WinText2}= | Run Keyword If   | "Version" not in "${WinText}" | Control Get Text
| | ...          | About Calculator | Version                       | Static4
| | ${WinText}= | Set Variable If | "version" in "${WinText2}" | ${WinText2}
| | ...         | ${WinText}
| | Run Keyword If | "Version" not in "${WinText}" | Fail | Can't find Calculator version
| | ${GUIMAP}= | Set Variable If | "5.1" in "${WinText}" | ${GUIMAP_51}
| | ${GUIMAP}= | Set Variable If | "6.0" in "${WinText}" | ${GUIMAP_60}
| | ...        | ${GUIMAP}
| | ${GUIMAP}= | Set Variable If | "6.1" in "${WinText}" | ${GUIMAP_61}
| | ...        | ${GUIMAP}
| | Run Keyword If | ${GUIMAP}== None | Fail | Calculator version not supported: ${WinText}
| | Set Suite Variable | ${GUIMAP}
| | ${MENUMAP}= | Set Variable If | "5.1" in "${WinText}" | ${GUIMAP_51}
| | ${MENUMAP}= | Set Variable If | "6.0" in "${WinText}" | ${GUIMAP_60}
| | ...          | ${MENUMAP}
| | ${MENUMAP}= | Set Variable If | "6.1" in "${WinText}" | ${GUIMAP_61}
| | ...          | ${MENUMAP}
| | Set Suite Variable | ${MENUMAP}
| | Control Click | About Calculator | Version | Button1

| Get Answer
| | Select Calculator menu Item | Edit Copy
| | ${Answer}= | Clip Get
| | [Return] | ${Answer}

这个步骤为什么会产生错误呢?

提前谢谢你。

1 个回答

1

“字典中没有键 'View Scientific'”这个提示应该很容易理解。你试图用键 "View Scientific" 去访问一个字典,但这个字典里没有这个键。

在你的情况中,你有一个叫 ${MENUMAP} 的字典,显然它里没有键 "View Scientific"。你需要找出为什么这个字典缺少这个键。一个简单的方法是记录一下这个字典,这样你就能看到它里面有哪些键。

撰写回答