Appium Hybird app, 想切换到 webview,但是中间有一层 floating action button, 所以执行 switch_to.context 的时候不能找到 webview

dai · 2015年11月04日 · 最后由 Monkey 回复于 2015年11月16日 · 2179 次阅读

Hybird app, 想切换到 webview,但是中间有一层 floating action button, 所以执行 switch_to.context 的时候不能找到 webview

请问有什么办法吗?
请求,衷心感谢!

共收到 21 条回复 时间 点赞
dai #1 · 2015年11月06日 Author

@seveniruby

请问你们帮我看看上面的问题吗?

#1 楼 @daibiao83 switch_to.context 并不是你想象的真正的切进去,而是拿到和 webview 之间的调试通道,你拿不到的原因和 floating xxx 没有关系。要看你的 webview 能否调试。

dai #3 · 2015年11月09日 Author

@lihuazhang
非常感谢你的回复,我想请问一个问题,如何判断 webview 是否能够访问?
其次,我找开发把 floating action button 移除掉后,是可以切换到 webview 的

#3 楼 @daibiao83 关键我不知道你说的找不到 webview 是个什么样子的场景

dai #6 · 2015年11月09日 Author

我是用 python 写的 code

current_context = self.driver.contexts
print current_context

打印出来只有 [u"NATIVE_APP"]

dai #6 · 2015年11月09日 Author

@lihuazhang 同样的 python 脚本,当把 floating action button 移除后得到的结果是

[u'NATIVE_APP', u'WEBVIEW_com.contactexplorer']

@chenhengjie123 遇到过这种问题吗?

#7 楼 @lihuazhang 目前没遇到过。我找个 sample 试下先。

#8 楼 @chenhengjie123 用 testerhome 的 android 客户端就可以了。 帖子那里就是用的 floating action button

#9 楼 @lihuazhang
#6 楼 @daibiao83 手上没有 4.4+ 的真机。。。用 genymotion 试了下 testerhome app ,可以找到 webview 的 context:

[u'NATIVE_APP', u'WEBVIEW_com.testerhome.nativeandroid']

appium 版本:1.4.13

dai #11 · 2015年11月09日 Author

@chenhengjie123
非常感谢!我也用的是 1.4.13.1, 真机是 LG G2(4.4.2), 奇怪,为什么我不行?难道我们的 app 有什么特别?还是我自己做的有问题

#11 楼 @daibiao83 贴下你的代码吧。

dai #13 · 2015年11月10日 Author

setup()

def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.2'
desired_caps['deviceName'] = '03xxxxxxxxxxxxxxx'
desired_caps['appPackage'] = 'com.good.salesforce1'
desired_caps['appActivity'] = '.LaunchActivity'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

Test code
current_context = self.driver.contexts
print current_context
self.driver.switch_to.context('WEBVIEW_com.salesforce.samples.contactexplorer')
print self.driver.current_context

console log
test_test (main.recordTheTimeDuringLaunch) ...
Step1, unlock G4SF1
Start to record Launch time...
[u'NATIVE_APP']
ERROR

======================================================================

ERROR: test_test (main.recordTheTimeDuringLaunch)

Traceback (most recent call last):
File "C:\Users\xdai\Desktop\Pythonpractice\Testing\TC9_Record_Launch_Time\recordTheTimeDuringLaunch_Timer.py", line 62, in test_test
self.driver.switch_to.context('WEBVIEW_com.salesforce.samples.contactexplorer')
File "build\bdist.win32\egg\appium\webdriver\switch_to.py", line 31, in context
self._driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {'name': context_name})
File "C:\Python27\lib\site-packages\selenium-2.47.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 196, in execute
self.error_handler.check_response(response)
File "build\bdist.win32\egg\appium\webdriver\errorhandler.py", line 27, in check_response
raise NoSuchContextException(wde.msg, wde.screen, wde.stacktrace)
NoSuchContextException: Message: No such context found.


Ran 1 test in 50.064s

FAILED (errors=1)

appium log
info: [debug] Device is at API Level 19
info: [debug] executing cmd: D:\sw\Android\android-sdk\platform-tools\adb.exe -s 03xxxxxxxxxxxxxxx sh
ell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n
com.good.salesforce1/.LaunchActivity"
info: [debug] Waiting for pkg "com.good.salesforce1" and activity ".LaunchActivity" to be focused
info: [debug] Getting focused package and activity
info: [debug] executing cmd: D:\sw\Android\android-sdk\platform-tools\adb.exe -s 03xxxxxxxxxxxxxxx sh
ell "dumpsys window windows"
info: [debug] executing cmd: D:\sw\Android\android-sdk\platform-tools\adb.exe -s 03xxxxxxxxxxxxxxx sh
ell "getprop ro.build.version.release"
info: [debug] Device is at release version 4.4.2
info: [debug] Device launched! Ready for commands
info: [debug] Setting command timeout to the default of 60 secs
info: [debug] Appium session started with sessionId c3f08750-ed35-4cab-a2db-5d9c3d01aee1
info: <-- POST /wd/hub/session 303 4748.721 ms - 74
info: --> GET /wd/hub/session/c3f08750-ed35-4cab-a2db-5d9c3d01aee1 {}
info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserNam
e":"Android","platformVersion":"4.4.2","webStorageEnabled":false,"takesScreenshot":true,"javascriptE
nabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,
"warnings":{},"desired":{"platformVersion":"4.4.2","deviceName":"03xxxxxxxxxxxxxxx","platformName":"A
ndroid","appActivity":".LaunchActivity","appPackage":"com.good.salesforce1"},"deviceName":"03xxxxxxxxxxxxxxx","platformName":"Android","appActivity":".LaunchActivity","appPackage":"com.good.salesforce1
"},"sessionId":"c3f08750-ed35-4cab-a2db-5d9c3d01aee1"}
info: <-- GET /wd/hub/session/c3f08750-ed35-4cab-a2db-5d9c3d01aee1 200 4.553 ms - 605 {"status":0,"v
alue":{"platform":"LINUX","browserName":"Android","platformVersion":"4.4.2","webStorageEnabled":fals
e,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled"
:true,"locationContextEnabled":false,"warnings":{},"desired":{"platformVersion":"4.4.2","deviceName"
:"039512595f0f029f","platformName":"Android","appActivity":".LaunchActivity","appPackage":"com.good.
salesforce1"},"deviceName":"03xxxxxxxxxxxxxxx","platformName":"Android","appActivity":".LaunchActivit
y","appPackage":"com.good.salesforce1"},"sessionId":"c3f08750-ed35-4cab-a2db-5d9c3d01aee1"}
info: --> GET /wd/hub/session/c3f08750-ed35-4cab-a2db-5d9c3d01aee1/contexts {}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: D:\sw\Android\android-sdk\platform-tools\adb.exe -s 039512595f0f029f sh
ell "cat /proc/net/unix"
info: [debug] Available contexts:
info: [debug] []
info: [debug] Available contexts: NATIVE_APP
info: [debug] Responding to client with success: {"status":0,"value":["NATIVE_APP"],"sessionId":"c3f
08750-ed35-4cab-a2db-5d9c3d01aee1"}
info: <-- GET /wd/hub/session/c3f08750-ed35-4cab-a2db-5d9c3d01aee1/contexts 200 85.690 ms - 86 {"sta
tus":0,"value":["NATIVE_APP"],"sessionId":"c3f08750-ed35-4cab-a2db-5d9c3d01aee1"}
info: --> POST /wd/hub/session/c3f08750-ed35-4cab-a2db-5d9c3d01aee1/context {"sessionId":"c3f08750-e
d35-4cab-a2db-5d9c3d01aee1","name":"WEBVIEW_com.salesforce.samples.contactexplorer"}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: D:\sw\Android\android-sdk\platform-tools\adb.exe -s 039512595f0f029f sh
ell "cat /proc/net/unix"
info: [debug] Available contexts: NATIVE_APP
info: [debug] []
info: [debug] Available contexts: NATIVE_APP
info: [debug] Responding to client with error: {"status":35,"value":{"message":"No such context foun
d.","origValue":"Context 'WEBVIEW_com.salesforce.samples.contactexplorer' does not exist"},"sessionI
d":"c3f08750-ed35-4cab-a2db-5d9c3d01aee1"}
info: <-- POST /wd/hub/session/c3f08750-ed35-4cab-a2db-5d9c3d01aee1/context 500 74.353 ms - 195
info: --> DELETE /wd/hub/session/c3f08750-ed35-4cab-a2db-5d9c3d01aee1 {}
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: D:\sw\Android\android-sdk\platform-tools\adb.exe -s 03xxxxxxxxxxxxxxx sh
ell "input keyevent 3"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"OK, shutting down","status":0}
info: [debug] [BOOTSTRAP] [debug] Closed client connection
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstr
ap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
info: [debug] [UIAUTOMATOR STDOUT] Time: 46.615
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] UiAutomator shut down normally
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"c3f08750-ed35
-4cab-a2db-5d9c3d01aee1"}
info: <-- DELETE /wd/hub/session/c3f08750-ed35-4cab-a2db-5d9c3d01aee1 200 1174.811 ms - 76 {"status"
:0,"value":null,"sessionId":"c3f08750-ed35-4cab-a2db-5d9c3d01aee1"}

dai #8 · 2015年11月13日 Author

#12 楼 @lihuazhang @chenhengjie123

sorry, 又打扰你们了,我的问题还没有解决,不过好像我们使用的是 layout 不是标准的 floating action button
请问可以给我一个邮箱什么的,我发一个简单的测试 app 给你们,帮忙看看可以吗?
非常感谢

#14 楼 @daibiao83
社区里找不到大家的信息吗?

dai #16 · 2015年11月14日 Author

@lihuazhang 谢谢,邮件已经发送,请查收

#16 楼 @daibiao83 1. 使用 markdown 格式来发帖子和回帖。2. 邮件已经收到,你代码写错了而已。

dai #18 · 2015年11月16日 Author
@lihuazhang , 请问帮忙指出错在哪些,非常感谢!
dai #19 · 2015年11月16日 Author
@lihuazhang 

上次我发给你的代码,其实第二和第三个find是没有执行,第一个就点击错误。

def test_test(self):
        print 'Start to record Launch time...'
        time.sleep(1)
        current_context = self.driver.contexts
        print current_context
        self.driver.find_element_by_class_name("android.widget.Button").click()
        self.driver.find_element_by_id("com.morningeast.layouttest:id/my_button").click()

下面两行是指的同一个操作, 两个都不是click 按钮“Click me”, 而都是点击了那个红色的图标。

self.driver.find_element_by_class_name("android.widget.Button").click()
self.driver.find_element_by_id("com.morningeast.layouttest:id/my_button").click()

#19 楼 @daibiao83 麻烦你自己研究下,排查方式就这么几种。另外请用真机试试看。另外描述问题,清晰一点,一步到位,不要浪费大家时间。

#19 楼 @daibiao83 话说。。。markdown 不是这样用的。。。。

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