当前位置:网站首页>Public variables of robotframework

Public variables of robotframework

2022-04-23 16:36:00 Sink the wine cup and fleeting time

Public variables

I've learned that through Resource file Manage user keywords , Duplicate the extracted content , Simplify the operation steps in test cases

common.robot

*** Settings ***
Library     SeleniumLibrary

*** Keywords ***
 Log in to Netease mailbox 
	#  open chrome Browser access 163 mailbox 
	open browser    https://mail.163.com/       chrome
	sleep    5
	maximize browser window
	#  Switch to iframe
	select frame    xpath=//iframe[contains(@id,'iframe')]
	#  Enter... In the user name input box   user name 
	sleep    2
	input text    xpath=//*[@name="email"]      Your username 
	sleep    2
	#  Enter... In the password input box   password 
	input text    xpath=//*[@name="password"]    Your password 
	sleep    2
	#  Click the login button 
	click element    xpath=//*[@id="dologin"]
	sleep    10

 Close the browser 
	close browser
		

test.robot

*** Settings ***
Library     SeleniumLibrary
Resource    common.robot
Suite Setup        Log in to Netease mailbox 
Suite Teardown     Close the browser 

*** Test Cases ***
 Use cases 1
	log to console     Here is the body of the use case 

 Use cases 2
	log to console     Here is the body of the use case 

And the variables in the operation steps , For example, element positioning , May be used in this keyword , It will also be used in other keywords , If the element location changes , We need to change these keywords at the same time , so much trouble .
Another example is the account number 、 Data like passwords , Different accounts have different permissions , You need to use different accounts to operate .
therefore , For this type of data , It can be separated , That is to say Public variables , Play the role of sharing

RF Use of public variables

Public variables need to declare variables in the variable table , Create a new one Public variables .robot file , establish Variables surface , stay Variables Declare variables in the table

Public variables .robot

*** Variables ***

#  Element localization 
${
     Netease email address }     https://mail.163.com/
${
    iframe}      xpath=//iframe[contains(@id,'iframe')]
${
     User name input box }       xpath=//*[@name="email"]
${
     Password input box }      xpath=//*[@name="password"]
${
     The login button }      xpath=//*[@id="dologin"]

#  Account data 
${
     jurisdiction 1 account number }         user name 1
${
     jurisdiction 1 password }         password 1
${
     jurisdiction 2 account number }         user name 2
${
     jurisdiction 2 password }         password 2

And then in common.robot Import in file Public variables .robot file :Resource Public variables .robot

common.robot

*** Settings ***
Library     SeleniumLibrary
Resource     Public variables .robot


*** Keywords ***

 Log in to Netease mailbox 
	[Arguments]    ${
     account number }=${
     jurisdiction 1 account number }      ${
     password }=${
     jurisdiction 1 password }
	#  open chrome Browser access 163 mailbox 
	open browser    ${
     Netease email address }        chrome
	sleep    5
	maximize browser window
	#  Switch to iframe
	select frame    ${
    iframe}
	#  Enter... In the user name input box   user name 
	sleep    2
	input text    ${
     User name input box }      ${
     account number }
	sleep    2
	#  Enter... In the password input box   password 
	input text    ${
     Password input box }     ${
     password }
	sleep    2
	#  Click the login button 
	click element    ${
     The login button }
	sleep    10

 Close the browser 
	close browser
		

In this case , Element positioning has changed , Only need Public variables .robot Just modify the file , The default login account is jurisdiction 1 account number , If you need to use another account ,
stay Log in to Netease mailbox Add the variables corresponding to the account and password to be used after the keyword
test.robot

*** Settings ***
Library     SeleniumLibrary
Resource    common.robot
Suite Setup        Log in to Netease mailbox 	${
     jurisdiction 2 account number } 	${
     jurisdiction 2 password }
Suite Teardown     Close the browser 

*** Test Cases ***
 Use cases 1
	log to console     Here is the body of the use case 

 Use cases 2
	log to console     Here is the body of the use case 

list Variable

Public variables .robot In file , The variable type can also be list Variable

For example, in the process of project iteration , There will be multiple test environments , By creating a list Variable , Put the addresses of these test environments in list Variable in . Here we use Netease email address For example , Be careful , When you declare a variable here, it becomes @{ Netease email address }

Public variables .robot

*** Variables ***

#  Element localization 
@{
     Netease email address }     https://mail.126.com/		https://mail.163.com/
${
    iframe}      xpath=//iframe[contains(@id,'iframe')]
${
     User name input box }       xpath=//*[@name="email"]
${
     Password input box }      xpath=//*[@name="password"]
${
     The login button }      xpath=//*[@id="dologin"]

#  Account data 
${
     jurisdiction 1 account number }         user name 1
${
     jurisdiction 1 password }         password 1
${
     jurisdiction 2 account number }         user name 2
${
     jurisdiction 2 password }         password 2

Use here https://mail.163.com/ This address , stay common.robot The file needs to be changed to ${ Netease email address }[1]( The second parameter )
common.robot

*** Settings ***
Library     SeleniumLibrary
Resource     Public variables .robot


*** Keywords ***

 Log in to Netease mailbox 
	[Arguments]    ${
     account number }=${
     jurisdiction 1 account number }      ${
     password }=${
     jurisdiction 1 password }
	#  open chrome Browser access 163 mailbox 
	open browser    ${
     Netease email address }[1]        chrome
	sleep    5
	maximize browser window
	#  Switch to iframe
	select frame    ${
    iframe}
	#  Enter... In the user name input box   user name 
	sleep    2
	input text    ${
     User name input box }      ${
     account number }
	sleep    2
	#  Enter... In the password input box   password 
	input text    ${
     Password input box }     ${
     password }
	sleep    2
	#  Click the login button 
	click element    ${
     The login button }
	sleep    10

 Close the browser 
	close browser
		

dict Variable

Public variables .robot In file , The variable type can be list Variable , It can also be dict Variable

Public variables .robot In file , The account password can be written like this
Public variables .robot

*** Variables ***

#  Element localization 
@{
     Netease email address }     https://mail.126.com/		https://mail.163.com/
${
    iframe}      xpath=//iframe[contains(@id,'iframe')]
${
     User name input box }       xpath=//*[@name="email"]
${
     Password input box }      xpath=//*[@name="password"]
${
     The login button }      xpath=//*[@id="dologin"]

#  Account data 
&{
     Account data }     jurisdiction 1 account number = user name 1	  jurisdiction 1 password = password 1	  jurisdiction 2 account number = user name 2	  jurisdiction 2 password = password 2

Here is an example , You don't use the default parameters , Account content is ${ Account data }[ jurisdiction 1 account number ], The corresponding is user name 1, The password content is ${ Account data }[ jurisdiction 1 password ], The corresponding is password 1
common.robot

*** Settings ***
Library     SeleniumLibrary
Resource     Public variables .robot


*** Keywords ***

 Log in to Netease mailbox 
	
	#  open chrome Browser access 163 mailbox 
	open browser    ${
     Netease email address }[1]        chrome
	sleep    5
	maximize browser window
	#  Switch to iframe
	select frame    ${
    iframe}
	#  Enter... In the user name input box   user name 
	sleep    2
	input text    ${
     User name input box }      ${
     Account data }[ jurisdiction 1 account number ]
	sleep    2
	#  Enter... In the password input box   password 
	input text    ${
     Password input box }     ${
     Account data }[ jurisdiction 1 password ]
	sleep    2
	#  Click the login button 
	click element    ${
     The login button }
	sleep    10

 Close the browser 
	close browser
		

Use variable files

In addition to creating Public variables .robot file , establish Variables surface , stay Variables Declare variables in the table in this way , You can also use python Module file Provide public variables to RF Use , Just define variables directly , Grammar is completely python grammar

config.py

url1 = 'https://mail.126.com/'	
url2 = 'https://mail.163.com/'

stay RF The variable file is declared in the file :Variables config.py, Use variables directly $url1 that will do
common.robot

*** Settings ***
Library     SeleniumLibrary
Resource     Public variables .robot
Variables    config.py

*** Keywords ***

 Log in to Netease mailbox 
	
	#  open chrome Browser access 163 mailbox 
	open browser    $url1        chrome
	sleep    5
	maximize browser window
	#  Switch to iframe
	select frame    ${
    iframe}
	#  Enter... In the user name input box   user name 
	sleep    2
	input text    ${
     User name input box }      ${
     Account data }[ jurisdiction 1 account number ]
	sleep    2
	#  Enter... In the password input box   password 
	input text    ${
     Password input box }     ${
     Account data }[ jurisdiction 1 password ]
	sleep    2
	#  Click the login button 
	click element    ${
     The login button }
	sleep    10

 Close the browser 
	close browser
		

版权声明
本文为[Sink the wine cup and fleeting time]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231402128887.html