Appium appium 对 hybridapp 进行 driver.getcontexts 无法获取 webview,多谢大牛看看

wang · 2017年10月26日 · 最后由 zhoupeiyue 回复于 2022年08月30日 · 2150 次阅读

appium 对 hybridapp 进行 driver.getcontexts 无法获取 webview

我们公司的 app 准确的说是单页面的 app,像 webapp,里面就是一个 webview 做所有业务功能,native 只做了一个简单的壳子来嵌入 wenview。
开始在 windows 环境把 appium 一切工作准备好了,启动 Andrid 的 app,但是 webview 一直没有获取到(webviewdebug 功能都开启了,跑去问开发:安卓的 webview 用的是 crosswalkview),后面论坛找贴找到了相关的答案,https://testerhome.com/topics/5221

按照方法把 appium 的里面的文件改了改,总算打印出来了(webview,native)两种 context,那应该可以继续来,但是切换到 webview 一直报错,一直没找到答案。

索性先放弃了 andriod-app,虽然开始费老大劲了。所以转战 ios-app,提前开发了解 webview 用的是原生的(心中窃喜原生的应该没有多问题,找到 webview 一切用 xpath 可以搞定),整了台 mac 开始一步一步整,环境就绪,启动 appium 跑脚本(见证奇迹的时刻开始),令人忧伤的问题 打印出来的还是 只有 native
附上我的代码(感觉就是最简单的获取上下文,没毛病啊):

#coding:utf-8
# urs/bin/python
import os
import  time
from appium import  webdriver

desire_caps = {}
desire_caps['platformName']= 'iOS'
desire_caps['platformVersion']= '8.4'
desire_caps['deviceName']= 'iphone4s'
desire_caps['app']= os.path.abspath('/Users/guan/Desktop/iSRM.ipa')
driver = webdriver.Remote('http://localhost:4723/wd/hub',desire_caps)
time.sleep(30)
print 'gggggg'
print driver.contexts
#driver.switch_to.context('WEBVIEW');
print driver.current_context;
quit(driver)

相关的打印信息

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/guan/PycharmProjects/test/iosapp.py
gggggg
[u'NATIVE_APP']
NATIVE_APP
<appium.webdriver.webdriver.WebDriver (session="d3fcae9f-1670-47e7-a125-93c8e30b4d75")>
Process finished with exit code 1

appium 日志:

[HTTP] --> GET /wd/hub/status {}

[MJSONWP] Calling AppiumDriver.getStatus() with args: []

[MJSONWP] Responding to client with driver.getStatus() result: {"build":{"version":"1.5.3"...
[HTTP] <-- GET /wd/hub/status 200 41 ms - 83 

[HTTP] --> GET /wd/hub/session/d3fcae9f-1670-47e7-a125-93c8e30b4d75/contexts {}

[MJSONWP] Calling AppiumDriver.getContexts() with args: ["d3fcae9f-1670-47e7-a125-9...

[debug] [iOS] Executing iOS command 'getContexts'
[debug] [iOS] Getting list of available contexts
[debug] [iOS] Retrieving contexts and views
[debug] [iOS] Selecting by url: false 
[debug] [RemoteDebugger] Getting WebKitRemoteDebugger pageArray: localhost, 27753
[debug] [RemoteDebugger] Sending request to: http://localhost:27753/json
[iOS] Attempted to get a list of webview contexts but could not connect to ios-webkit-debug-proxy. If you expect to find webviews, please ensure that the proxy is running and accessible
[debug] [iOS] No web frames found.
[MJSONWP] Responding to client with driver.getContexts() result: ["NATIVE_APP"]
[HTTP] <-- GET /wd/hub/session/d3fcae9f-1670-47e7-a125-93c8e30b4d75/contexts 200 46 ms - 86 
[HTTP] --> GET /wd/hub/session/d3fcae9f-1670-47e7-a125-93c8e30b4d75/context {}
[MJSONWP] Calling AppiumDriver.getCurrentContext() with args: ["d3fcae9f-1670-47e7-a125-9...
[debug] [iOS] Executing iOS command 'getCurrentContext'
[MJSONWP] Responding to client with driver.getCurrentContext() result: "NATIVE_APP"
[HTTP] <-- GET /wd/hub/session/d3fcae9f-1670-47e7-a125-93c8e30b4d75/context 200 3 ms - 84 

[HTTP] --> GET /wd/hub/status {}

[MJSONWP] Calling AppiumDriver.getStatus() with args: []


想到是不是 ios 原生的 webview 的 bug 属相没有开,开发说没有这个属性,我打开 xcode 看原生的 uiwebview 没找到这个属性

ios UIWebview 的原生代码对开放属性:

//
//  UIWebView.h
//  UIKit
//
//  Copyright (c) 2007-2017 Apple Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIView.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIDataDetectors.h>
#import <UIKit/UIScrollView.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, UIWebViewNavigationType) {
    UIWebViewNavigationTypeLinkClicked,
    UIWebViewNavigationTypeFormSubmitted,
    UIWebViewNavigationTypeBackForward,
    UIWebViewNavigationTypeReload,
    UIWebViewNavigationTypeFormResubmitted,
    UIWebViewNavigationTypeOther
} __TVOS_PROHIBITED;

typedef NS_ENUM(NSInteger, UIWebPaginationMode) {
    UIWebPaginationModeUnpaginated,
    UIWebPaginationModeLeftToRight,
    UIWebPaginationModeTopToBottom,
    UIWebPaginationModeBottomToTop,
    UIWebPaginationModeRightToLeft
} __TVOS_PROHIBITED;

typedef NS_ENUM(NSInteger, UIWebPaginationBreakingMode) {
    UIWebPaginationBreakingModePage,
    UIWebPaginationBreakingModeColumn
} __TVOS_PROHIBITED;

@class UIWebViewInternal;
@protocol UIWebViewDelegate;

NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrollViewDelegate> 

@property (nullable, nonatomic, assign) id <UIWebViewDelegate> delegate;

@property (nonatomic, readonly, strong) UIScrollView *scrollView NS_AVAILABLE_IOS(5_0);

- (void)loadRequest:(NSURLRequest *)request;
- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;

@property (nullable, nonatomic, readonly, strong) NSURLRequest *request;

- (void)reload;
- (void)stopLoading;

- (void)goBack;
- (void)goForward;

@property (nonatomic, readonly, getter=canGoBack) BOOL canGoBack;
@property (nonatomic, readonly, getter=canGoForward) BOOL canGoForward;
@property (nonatomic, readonly, getter=isLoading) BOOL loading;

- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

@property (nonatomic) BOOL scalesPageToFit;

@property (nonatomic) BOOL detectsPhoneNumbers NS_DEPRECATED_IOS(2_0, 3_0);
@property (nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0);

@property (nonatomic) BOOL allowsInlineMediaPlayback NS_AVAILABLE_IOS(4_0); // iPhone Safari defaults to NO. iPad Safari defaults to YES
@property (nonatomic) BOOL mediaPlaybackRequiresUserAction NS_AVAILABLE_IOS(4_0); // iPhone and iPad Safari both default to YES

@property (nonatomic) BOOL mediaPlaybackAllowsAirPlay NS_AVAILABLE_IOS(5_0); // iPhone and iPad Safari both default to YES

@property (nonatomic) BOOL suppressesIncrementalRendering NS_AVAILABLE_IOS(6_0); // iPhone and iPad Safari both default to NO

@property (nonatomic) BOOL keyboardDisplayRequiresUserAction NS_AVAILABLE_IOS(6_0); // default is YES

@property (nonatomic) UIWebPaginationMode paginationMode NS_AVAILABLE_IOS(7_0);
@property (nonatomic) UIWebPaginationBreakingMode paginationBreakingMode NS_AVAILABLE_IOS(7_0);
@property (nonatomic) CGFloat pageLength NS_AVAILABLE_IOS(7_0);
@property (nonatomic) CGFloat gapBetweenPages NS_AVAILABLE_IOS(7_0);
@property (nonatomic, readonly) NSUInteger pageCount NS_AVAILABLE_IOS(7_0);

@property (nonatomic) BOOL allowsPictureInPictureMediaPlayback NS_AVAILABLE_IOS(9_0);

@property (nonatomic) BOOL allowsLinkPreview NS_AVAILABLE_IOS(9_0); // default is NO
@end

__TVOS_PROHIBITED @protocol UIWebViewDelegate <NSObject>

@optional
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;

@end

NS_ASSUME_NONNULL_END

谢谢各位大叔叔、小姐姐帮忙看看,留下你的 idea 我会努力 try try

由于 app 一直没获取到 webview,所以我本地学开发起了服务,通过网页访问 app 的页面(我们的 app 准确的说是 webapp)so,我最近的自动化都是基于 selenuim(但是和 native 交互调取摄像机什么的都实现不了)用网页的那一套写 case ( 现在都怀疑我们的 app 太非主流了,主流的框架是不是都不适用 /(ㄒ o ㄒ)/~~)

我们 app 的截图给大家瞄一眼:

共收到 3 条回复 时间 点赞

你看有下面的报错,说明 ios-webkit-debug-proxy 没有启动起来,可参考#10951 后面 iOS 部分

[iOS] Attempted to get a list of webview contexts but could not connect to ios-webkit-debug-proxy. If you expect to find webviews, please ensure that the proxy is running and accessible

wang #2 · 2017年11月28日 Author
candysonya 回复

好的 谢谢 我尝试一下

这个问题解决了吗。楼主

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