繁体   English   中英

在机器人框架中进行分配时,如何让全局变量运行generate random关键字

[英]How to have a global variable running the generate random keyword while assigning in robot framework

我已经使用selenium驱动程序自动填写了在线表单,然后将所有硬编码输入都转换为变量,现在我试图为必须更改的几个变量生成随机字符串,以使每次表单提交都能正常工作。 我有字段“姓”,我想输入一个随机字符串并分配给一个变量。 然后,在代码的后面,我希望能够将数据库中的此变量与表单中输入的变量进行比较。 我的问题是,在保持全局变量的同时,似乎无法使用关键字分配变量。 我尝试了许多事情,并且阅读和研究了很多东西,但仍然无法获得它,并且可能缺少一些即时消息。

我有以下文件,这些文件具有/可以作为资源插入,以便可以在每个文件中识别该变量。

1. Tests.robot - This is the runtime file and actual file i will have my test cases
2. AboutPage.robot - This file contains all variables and keywords to fill out each field on the first page of the form
3. FormKeywords - This is just a file which contains keywords for each page of the form so it can call the keywords in about page in a logical order
4. common.robot - this has my keywords for common functions such as logging into the DB and bringing back data 

我尝试创建关键字和测试用例以返回变量。 试图在关键字中使用set global variable关键字,尝试在将其分配为变量时运行该关键字。

My Test case in the Tests.robot file is:
Test name
   [Documentation]  test is to check documents are attached in db
   Fill in about you page
   click document menu button
   Upload documents
   click continue button
   Fill in other information
   click continue button
   Click submit application button
   Log into database

The 'Fill in about you page' has the following keyword:
    Input surname

The 'Input surname' keyword is on the following page with the current code:
    ***Keywords***
    Input surname  ${RANDOM_SURNAME}
    input text  ${surname_id}  ${RANDOM_SURNAME}

Currently i have also tried to put the variable code in here as another keyword so that it can be passed to the input surname keyword. 
    Generate random surname string
    ${RANDOM_SURNAME} =  Generate Random String  8  [LOWER]
    set global variable  ${RANDOM_SURNAME}
    [Return]

    -------------------
Due to another answer and similar question on here I also tried:
    Generate random surname string
        ${RANDOM_SURNAME} =  Generate Random String  8  [LOWER]
        set global variable  ${RANDOM_SURNAME}
        [Return]
        Generate surname  ${RANDOM_SURNAME}

    Generate surname
      [Arguments]  ${RANDOM_SURNAME}
      log to console  \nattempting input surname  ${RANDOM_SURNAME}
      set global variable  ${RANDOM_SURNAME}

最初,我在变量部分尝试了以下几种变体-但我的问题是我无法在变量部分中调用“生成随机字符串”关键字。 当我将代码移到关键字部分并设置为全局变量时,在其他文件中仍然无法识别。 该变量仅在变量部分中可识别,但是它不会调用关键字:

    ***Variables***
    1. ${RANDOM_SURNAME}=  Generate random string  8  [LOWER]
    2. Set Global variable ${RANDOM_SURNAME}=  Generate random string  8     [LOWER]

 As a testcase:
    ***Test Cases*** 
    3. ${RANDOM_SURNAME}=  Generate random string  8  [LOWER]
       set global variable  ${RANDOM_SURNAME}

根据代码的不同,我可以运行它,因为它没有选择关键字,所以将“生成随机姓氏”作为姓氏插入表格或atm中。我得到的“关键字名称不能为空”,这是使用关键字方法,但是它不产生字符串或将字符串传递给final关键字。

我期望将随机字符串分配给全局变量,并且可以在其他页面对象中使用和引用该随机变量,并在enter code here和关键字。

如您所见,您无法在套件的*** Variables ***部分中调用关键字。

而是使用Suite Setup调用关键字,然后可以在该关键字中设置全局变量。

*** Settings ***
Library      String
Suite Setup  Initialize Random Variables

*** Keywords ***
Initialize Random Variables
    ${RANDOM_SURNAME}=   Generate random string  8  [LOWER]
    Set global variable  ${RANDOM_SURNAME}

*** Test Cases ***
Example 1
    log to console  surname: ${RANDOM_SURNAME}

Example 2
    log to console  surname: ${RANDOM_SURNAME}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM