测试人社区

test_search文件中一直不能用base_page的find方法

直接上传代码了,麻烦大佬们看看为什么我这个myfind一直找不到方法
image

from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from page.base_page import BasePage
from page.main import Main

class App(BasePage):
_package=“com.xueqiu.android”
_appActivity = “.view.WelcomeActivityAlias”

'''代表APP基础业务行为 启动 重启 停止'''
def start(self):
    if self._driver == None:
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "emulator-5554"
        caps["appPackage"] = self._package
        caps["appActivity"] = self._appActivity
        caps["noReset"] = True
        # caps["chromedriverExecutable"] = "/Users/seveniruby/projects/chromedriver/all/chromedriver_2.20"
        self._driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self._driver.implicitly_wait(15)
    else:
        print(self._driver)
        self._driver.start_activity(self._package,self._appActivity)

    return self

def restart(self):
    pass

def stop(self):
    pass


def main(self)-> Main:
    def wait_load(driver):
        source = self._driver.page_source
        if "我的" in source:
            return True
        if "同意" in source:
            return True
        if "image_cancel" in source:
            return True
        return  False
    # WebDriverWait(self._driver,30).until(lambda x:"我的" in self._driver.page_source)
    WebDriverWait(self._driver, 30).until(wait_load)
    return Main(self._driver)

from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
from appium.webdriver import WebElement

import logging

class BasePage:
logging.basicConfig(level=logging.DEBUG)

_driver: WebDriver
_black_list=[
    (MobileBy.ID,'tv_agree'),
    (MobileBy.XPATH, '//*[@text="确定"]'),
    (MobileBy.XPATH, '//*[@text="下次再说"]'),
    (MobileBy.ID, 'image_cancel')
]
_error_max = 10
_error_count = 0
def __init__(self, driver: WebDriver = None):
    self._driver = driver

def myfind(self, locator, value: str = None):
    logging.info(value)
    logging.info(locator)
    try:
        # if isinstance(locator,tuple):
        #     return self._driver.find_element(*locator)
        # else:
        #     return self._driver.find_element(locator,value)
        # self._error_count = 0
        # 寻找控件
        element1 = self._driver.find_element(*locator) if isinstance(locator, tuple) else self._driver.find_element(
            locator, value)
        # 如果成功,清空错误计数
        self._error_count = 0
        return element1
    except Exception as e:
        if self._error_count >self._error_max :
            raise e
        self._error_count +=1
        for element in self._black_list:
            logging.info(element)
            elements = self._driver.find_elements(*element)
            if len(elements)>0:
                elements[0].click()
                return self.myfind(locator, value)
        #如果黑名单也没有就报错
        logging.warn("black list no one found")
        raise e
def get_toast(self):
    return self.myfind(MobileBy.XPATH,"//*[@class='android.widget.Toast']").text

def text(self,key):
    return (MobileBy.XPATH,"//*[@text='%s']" %key)
def  find_by_text(self,key):
    return  self.myfind(self.text(key))

#coding=utf-8
from appium.webdriver.common.mobileby import MobileBy
from page.base_page import BasePage
from page.profile import Profile
from page.search import Search
class Main(BasePage):
def goto_search_page(self):
# self.driver.find_element(MobileBy.ID, “tv_agree”).click()
self._driver.myfind(MobileBy.ID, “tv_search”).click()
self._driver.myfind((MobileBy.ID, “search_input_text”)).send_keys(“阿里巴巴”)
return Search(self._driver)
def goto_stock(self):
pass
def goto_trade(self):
pass
def goto_profile(self):
self.myfind(MobileBy.XPATH,"//*[@text=‘我的’]").click()
return Profile(self._driver)

def goto_message(self):
    pass

from appium.webdriver.common.mobileby import MobileBy
from page.base_page import BasePage

class Search(BasePage):

_name_locator = (MobileBy.ID, "name")

def search(self, key: str):
    self._driver.myfind((MobileBy.ID, "search_input_text")).send_keys(key)
    self._driver.myfind(self._name_locator).click()
    return self
    #todo:

def get_price(self,key:str)->float:
    # return float(self._driver.find(MobileBy.ID, "current_price").text) > 200
    return float(self._driver.myfind(MobileBy.ID, "current_price").text)

def add_select(self):
    element = self.find_by_text("加自选")
    element.click()
    return element.text

from page.app import App
import pytest

class TestSearch:

def setup(self):
    self.main = App().start().main()
def test_search(self):
    self.main.goto_search_page().search("alibaba").get_price("BABA") > 200
    # self.main.goto_search_page()
@pytest.mark.parametrize("key,stock_type,price",[
    ("alibaba","BABA",200),
    ("JD","JD",20)
])

def test_search_data(self,key,stock_type,price):
    assert self.main.goto_search_page().search(key).get_price(stock_type)>price

直接self.myfind()

代码的部分建议使用markdown的代码块格式
image

已经解决了,谢谢你的解答