Write robot framework tests in Python

Source: Internet
Author: User

The Robot framework framework was developed based on the Python language, so it is essentially a library of Python.

Baidu Search Example

Create the py_robot.py file with the following code:

From Robot.apiImport TestSuiteFrom Robot.apiImport ResultwriterFrom Robot.modelImport Keyword# Baidu Search TestClassBaidusearchtest:Def__init__(Self, name, librarys=["Seleniumlibrary"]):# Create test suite self.suite = TestSuite (name)# import SeleniumlibraryFor LibIn Librarys:self.suite.resource.imports.library (Lib)# define VariablesDefCreate_variables(self): variables = {"${baidu}":"Https://www.baidu.com","${browser}":"Chrome","${search_input}":"Id=kw","${search_btn}":"Id=su"}For K, VIn Variables.items (): Self.suite.resource.variables.create (k, v)# test Case: Launch browserDefOpen_browsers(self): test_01 = Self.suite.tests.create ("Start Browser") Test_01.keywords.create ("Open Browser", args=["${baidu}","${browser}"]) Test_01.keywords.create ("Title should be", args=["Baidu a bit, you will know"])# test Case: Baidu Search testDefSearch_word(self): test_02 = Self.suite.tests.create ("Baidu Search Test") test_02.keywords.create ("Input Text", args=["${search_input}","Test tutorial Net"]) Test_02.keywords.create ("Click button", args=["${search_btn}"]) Test_02.keywords.create ("Sleep", args=["5s"])# test Case: Assertion validation Search result titleDefAssert_title(self): test_03 = Self.suite.tests.create ("Assertion Verification Search result title") Test_03.keywords.create ("Title should be", args=["Test Tutorial Net _ Baidu Search"])# test Case: Close test caseDefClose_browsers(self): Test_04 = Self.suite.tests.create ("Close Browser") Test_04.keywords.create ( "Close all Browsers") # run def run# run kit result = Self.suite.run (Critical= "Baidu search", Output= "Output.xml") # generate log, report file Resultwriter (Result). Write_results (Report= " Report.html ", Log=" log.html ") if __name__ = " __main__ ": Print (" Write robot framework Test in Python ") Suite = Baidusearchtest ( "Baidu Search test Suite") Suite.run ()          

The run of this code is performed by Python commands.

> python py_robot.pyWrite robot framework test ============================================================================== in Python Baidu Search test suite ============================================================================ = = Start browser Devtools listening on Ws://127.0.0.1:12950/devtools/browser/bcbf14bb-ebc4-425c-882f-44531afd9689 launch Browser | PASS |------------------------------------------------------------------------------ Baidu Search Test | PASS |------------------------------------------------------------------------------ Assertion validation Search Result title | PASS |------------------------------------------------------------------------------ Close Browser | PASS |------------------------------------------------------------------------------Baidu Search Test Suite | PASS | Critical tests, 0 passed, 0 failed4 tests Total, 4 passed, 0 failed==================== ==========================================================output:d:\rf_test\robotse\output.xml   

Robot Framework used Well, Python is not! So, my advice is to use the Robot Framework to learn and master the Python language.

Write robot framework tests in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.