Robot Framework framework is developed in python language, so it can also be used as a library in python in essence, so when programming directly, it can be used as a library reference to implement automated framework testing based on Robot Framework based on code pattern.
from robot.api import TestSuite
from robot.api import ResultWriter
class BaiduSearchTest(object):
def __init__(self,name,librarys=['SeleniumLibrary']):
# Create test suites
self.suite = TestSuite(name)
#Import the Selenium Library package
for lib in librarys:
self.suite.resource.imports.library(lib)
#Defining variables
def create_variables(self):
#Variable expression is also ${variable name}
variables = {
"${baidu}":"https://www.baidu.com",
"${browser}":"chrome",
"${search_input}":"id=kw",
"${search_btn}":"id=su",
}
for k,v in variables.items():
self.suite.resource.variables.create(k,v)
#Test case: Start the browser
def open_browser(self):
#Use case name
test_01 = self.suite.tests.create("Start the browser")
test_01.keywords.create("Open Browser",args=["${baidu}","${browser}"]) #Use keywords and assign values
test_01.keywords.create("Title Should Be",args=['Baidu, you know'])
#Test case: Baidu search
def search_word(self):
test_02 = self.suite.tests.create("Baidu Search Test")
test_02.keywords.create("Input Text",args=["${search_input}","selenium"])
test_02.keywords.create("Click Button",args=["${search_btn}"])
test_02.keywords.create("Sleep",args=["5s"])
#Assertion of page title after successful search
def assert_title(self):
test_03 = self.suite.tests.create("Page Title Verification")
test_03.keywords.create("Title Should Be",args=['selenium_Baidu search'])
#Close the browser
def close_browser(self):
test_04 = self.suite.tests.create("Close the browser")
test_04.keywords.create("Close All Browsers")
def run(self):
self.create_variables()
self.open_browser()
self.search_word()
self.assert_title()
self.close_browser()
result = self.suite.run(critical="Baidu search",output="output.xml") #Execute the test suite and output the document
ResultWriter(result).write_results(report="report.html",log="log.html") #Generate log and report documents
if __name__=="__main__":
print("use python Written robot framework test")
suite=BaiduSearchTest("Baidu Search Test Suite")
suite.run()
For example, when using code form, variable definitions are also in the form of ${variable name}, test_01 = self.suite.tests.create("launch browser") to create test cases and set the name of the use cases.
Set keywords and assign values. Open Browser defines keywords for frameworks, or self-defined keywords, args=["${baidu}" and "${browser}" fill in parameters, similar to Open Browser ${URL} ${BROWSER} without python code.
test_01.keywords.create("Open Browser",args=["${baidu}","${browser}"])
Execution test suite
result = self.suite.run(critical="Baidu search",output="output.xml")
Generate reports and log documents
ResultWriter(result).write_results(report="report.html",log="log.html")
Robot Framework is a simple familiar language. It uses python language to use Robot Framework framework. Robot Framework framework encapsulates many classes. It really saves encapsulation of classes. But it is not used to using its keyword grammar, so it is better to use it according to specific scenarios. Powerful Robot Framework framework for solving automation problems. Learn more about records later