Appium+python Automation 50-Generate anchored object template Templet (JINJA2)

Source: Internet
Author: User

Every time I write PageObject positioning element object is too cumbersome, the format is similar, just change the positioning method, this can only be a template way, batch generated pageobject positioning element Object template
Python generates a template with two modules to choose from: Templet and Mako
Simply put, the purpose of this implementation is to use code to automatically generate code to automate code

Then the previous article: Appium+python Automation 49-yaml Management positioning elements

JINJA2 Introduction

1.JINJA2 Official documents: Official documents

To understand JINJA2, you need to understand the concept of templates first. Templates are widely used in Python's web development, which effectively separates business logic from page logic, makes code more readable, and easier to understand and maintain.
A template is simply a file that contains a placeholder variable that represents the dynamic part, and the template file is returned to the user after it has been dynamically assigned.
--can be understood as rendering

2.PIP Mounting JINJA2

Pip Install JINJA2

Templetpage templates

1. Create a new templetpage without a suffix, the directory format is as follows

2. Open the file and write the following template

#-*-coding:utf-8-*- fromPageImporttoolspages=Tools.parseyaml ()defGet_locater (Clazz_name, method_name): Locators= pages[clazz_name]['Locators']     forLocatorinchLocators:iflocator['name'] ==method_name:returnlocator{% forPage, LocatorsinchPage_list.items ()%}class{{page}}:{% forLocatorinchLocators%} {{Locator}}= Get_locater ('{{page}}','{{Locator}}') {% ENDFOR%}    {% ENDFOR%}

Extracting YAML Data

Generate pages.py File

Achieve results

1. Running the tools.py script will automatically generate a pages.py file in the current script directory, as follows

#-*-coding:utf-8-*- fromPageImporttoolspages=Tools.parseyaml ()defGet_locater (Clazz_name, method_name): Locators= pages[clazz_name]['Locators']     forLocatorinchLocators:iflocator['name'] ==method_name:returnLocatorclasshomepage: City selection= Get_locater ('Homepage','City Selection') Home Search= Get_locater ('Homepage','Home Search')    classloginpage: Login= Get_locater ('LoginPage','Login') mobile phone number login= Get_locater ('LoginPage','Mobile phone Number login') Other login= Get_locater ('LoginPage','Other Login') QQ= Get_locater ('LoginPage','QQ') Micro-blog= Get_locater ('LoginPage','Micro Blog') account password= Get_locater ('LoginPage','account Password') Enter your account number= Get_locater ('LoginPage','Enter your account') Enter a password= Get_locater ('LoginPage','Enter Password') Login button= Get_locater ('LoginPage','Login Button')    classMyPage: My= Get_locater ('MyPage','my own') Please click Login= Get_locater ('MyPage','Please click to login')

2. You can continue to enter the code inside, test is not able to invoke the successful

Appium+python Automation 50-Generate anchored object template Templet (JINJA2)

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.