Appium Appium v1.4.16 上 安卓真机测试混合 App 时 clear 方法无法工作,怎么解决?

appium · 2016年03月01日 · 最后由 黑水 回复于 2016年03月02日 · 1960 次阅读

环境:

Appium: 1.4.16
selenium: 2.52.0
安卓版本 5.1.1
测试app:官方selendroid-test-app.apk  

官方 selendroid-test-app.apk
对该 App 测试官方代码样例在地址
官方混合应用文档说明:指导说明
我修改后的代码:

PLATFORM_VERSION = '5.1.1'
class AndroidWebViewTests(unittest.TestCase):

    def setUp(self):
        app = os.path.abspath(
                os.path.join(os.path.dirname(__file__),
                             '../../apps/selendroid-test-app.apk'))
        desired_caps = {
            'app': app,
            'appPackage': 'io.selendroid.testapp',
            'appActivity': '.HomeScreenActivity',
            'platformName': 'Android',
            'platformVersion': PLATFORM_VERSION,
            'deviceName': 'emulator-22'
        }


        if (PLATFORM_VERSION != '4.4'):
            desired_caps['automationName'] = 'selendroid'

        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def test_webview(self):
        if (PLATFORM_VERSION == '4.4'):
            button = self.driver.find_element_by_accessibility_id('buttonStartWebviewCD')
        else:
            button = self.driver.find_element_by_name('buttonStartWebviewCD')
        button.click()

    sleep(2)
    self.driver.switch_to.context('WEBVIEW_0') # NATIVE_APP
    print self.driver.page_source
        input_field = self.driver.find_element_by_id('name_input')
        sleep(1)
    # input_field.click()
    sleep(1)
        input_field.clear()
    # error due to clear method 
    input_field.send_keys('Appium User')
        input_field.submit()

日志记录:

Device launched! Ready for commands
info: [debug] Setting command timeout to the default of 60 secs
info: [debug] Appium session started with sessionId 255aebc2-5b02-a65b-1277-4a75e57bcab0
info: <-- POST /wd/hub/session 303 9829.718 ms - 74 
info: --> GET /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0 {}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0","method":"GET"}
info: [debug] Proxied response received with status 200: {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","status":0,"value":{"warnings":{},"desired":{"deviceName":"emulator-22","automationName":"selendroid","appActivity":".HomeScreenActivity","platformVersion":"5.1.1","appPackage":"io.selendroid.testapp","platformName":"Android","app":"/home/r00t/android_sample_code/sample-code-master/sample-code/apps/selendroid-test-app.apk"},"deviceName":"emulator-22","automationName":"selendroid","appActivity":".HomeScreenActivity","platformVersion":"22","appPackage":"io.selendroid.testapp","platformName":"android","app":"/home/r00t/android_sample_code/sample-code-master/sample-code/apps/selendroid-test-app.apk","takesScreenshot":true,"browserName":"selendroid","rotatable":true,"platform":"android","handlesAlerts":true,"javascriptEnabled":true,"networkConnectionEnabled":true,"version":"0.17.0","acceptSslCerts":true}}
info: <-- GET /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0 200 40.246 ms - 863 
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: --> POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element {"using":"name","sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","value":"buttonStartWebviewCD"}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element","method":"POST","json":{"using":"name","sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","value":"buttonStartWebviewCD"}}
info: [debug] Proxied response received with status 200: {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","status":0,"value":{"ELEMENT":"73d3f5e2-b3cd-e1bf-202b-e8c6067f3566"}}
info: <-- POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element 200 35.439 ms - 122 
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: --> POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/73d3f5e2-b3cd-e1bf-202b-e8c6067f3566/click {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","id":"73d3f5e2-b3cd-e1bf-202b-e8c6067f3566"}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/73d3f5e2-b3cd-e1bf-202b-e8c6067f3566/click","method":"POST","json":{"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","id":"73d3f5e2-b3cd-e1bf-202b-e8c6067f3566"}}
info: [debug] [SELENDROID] HTTP Server started on port 4450
info: [debug] Proxied response received with status 200: {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","status":0,"value":""}
info: <-- POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/73d3f5e2-b3cd-e1bf-202b-e8c6067f3566/click 200 645.889 ms - 74 
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: [debug] [SELENDROID] setSystemUiVisibility vis=0 mask=1 oldVal=0 newVal=0 diff=0
info: [debug] [SELENDROID] [CDS]close[4450]
info: [debug] [SELENDROID] 1456806721463, io.selendroid.testapp/isExcluded = false/isTopTaskHome = true/isFirstValidTask = true/t.id = 822
info: [debug] [SELENDROID] 1456806367861, com.github.shadowsocks/isExcluded = false/isTopTaskHome = true/isFirstValidTask = false/t.id = 815
info: [debug] [SELENDROID] 1456805995827, com.mediatek.data/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456796856891, io.appium.unlock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = 798
info: [debug] [SELENDROID] 1456787722406, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456786824199, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456759960451, com.mediatek.op01.plugin/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456725925274, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456701313411, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456700625750, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456639510090, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456614921167, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456614037116, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456527632593, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456441228169, com.android.deskclock/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: [debug] [SELENDROID] 1456419952134, com.android.packageinstaller/isExcluded = true/isTopTaskHome = true/isFirstValidTask = false/t.id = -1
info: --> POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/context {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","name":"WEBVIEW_0"}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: /home/r00t/software/sdk/platform-tools/adb -s ZTEQ519T shell "cat /proc/net/unix"
info: [debug] Available contexts: undefined
info: [debug] []
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/window_handles","method":"GET"}
info: [debug] Available contexts: ["WEBVIEW_0","NATIVE_APP"]
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/context","method":"POST","json":{"name":"WEBVIEW_0"}}
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0"}
info: <-- POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/context 200 216.166 ms - 76 {"status":0,"value":null,"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0"}
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: --> GET /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/source {}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/source","method":"GET"}
info: [debug] Proxied response received with status 200: {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","status":0,"value":"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Say Hello Demo</title></head><body>Hello, can you please tell me your name?<form name=\"myform\" action=\"http://localhost:4450/sayhello\" method=\"get\"><div align=\"center\"><br /><br /><input type=\"text\" id=\"name_input\" name=\"name\" size=\"25\" value=\"Enter your name here!\" /><br /><p>Prefered Car:<br /><select name=\"car\"><option value=\"volvo\">Volvo</option><option value=\"mercedes\">Mercedes</option><option value=\"audi\">Audi</option></select></p><br /><input type=\"submit\" value=\"Send me your name!\" /><br /></div></form></body></html>"}
info: <-- GET /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/source 200 248.182 ms - 692 
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: --> POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element {"using":"id","sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","value":"name_input"}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element","method":"POST","json":{"using":"id","sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","value":"name_input"}}
info: [debug] Proxied response received with status 200: {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","status":0,"value":{"ELEMENT":":wdc:1456806724295"}}
info: <-- POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element 200 93.817 ms - 104 
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: --> POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/:wdc:1456806724295/click {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","id":":wdc:1456806724295"}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/:wdc:1456806724295/click","method":"POST","json":{"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","id":":wdc:1456806724295"}}
info: [debug] [SELENDROID] [CMCC Performance test][SystemUI][Statusbar] show signal end [391214664]
info: [debug] [SELENDROID] isEmailSend:POST
info: [debug] [SELENDROID] getAllHeaders:POST
info: [debug] [SELENDROID] test:Content-Type:application/x-www-form-urlencoded
info: [debug] [SELENDROID] test:Accept:*/*
info: [debug] [SELENDROID] test:Accept-Encoding:gzip
info: [debug] [SELENDROID] httpHeader:null
info: [debug] [SELENDROID] this.mCheckHandler:android.net.Proxy$DefaultHttpRequestCheckHandler@3397d2ef
info: [debug] [SELENDROID] [CDS][DNS] getAllByNameImpl netId = 0
info: [debug] [SELENDROID ERR] java.util.concurrent.TimeoutException
info: [debug] [SELENDROID ERR]  at java.util.concurrent.FutureTask.get(FutureTask.java:176)
info: [debug] [SELENDROID ERR]  at avg.run(SogouSource:653)
info: [debug] [SELENDROID ERR]  at java.lang.Thread.run(Thread.java:818)
info: [debug] Proxied response received with status 200: {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","status":0,"value":""}
info: <-- POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/:wdc:1456806724295/click 200 989.412 ms - 74 
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: --> POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/:wdc:1456806724295/clear {"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","id":":wdc:1456806724295"}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/:wdc:1456806724295/clear","method":"POST","json":{"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0","id":":wdc:1456806724295"}}
info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"CATCH_ALL: io.selendroid.server.common.exceptions.SelendroidException: {\"message\":\"Element is not currently interactable and may not be manipulated\"}\n\tat io.selendroid.server.model.SelendroidWebDriver.executeAtom(SelendroidWebDriver.java:214)\n\tat io.selendroid.server.model.SelendroidWebDriver.executeAtom(SelendroidWebDriver.java:181)\n\tat io.selendroid.server.model.AndroidWebElement.clear(AndroidWebElement.java:285)\n\tat io.selendroid.server.handler.ClearElement.safeHandle(ClearElement.java:36)\n\tat io.selendroid.server.handler.SafeRequestHandler.handle(SafeRequestHandler.java:86)\n\tat io.selendroid.server.AndroidServlet.handleRequest(AndroidServlet.java:264)\n\tat io.selendroid.server.common.BaseServlet.handleHttpRequest(BaseServlet.java:67)\n\tat io.selendroid.server.common.http.ServerHandler.channelRead(ServerHandler.java:53)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)\n\tat io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)\n\tat io.netty.handler.traffic.AbstractTrafficShapingHandler.channelRead(AbstractTrafficShapingHandler.java:223)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)\n\tat io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)\n\tat io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)\n\tat io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)\n\tat io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)\n\tat io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:148)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)\n\tat io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)\n\tat io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787)\n\tat io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:125)\n\tat io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)\n\tat io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:430)\n\tat io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:384)\n\tat io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)\n\tat io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)\n\tat io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)\n\tat java.lang.Thread.run(Thread.java:818)\n"},"sessionId":"255aebc2-5b02-a65b-1277-4a75e57bcab0"}
info: <-- POST /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0/element/:wdc:1456806724295/clear 500 205.145 ms - 3083 
info: [debug] [SELENDROID] [CDS]close[8080]
info: [debug] [SELENDROID] close [socket][/127.0.0.1:8080]
info: --> DELETE /wd/hub/session/255aebc2-5b02-a65b-1277-4a75e57bcab0 {}
info: Shutting down appium session

切到 web 视图后 driver.page_source 输出为:

<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Say Hello Demo</title></head><body>Hello, can you please tell me your name?<form name=\"myform\" action=\"http://localhost:4450/sayhello\" method=\"get\"><div align=\"center\"><br /><br /><input type=\"text\" id=\"name_input\" name=\"name\" size=\"25\" value=\"Enter your name here!\" /><br /><p>Prefered Car:<br /><select name=\"car\"><option value=\"volvo\">Volvo</option><option value=\"mercedes\">Mercedes</option><option value=\"audi\">Audi</option></select></p><br /><input type=\"submit\" value=\"Send me your name!\" /><br /></div></form></body></html>

从 driver.page_source 结果中,可以看出 web 视图中具有 id 值为 name_input 的表单(value="Enter your name here!"),而且可以定位到该元素,为什么说可以定位到该元素,因为,把调用 clear 的代码

input_field.clear()

注释掉后测试就正常了。那么,如何解决调用 clear 方法清理表单值的异常?

共收到 5 条回复 时间 点赞

在补充一条记录

/XXXXXXX/python2.7/site-packages/selenium/webdriver/remote/webelement.py:89: in clear
    self._execute(Command.CLEAR_ELEMENT)
/XXXXXXX/python2.7/site-packages/selenium/webdriver/remote/webelement.py:469: in _execute
    return self._parent.execute(command, params)
/XXXXXXX/python2.7/site-packages/selenium/webdriver/remote/webdriver.py:201: in execute
    self.error_handler.check_response(response)

这是不是提示 selenium 的问题?

想到两次调用

send_keys()

方法,第一次发送空字符串,第二次再发自定义字符串,结果不是我的预期:它把空字符串也输入到表单中了😭

我被你搞混乱了,到底是 clear() 方法有问题还是 click() 有问题?

你的标题说 click 不行,但正文明显是说 clear 报错。。。

#3 楼 @chenhengjie123 已修改。是 clear 方法清楚表单值时异常,多谢指出。您有什么高见?

为什么代码里注释这行# input_field.click()
log 里看还是 click 了呢

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