测试人社区

调用封装的APP滑动方法时,报错'NoneType' object has no attribute 'get_window_size'

用老师上课教的UI自动化框架,然后自己封装了一套滑动手机屏幕的方法,但是一直报错’NoneType’ object has no attribute ‘get_window_size’

app.py文件内容:

         if self._driver is None:
             caps = {}
             caps["platformName"] = "android"
             caps["deviceName"] = "CMS"
             caps["appPackage"] = self._package
             caps["appActivity"] = self._activity
             caps["noReset"] = True
             self._driver = webdriver.Remote(
                 "http://localhost:4723/wd/hub",
                 caps)

     else:
         self._driver.start_activity(self._package, self._activity)
     self._driver.implicitly_wait(10)
     return self

 # ->定义返回类型
 def main(self) -> Main:
     return Main(self._driver)

方法封装在Base_Page.py文件里:

    def swipe_window(self,x,y):
        now_width = self._driver.get_window_size()["width"]
        now_height = self._driver.get_window_size()["height"]
        click_x = round(x / 720 * now_width)
        click_y = round(y / 1280 * now_height)
        return click_x,click_y

是这样调用的:

class Testtest():
    def test001(self):
        App().start().main().goto_market()
        sleep(3)
        BasePage().swipe_window(2,1)
        sleep(3)

报错信息:

self = <page.base_page.BasePage object at 0x000001A9C4D5D898>, x = 2, y = 1

    def swipe_window(self,x,y):
       now_width = self._driver.get_window_size()["width"]
E       AttributeError: 'NoneType' object has no attribute 'get_window_size'

..\page\base_page.py:112: AttributeError

Assertion failed

请求老师解答:在上课教的UI自动化框架下如何正确封装和调用手机滑动的方法

还是空指针异常self._drive没有初始化,或者初始化失败,导致为None,所以None就没有get_window_size()方法导致

那这个问题该怎么解决呢?self._drive再次初始化会不会导致APP重启呢?

首先你得找到哪里导致driver为空的,在那里修改bug或者添加初始化逻辑就好了