Python Appium实现页面对象模型

时间:2017-06-29 16:00:01

标签: python selenium appium python-appium

我正在尝试使用appium实现'By'和'Keys',就像我在selenium上实现它一样。

在硒上,我可以这样做:

定位符

from selenium.webdriver.common.by import By

class LoginPageLocators(object):
    HEADING = (By.CSS_SELECTOR, 'h3[class="panel-title"]')
    USERNAME = (By.NAME, 'username')
    PASSWORD = (By.NAME, 'password')
    LOGIN_BTN = (By.CSS_SELECTOR, 'input[value="Login"]')

功能

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from base import Page
from locators.locators import *

class LoginPage(Page):

    def __init__(self, context):
        Page.__init__(
            self,
            context)

    def goto_login_page(self, url):
        self.open(url)

    def enter_username(self, username):
        uname = self.find_element(*LoginPageLocators.USERNAME)
        uname.send_keys(username)

    def enter_password(self, password):
        pword = self.find_element(*LoginPageLocators.PASSWORD)
        pword.send_keys(password)

    def click_login(self):
        login = self.find_element(*LoginPageLocators.LOGIN_BTN)
        login.click()

    def verify_dashboard_page(self, page):
        self.verify_page(page)

在appium中有这种方法吗?如果我这样做,就没有模块:

 from appium.webdriver.common.by import By
 from appium.webdriver.common.keys import Keys

2 个答案:

答案 0 :(得分:3)

from appium.webdriver.common.mobileby import By
from appium.webdriver.common.mobileby import MobileBy

class FirstPageLocators(object):
    LOCATOR_ONE = (MobileBy.ACCESSIBILITY_ID, 'id')
    LOCATOR_TWO = (MobileBy.XPATH, 'xpath_value')

答案 1 :(得分:1)

我用另一种方式定位。

我有page_object目录,其中包含应用程序页面的文件和常规文件-base_page。在Base_page.py中,我包括了其他页面的常规操作方法。示例:

SELECT [SocioNome] FROM Socios NOT IN (SELECT [NomeSocio] FROM [RegistroDePagamento] WHERE [MesDoBoleto]=[Forms]![Selecionar_Mes_Cobranca]![TBoxMes] AND [AnoDoBoleto]=[Forms]![Selecionar_Mes_Cobranca]![TBoxAno]);

为了找到正确的定位器方法,我在同一文件base_page.py中创建了自定义方法。示例:

from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ex_cond
from src.platform import PLATFORM, IS_IOS

class BasePage:

    def __init__(self, driver: webdriver) -> None:
        self._driver = driver

    def get_element(self, locator: str, timeout=10):
        by = get_locator_by_string(locator)
        return WebDriverWait(self._driver, timeout).until(
            ex_cond.visibility_of_element_located(by), ' : '.join(by))

    def get_no_element(self, locator: str, timeout=10):
        by = get_locator_by_string(locator)
        element = WebDriverWait(self._driver, timeout).until(
            ex_cond.invisibility_of_element_located(by), ' : '.join(by))
        if element is None:
            return 'No element found'


    def get_element_text(self, locator: str, timeout=10):
        by = get_locator_by_string(locator)
        element = WebDriverWait(self._driver, timeout).until(
            ex_cond.visibility_of_element_located(by), ' : '.join(by))
        if IS_IOS:
            return element.get_attribute('label')
        else:
            return element.text

    def get_element_and_click(self, locator: str, timeout=10):
        by = get_locator_by_string(locator)
        element = WebDriverWait(self._driver, timeout).until(
            ex_cond.visibility_of_element_located(by), ' : '.join(by))
        return element.click()

Get_locator_by_string并不包含所有搜索方法。有我需要的方法。

在locators文件中,我有两种方法-适用于Android和iOS。示例:

def get_locator_by_string(locator_with_type):
    exploided_locator = locator_with_type.split(':', 1)
    by_type = exploided_locator[0]
    locator = exploided_locator[1]

    if by_type == 'xpath':
        return (MobileBy.XPATH, locator)
    elif by_type == 'css':
        return (MobileBy.CSS_SELECTOR, locator)
    elif by_type == 'id':
        return (MobileBy.ID, locator)
    elif by_type == 'accessibility_id':
        return (MobileBy.ACCESSIBILITY_ID, locator)
    elif by_type == 'android_uiautomator':
        return (MobileBy.ANDROID_UIAUTOMATOR, locator)
    elif by_type == 'ios_uiautomation':
        return (MobileBy.IOS_UIAUTOMATION, locator)
    elif by_type == 'ios_predicate':
        return (MobileBy.IOS_PREDICATE, locator)
    elif by_type == 'class':
        return (MobileBy.CLASS_NAME, locator)
    else:
        raise Exception(f'Cannot get type of locator. Locator 
            {locator_with_type}')

要为平台选择正确的定位器,我使用了用于平台检查的方法。示例:

import allure
from src.config import BUNDLE_APP
from src.ui.base_page import BasePage, locator_for_platform


class AuthorPage(BasePage):

    _author_title = locator_for_platform({
        'ANDROID': 'id:%s:id/authorName' % BUNDLE_APP,
        'IOS': 'accessibility_id:author_name'
    })

    _subscribe_button = locator_for_platform({
        'ANDROID': 'id:%s:id/subscribeBackground' % BUNDLE_APP,
        'IOS': 'accessibility_id:author_subscribe_button'
    })

    @allure.step('Press subscribe button')
    def press_subscribe_button(self):
        super().get_element_and_click(self._subscribe_button)

    @allure.step('Get subscribe button text')
    def get_subscribe_button_text(self):
        return super().get_element_text(self._subscribe_button_text)

    @allure.step('Get author\'s name text')
    def get_author_name_text(self):
        return super().get_element_text(self._author_title)

要检查当前平台的文件,请执行以下操作:

def locator_for_platform(selectors):
    return selectors.get(PLATFORM, 'Undefined Selector')

以及用于设置当前测试平台的最后一个文件:

import os
from appium import webdriver
from src import config


def get_env(key, default=None):
    return os.environ.get(key=key, default=default)


def get():
    if IS_ANDROID:
        return webdriver.Remote(
            command_executor=config.APPIUM_HOST,
            desired_capabilities=config.DESIRED_CAPS_ANDROID_RESET
        )
    else:
        return webdriver.Remote(
            command_executor=config.APPIUM_HOST,
            desired_capabilities=config.DESIRED_CAPS_ANDROID_RESET
        )


PLATFORM = get_env('PLATFORM', 'IOS')

IS_ANDROID = PLATFORM == 'ANDROID'
IS_IOS = PLATFORM == 'IOS'
其他文件中的

APPIUM_HOST和DESIRED_CAPS_ANDROID_RESET / DESIRED_CAPS_ANDROID_RESET。示例:

#!/usr/bin/env bash

export PLATFORM=IOS
export PLATFORM=ANDROID

DEVICES和MY_APP_ANDROID是另一本字典。

我希望我的经验对您有所帮助。