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

Source: Internet
Author: User
Tags appium

Objective

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 -*-from page import toolspages = tools.parseyaml()def get_locater(clazz_name, method_name):    locators = pages[clazz_name]['locators']    for locator in locators:        if locator['name'] == method_name:            return locator{% for page, locators in page_list.items() %}class {{page}}:{% for locator in locators %}    {{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 -*-from page import toolspages = tools.parseyaml()def get_locater(clazz_name, method_name):    locators = pages[clazz_name]['locators']    for locator in locators:        if locator['name'] == method_name:            return locatorclass HomePage:    城市选择 = get_locater('HomePage', '城市选择')    首页搜索 = get_locater('HomePage', '首页搜索')    class LoginPage:    登录 = get_locater('LoginPage', '登录')    手机号登录 = get_locater('LoginPage', '手机号登录')    其它登录 = get_locater('LoginPage', '其它登录')    QQ = get_locater('LoginPage', 'QQ')    微博 = get_locater('LoginPage', '微博')    账号密码 = get_locater('LoginPage', '账号密码')    输入账号 = get_locater('LoginPage', '输入账号')    输入密码 = get_locater('LoginPage', '输入密码')    登录按钮 = get_locater('LoginPage', '登录按钮')    class MyPage:    我的 = get_locater('MyPage', '我的')    请点击登录 = get_locater('MyPage', '请点击登录')

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)

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.