机器人框架嵌套if语句

时间:2016-01-04 11:21:34

标签: python robotframework

我需要在我的测试用例中使用嵌套的if语句。

我需要检查变量a是否等于X,如果确实如此,我需要检查变量b是否等于Y.

我尝试过这样的事情:

Click on button
Run Keyword If                      '${var_a}' == 'X'
...         Run Keyword If                      '${var_b}' == 'Y'
...                 Click Element               Locator_a
...                 ELSE
...                 Click Element               Locator_b

...         ELSE
...         Click Element                       Locator_c

我收到的错误是点击元素预期1参数并得到4。 意味着一旦它为第一个if语句(var_a == X)返回False,它就会尝试调用第一个ELSE语句,并将所有后面的关键字作为参数(Click Element,Arg1 = locator_b,Arg2 = Else,Arg3 = Click Element,Arg5 = Locator_c)。

如果没有自己编写自定义关键字,是否有机器人方法可以做到这一点?

1 个答案:

答案 0 :(得分:5)

严格地说,不,没有办法做你想做的事。但是,您可以将if语句组合成一个大型语句,其中三个条件由“ELSE IF”和“ELSE”连接:

[Files]
; Embed the second license files
; (the part after underscore must match the Name parameter from Languages section)
Source: "license2_english.txt"; Flags: dontcopy
Source: "license2_czech.txt"; Flags: dontcopy
; Other languages
...

[Code]

var
  SecondLicensePage: TOutputMsgMemoWizardPage;
  License2AcceptedRadio: TRadioButton;
  License2NotAcceptedRadio: TRadioButton;

procedure CheckLicense2Accepted(Sender: TObject);
begin
  { Update Next button when user (un)accepts the license }
  WizardForm.NextButton.Enabled := License2AcceptedRadio.Checked;
end;

function CloneLicenseRadioButton(Source: TRadioButton): TRadioButton;
begin
  Result := TRadioButton.Create(WizardForm);
  Result.Parent := SecondLicensePage.Surface;
  Result.Caption := Source.Caption;
  Result.Left := Source.Left;
  Result.Top := Source.Top;
  Result.Width := Source.Width;
  Result.Height := Source.Height;
  Result.OnClick := @CheckLicense2Accepted;
end;

procedure InitializeWizard();
var
  LicenseFileName: string;
  LicenseFilePath: string;
begin
  { Create second license page, with the same labels as the original license page }
  SecondLicensePage :=
    CreateOutputMsgMemoPage(
      wpLicense, SetupMessage(msgWizardLicense), SetupMessage(msgLicenseLabel),
      SetupMessage(msgLicenseLabel3), '');

  { Shrink license box to make space for radio buttons }
  SecondLicensePage.RichEditViewer.Height := WizardForm.LicenseMemo.Height;

  { Load license }
  { Loading ex-post, as Lines.LoadFromFile supports UTF-8, }
  { contrary to LoadStringFromFile. }
  LicenseFileName := 'license2_' + ActiveLanguage + '.txt';
  ExtractTemporaryFile(LicenseFileName);
  LicenseFilePath := ExpandConstant('{tmp}\' + LicenseFileName);
  SecondLicensePage.RichEditViewer.Lines.LoadFromFile(LicenseFilePath);
  DeleteFile(LicenseFilePath);

  { Clone accept/do not accept radio buttons for the second license }
  License2AcceptedRadio :=
    CloneLicenseRadioButton(WizardForm.LicenseAcceptedRadio);
  License2NotAcceptedRadio :=
    CloneLicenseRadioButton(WizardForm.LicenseNotAcceptedRadio);

  { Initially not accepted }
  License2NotAcceptedRadio.Checked := True;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  { Update Next button when user gets to second license page }
  if CurPageID = SecondLicensePage.ID then
  begin
    CheckLicense2Accepted(nil);
  end;
end;