Appium 大佬们帮忙看看这个为什么会无 session_id

FyueC · 2019年07月12日 · 最后由 FyueC 回复于 2019年07月12日 · 2261 次阅读

做 google 联系人的自动化练习,新建了一个子类,继承了 appium 中的 Webdriver,运行后提示继承的类无 session_id 属性,求解答
1.继承类代码,在 uiTest.py 文件中,只贴出了跟本题有关的方法:

from appium import webdriver
from appium.webdriver.webdriver import WebDriver
import logging
import configparser
import os
import time
class myui(WebDriver):
    """继承appium的Webdriver类,封装一些自己的方法"""
    def __init__(self,appPackage,appActivity):
        """初始化连接数据"""
        self.desired_caps = {}
        self.driver = None
        self.desired_caps = {
            'platformName': 'android',
            'platformVersion': '9',
            'deviceName': 'GM9',
            'automationName': 'uiautomator2'
        }
        self.desired_caps['appPackage'] = appPackage
        self.desired_caps['appActivity'] = appActivity

    def connect_driver(self,hostName='http://127.0.0.1:4723/wd/hub'):
        """连接appium服务器,hostname默认为本机的服务器"""
        try:
            self.driver = webdriver.Remote(hostName, self.desired_caps)
            print(type(self.driver))
            self.driver.implicitly_wait(5)
            logging.info('driver_connect success')
        except Exception as myerror:
            logging.error('connect error',myerror)

    def findElement(self,type,element):
        #能过传入type寻找类型,elment控件字串,自动查找控件,返回控件,如未找到则返回None
        e = None
        try:
            if type=='id':
                e = self.driver.find_element_by_id(element)
            elif type=='class':
                e = self.driver.find_elements_by_class_name(element)
        except Exception as er:
            logging.error('finderror:',er)
            self.getScrren()
            assert False
        return e


2.运行代码:

from time import sleep
from MTBFTest.uiTest import myui
import pytest
import logging
import mylog
import os

class TestCase_contact():

    def test_newConteact(self):
        try:
            conf = myui.getDate('contactconfig.ini')
            driver = myui(conf.get('apppackage'),conf.get('appactivity'))
            driver.connect_driver()
            driver.findElement('id',conf.get('skip_id')).click()
            sleep(3)
           driver.tap([(946,1884)],500)     #这一行代码报错
            #driver.find_element_by_xpath('//*[@content-dest="创建联系人"]')
            ine = driver.findElement('id',conf.get('in_name_f_id'))
            conteactName = ine.find_elements_by_class_name(conf.get('in_classes'))
            conteactName[0].send_keys('test')
            conteactName[1].send_keys('test')
            driver.find_element_by_id(conf.get('save_id')).click()
            e = driver.find_element_by_xpath("//*[contains(@text,'已保存')]")  # 此处为使用xpath模糊定位定位toast
            text = e.get_attribute("text")
            logging.info(text)
            assert '已保存' in text
            ui.press_keycode(4)
        except Exception as e:
            logging.error(e)
            assert False
        finally:
            driver.driverquit()
if __name__=='__main__':
    mylog.startLog()
    pytest.main(['-q'])

3.运行结果:

4.错误提示为: AttributeError: 'myui' object has no attribute 'session_id'

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 5 条回复 时间 点赞

部分 appium-log
Starting UIAutomator2 server 3.5.1
[UiAutomator2] Using UIAutomator2 server from 'C:\Users\chen5\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-uiautomator2-server\apks\appium-uiautomator2-server-v3.5.1.apk' and test from 'C:\Users\chen5\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-uiautomator2-server\apks\appium-uiautomator2-server-debug-androidTest.apk'
[UiAutomator2] Waiting up to 30000ms for UiAutomator2 to be online...
[ADB] Creating ADB subprocess with args: ["-P",5037,"-s","mtp62615352326","shell","am","instrument","-w","io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner"]
[WD Proxy] Matched '/status' to command name 'getStatus'
[WD Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/hub/status] with no body
[WD Proxy] Got an unexpected response: {"errno":"ECONNRESET","code":"ECONNRESET","syscall":"read"}
[Instrumentation] io.appium.uiautomator2.server.test.AppiumUiAutomator2Server:
[WD Proxy] Matched '/status' to command name 'getStatus'
[WD Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/hub/status] with no body
[WD Proxy] Got an unexpected response: {"errno":"ECONNRESET","code":"ECONNRESET","syscall":"read"}
[WD Proxy] Matched '/status' to command name 'getStatus'
[WD Proxy] Proxying [GET /status] to [GET http://localhost:8200/wd/hub/status] with no body
[WD Proxy] Got response with status 200: "{\"sessionId\":\"None\",\"status\":0,\"value\":\"Status Invoked\"}"
[WD Proxy] Determined that the downstream protocol for proxy is MJSONWP
[UiAutomator2] The initialization of the instrumentation process took 3178ms
[WD Proxy] Matched '/session' to command name 'createSession'
[WD Proxy] Proxying [POST /session] to [POST http://localhost:8200/wd/hub/session] with body: {"desiredCapabilities":{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"android","platformVersion":"9","deviceName":"GM9","automationName":"uiautomator2","appPackage":"com.google.android.contacts","appActivity":"com.android.contacts.activities.PeopleActivity"},"platformName":"android","platformVersion":"9","deviceName":"mtp62615352326","automationName":"uiautomator2","appPackage":"com.google.android.contacts","appActivity":"com.android.contacts.activities.PeopleActivity","deviceUDID":"mtp62615352326"}}
[WD Proxy] Got response with status 200: {"sessionId":"ef4d62ec-f76b-44c0-962b-8438c75aa8c2","status":0,"value":"Created Session"}
[WD Proxy] Proxying [GET /appium/device/info] to [GET http://localhost:8200/wd/hub/session/ef4d62ec-f76b-44c0-962b-8438c75aa8c2/appium/device/info] with no body
[WD Proxy] Got response with status 200: "{\"sessionId\":\"ef4d62ec-f76b-44c0-962b-8438c75aa8c2\",\"status\":0,\"value\":{\"androidId\":\"920408402bd2ea58\",\"manufacturer\":\"General Mobile\",\"model\":\"GM 9 Pro d\",\"brand\":\"GM\",\"apiVersion\":\"28\",\"platformVersion\":\"9\",\"carrierName\":\"\",\"realDisplaySize\":\"1080x2160\",\"displayDensity\":480,\"networks\":[]}}"
[ADB] Running 'F:\Tools\android\sdk\platform-tools\adb.exe -P 5037 -s mtp62615352326 shell dumpsys window'
[AndroidDriver] Screen already unlocked, doing nothing
[UiAutomator2] UiAutomator2 did not start the activity we were waiting for, 'com.google.android.contacts/com.android.contacts.activities.PeopleActivity'. Starting it ourselves
[ADB] Running 'F:\Tools\android\sdk\platform-tools\adb.exe -P 5037 -s mtp62615352326 shell am start -W -n com.google.android.contacts/com.android.contacts.activities.PeopleActivity -S'
[WD Proxy] Proxying [GET /appium/device/pixel_ratio] to [GET http://localhost:8200/wd/hub/session/ef4d62ec-f76b-44c0-962b-8438c75aa8c2/appium/device/pixel_ratio] with body: {}
[WD Proxy] Got response with status 200: "{\"sessionId\":\"ef4d62ec-f76b-44c0-962b-8438c75aa8c2\",\"status\":0,\"value\":3}"
[WD Proxy] Matched '/appium/device/system_bars' to command name 'getSystemBars'
[WD Proxy] Proxying [GET /appium/device/system_bars] to [GET http://localhost:8200/wd/hub/session/ef4d62ec-f76b-44c0-962b-8438c75aa8c2/appium/device/system_bars] with body: {}
[WD Proxy] Got response with status 200: "{\"sessionId\":\"ef4d62ec-f76b-44c0-962b-8438c75aa8c2\",\"status\":0,\"value\":{\"statusBar\":72}}"
[WD Proxy] Matched '/window/current/size' to command name 'getWindowSize'
[WD Proxy] Proxying [GET /window/current/size] to [GET http://localhost:8200/wd/hub/session/ef4d62ec-f76b-44c0-962b-8438c75aa8c2/window/current/size] with body: {}
[WD Proxy] Got response with status 200: "{\"sessionId\":\"ef4d62ec-f76b-44c0-962b-8438c75aa8c2\",\"status\":0,\"value\":{\"height\":2016,\"width\":1080}}"
[Appium] New AndroidUiautomator2Driver session created successfully, session 7084faeb-b4cd-4b2b-9dd9-8b153a37eab5 added to master session list
[BaseDriver] Event 'newSessionStarted' logged at 1562860342753 (23:52:22 GMT+0800 (中国标准时间))
[W3C (7084faeb)] Cached the protocol value 'W3C' for the new session 7084faeb-b4cd-4b2b-9dd9-8b153a37eab5
[W3C (7084faeb)] Responding to client with driver.createSession() result: {"capabilities":{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"android","platformVersion":"9","deviceName":"GM9","automationName":"uiautomator2","appPackage":"com.google.android.contacts","appActivity":"com.android.contacts.activities.PeopleActivity"},"platformName":"android","platformVersion":"9","deviceName":"mtp62615352326","automationName":"uiautomator2","appPackage":"com.google.android.contacts","appActivity":"com.android.contacts.activities.PeopleActivity","deviceUDID":"mtp62615352326","deviceApiLevel":28,"deviceScreenSize":"1080x2160","deviceScreenDensity":480,"deviceModel":"GM 9 Pro d","deviceManufacturer":"General Mobile","pixelRatio":3,"statBarHeight":72,"viewportRect":{"left":0,"top":72,"width":1080,"height":1944}}}
[HTTP] <-- POST /wd/hub/session 200 10805 ms - 972
[HTTP]
[HTTP] --> POST /wd/hub/session/7084faeb-b4cd-4b2b-9dd9-8b153a37eab5/timeouts
[HTTP] {"implicit":5000}
[W3C (7084faeb)] Calling AppiumDriver.timeouts() with args: [null,null,null,null,5000,"7084faeb-b4cd-4b2b-9dd9-8b153a37eab5"]
[BaseDriver] W3C timeout argument: {"implicit":5000}}
[BaseDriver] Set implicit wait to 5000ms
[W3C (7084faeb)] Responding to client with driver.timeouts() result: null
[HTTP] <-- POST /wd/hub/session/7084faeb-b4cd-4b2b-9dd9-8b153a37eab5/timeouts 200 2 ms - 14
[HTTP]
[HTTP] --> POST /wd/hub/session/7084faeb-b4cd-4b2b-9dd9-8b153a37eab5/element
[HTTP] {"using":"id","value":"android:id/button2"}
[W3C (7084faeb)] Calling AppiumDriver.findElement() with args: ["id","android:id/button2","7084faeb-b4cd-4b2b-9dd9-8b153a37eab5"]
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Waiting up to 5000 ms for condition
[WD Proxy] Matched '/element' to command name 'findElement'
[WD Proxy] Proxying [POST /element] to [POST http://localhost:8200/wd/hub/session/ef4d62ec-f76b-44c0-962b-8438c75aa8c2/element] with body: {"strategy":"id","selector":"android:id/button2","context":"","multiple":false}
[WD Proxy] Got response with status 200: {"sessionId":"ef4d62ec-f76b-44c0-962b-8438c75aa8c2","status":0,"value":{"ELEMENT":"c5857e55-f287-448f-a9bc-c534f2be420d"}}
[W3C (7084faeb)] Responding to client with driver.findElement() result: {"element-6066-11e4-a52e-4f735466cecf":"c5857e55-f287-448f-a9bc-c534f2be420d","ELEMENT":"c5857e55-f287-448f-a9bc-c534f2be420d"}
[HTTP] <-- POST /wd/hub/session/7084faeb-b4cd-4b2b-9dd9-8b153a37eab5/element 200 498 ms - 137
[HTTP]
[HTTP] --> POST /wd/hub/session/7084faeb-b4cd-4b2b-9dd9-8b153a37eab5/element/c5857e55-f287-448f-a9bc-c534f2be420d/click
[HTTP] {"id":"c5857e55-f287-448f-a9bc-c534f2be420d"}
[W3C (7084faeb)] Calling AppiumDriver.click() with args: ["c5857e55-f287-448f-a9bc-c534f2be420d","7084faeb-b4cd-4b2b-9dd9-8b153a37eab5"]
[WD Proxy] Matched '/element/c5857e55-f287-448f-a9bc-c534f2be420d/click' to command name 'click'
[WD Proxy] Proxying [POST /element/c5857e55-f287-448f-a9bc-c534f2be420d/click] to [POST http://localhost:8200/wd/hub/session/ef4d62ec-f76b-44c0-962b-8438c75aa8c2/element/c5857e55-f287-448f-a9bc-c534f2be420d/click] with body: {"element":"c5857e55-f287-448f-a9bc-c534f2be420d"}
[WD Proxy] Got response with status 200: {"sessionId":"ef4d62ec-f76b-44c0-962b-8438c75aa8c2","status":0,"value":true}
[W3C (7084faeb)] Responding to client with driver.click() result: true
[HTTP] <-- POST /wd/hub/session/7084faeb-b4cd-4b2b-9dd9-8b153a37eab5/element/c5857e55-f287-448f-a9bc-c534f2be420d/click 200 44 ms - 14
[HTTP]
[HTTP] --> DELETE /wd/hub/session/7084faeb-b4cd-4b2b-9dd9-8b153a37eab5
[HTTP] {}
[W3C (7084faeb)] Calling AppiumDriver.deleteSession() with args: ["7084faeb-b4cd-4b2b-9dd9-8b153a37eab5"]
[BaseDriver] Event 'quitSessionRequested' logged at 1562860346310 (23:52:26 GMT+0800 (中国标准时间))
[Appium] Removing session 7084faeb-b4cd-4b2b-9dd9-8b153a37eab5 from our master session list
[UiAutomator2] Deleting UiAutomator2 session
[UiAutomator2] Deleting UiAutomator2 server session
[WD Proxy] Matched '/' to command name 'deleteSession'
[WD Proxy] Proxying [DELETE /] to [DELETE http://localhost:8200/wd/hub/session/ef4d62ec-f76b-44c0-962b-8438c75aa8c2] with no body
[WD Proxy] Got response with status 200: "{\"sessionId\":\"ef4d62ec-f76b-44c0-962b-8438c75aa8c2\",\"status\":0,\"value\":\"Session deleted\"}"
[ADB] Running 'F:\Tools\android\sdk\platform-tools\adb.exe -P 5037 -s mtp62615352326 shell am force-stop com.google.android.contacts'
[Logcat] Stopping logcat capture
[ADB] Removing forwarded port socket connection: 8200
[ADB] Running 'F:\Tools\android\sdk\platform-tools\adb.exe -P 5037 -s mtp62615352326 forward --remove tcp:8200'
[UiAutomator2] Restoring hidden api policy to the device default configuration
[ADB] Running 'F:\Tools\android\sdk\platform-tools\adb.exe -P 5037 -s mtp62615352326 shell settings delete global hidden_api_policy_pre_p_apps'
[Instrumentation] .
[ADB] Running 'F:\Tools\android\sdk\platform-tools\adb.exe -P 5037 -s mtp62615352326 shell settings delete global hidden_api_policy_p_apps'
[Instrumentation] Time: 7.078
[Instrumentation]
[Instrumentation] OK (1 test)
[Instrumentation] The process has exited with code 0
[ADB] Running 'F:\Tools\android\sdk\platform-tools\adb.exe -P 5037 -s mtp62615352326 shell settings delete global hidden_api_policy'
[BaseDriver] Event 'quitSessionFinished' logged at 1562860347219 (23:52:27 GMT+0800 (中国标准时间))
[W3C (7084faeb)] Received response: null
[W3C (7084faeb)] But deleting session, so not returning
[W3C (7084faeb)] Responding to client with driver.deleteSession() result: null
[HTTP] <-- DELETE /wd/hub/session/7084faeb-b4cd-4b2b-9dd9-8b153a37eab5 200 910 ms - 14
[HTTP]

不要重写init方法或者你在init中显式调用父类的init,因为看源码可知 appium 中的 Webdriver init 调用 selenium.webdriver 的init方法,而 selenium.webdriver 中 init 是有 session_id 这个属性的

子类需要调用 super().init()

blues_mli 回复

好,万分感谢,晚上回去试试

arrow 回复

万分感谢😊

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册