FLAG_SECURE:安卓无法抓取页面截屏
問題
在使用Appium Inspector對安卓-微醫(yī)生APP登陸頁元素獲取的時(shí)候發(fā)現(xiàn)無法獲取到內(nèi)容,Appium報(bào)錯(cuò)如下:
[HTTP]?-->?GET?/wd/hub/session/669c743a-cede-43f2-8950-019af660f2f6/screenshot [HTTP]?{} [MJSONWP?(669c743a)]?Calling?AppiumDriver.getScreenshot()?with?args:?["669c743a-cede-43f2-8950-019af660f2f6"] [WD?Proxy]?Matched?'/screenshot'?to?command?name?'getScreenshot' [WD?Proxy]?Proxying?[GET?/screenshot]?to?[GET?http://127.0.0.1:8200/wd/hub/session/fb84ef2a-405f-4983-b2c4-f833ec8b5562/screenshot]?with?no?body [WD?Proxy]?Got?response?with?status?500:?{"sessionId":"fb84ef2a-405f-4983-b2c4-f833ec8b5562","value":{"error":"unable?to?capture?screen","message":"Failed?to?capture?a?screenshot.?Does?the?current?view?have?'secure'?flag?set?","stacktrace":"io.appium.uiautomator2.common.exceptions.TakeScreenshotException:?Failed?to?capture?a?screenshot.?Does?the?current?view?have?'secure'?flag?set?\n\tat?io.appium.uiautomator2.utils.ScreenshotHelper.takeDeviceScreenshot(ScreenshotHelper.java:131)\n\tat?io.appium.uiautomator2.utils.ScreenshotHelper.takeScreenshot(ScreenshotHelper.java:61)\n\tat?io.appium.uiautomator2.utils.ScreenshotHelper.takeScreenshot(ScreenshotHelper.java:79)\n\tat?io.appium.uiautomator2.handler.CaptureScreenshot.safeHandle(CaptureScreenshot.java:32)\n\tat?io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:41)\n\tat?io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:262)\n\tat?io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:256)\n\tat?io.appium.uia... [W3C]?Matched?W3C?error?code?'unable?to?capture?screen'?to?UnableToCaptureScreen [MJSONWP?(669c743a)]?Encountered?internal?error?running?command:?io.appium.uiautomator2.common.exceptions.TakeScreenshotException:?Failed?to?capture?a?screenshot.?Does?the?current?view?have?'secure'?flag?set? [MJSONWP?(669c743a)]???at?io.appium.uiautomator2.utils.ScreenshotHelper.takeDeviceScreenshot(ScreenshotHelper.java:131) [MJSONWP?(669c743a)]???at?io.appium.uiautomator2.utils.ScreenshotHelper.takeScreenshot(ScreenshotHelper.java:61) [MJSONWP?(669c743a)]???at?io.appium.uiautomator2.utils.ScreenshotHelper.takeScreenshot(ScreenshotHelper.java:79) [MJSONWP?(669c743a)]???at?io.appium.uiautomator2.handler.CaptureScreenshot.safeHandle(CaptureScreenshot.java:32) [MJSONWP?(669c743a)]???at?io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:41) [MJSONWP?(669c743a)]???at?io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:262) [MJSONWP?(669c743a)]???at?io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:256) [MJSONWP?(669c743a)]???at?io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) [MJSONWP?(669c743a)]???at?io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) [MJSONWP?(669c743a)]???at?io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:435) [MJSONWP?(669c743a)]???at?io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) [MJSONWP?(669c743a)]???at?io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) [MJSONWP?(669c743a)]???at?io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:250) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) [MJSONWP?(669c743a)]???at?io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) [MJSONWP?(669c743a)]???at?io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1294) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) [MJSONWP?(669c743a)]???at?io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) [MJSONWP?(669c743a)]???at?io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911) [MJSONWP?(669c743a)]???at?io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) [MJSONWP?(669c743a)]???at?io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:611) [MJSONWP?(669c743a)]???at?io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:552) [MJSONWP?(669c743a)]???at?io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:466) [MJSONWP?(669c743a)]???at?io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) [MJSONWP?(669c743a)]???at?io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140) [MJSONWP?(669c743a)]???at?io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) [MJSONWP?(669c743a)]???at?java.lang.Thread.run(Thread.java:923) [MJSONWP?(669c743a)]? [HTTP]?<--?GET?/wd/hub/session/669c743a-cede-43f2-8950-019af660f2f6/screenshot?500?270?ms?-?164 [HTTP]FLAG_SECURE
APP出于安全考慮,不希望被用戶截屏或者錄屏,所以使用FLAG_SECURE來進(jìn)行處理
禁止截屏的實(shí)現(xiàn)方式并不是很難,在需要設(shè)置禁止截屏的 Activity 的生命周期 onCreate() 方法中添加一行代碼即可
void?onCreate()?{//禁止截屏//?方法?1getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);//?方法?2getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,?WindowManager.LayoutParams.FLAG_SECURE); }設(shè)置了這個(gè)flag后, 系統(tǒng)會把當(dāng)前窗口的內(nèi)容視為安全隱私內(nèi)容, 系統(tǒng)會阻止這些內(nèi)容被截屏或者在不安全可靠的場景顯示出來.
在Google的過程中, 發(fā)現(xiàn)這個(gè)flag可以實(shí)現(xiàn)以下功能:
阻止屏幕截圖
在Recent apps(任務(wù)切換界面)中只顯示應(yīng)用名字和圖標(biāo), 不顯示內(nèi)容
Google App的Now on tap功能不會去分析你的頁面的內(nèi)容
代碼里面可以通過去除來去除這個(gè)防錄屏
Window?window?=?getActivity().getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);不過在進(jìn)行自動化測試的時(shí)候就不能這樣操作了,但其實(shí)這個(gè)只會影響錄屏,而不影響操作,所以我們直接拿到頁面元素,就可以通過元素標(biāo)簽,來編寫定位信息來操作元素了。
driver?=?app_webdriver.Remote("http://127.0.0.1:4723/wd/hub",?desired_caps) driver.page_source通過這個(gè)方式就可以拿到賬號密碼的元素定位
<android.widget.EditText?index="0"?package="com.greenline.yihuantong"class="android.widget.EditText"?text="登錄名/手機(jī)號"resource-id="com.greenline.yihuantong:id/bc7"checkable="false"?checked="false"?clickable="true"enabled="true"?focusable="true"?focused="true"long-clickable="true"?password="false"scrollable="false"?selected="false"bounds="[66,658][1014,783]"?displayed="true"/> <android.widget.EditText?index="2"?package="com.greenline.yihuantong"class="android.widget.EditText"?text="密碼"resource-id="com.greenline.yihuantong:id/bca"checkable="false"?checked="false"?clickable="true"enabled="true"?focusable="true"?focused="false"long-clickable="true"?password="true"scrollable="false"?selected="false"bounds="[66,784][1014,909]"?displayed="true"/>提取一下
-?{?desc:?"賬號",type:?"xpath",value:?'//*[@class="android.widget.EditText"][1]',?name:?"username"?} -?{?desc:?"密碼",type:?"xpath",value:?'//*[@class="android.widget.EditText"][2]',?name:?"password"?}總結(jié)
以上是生活随笔為你收集整理的FLAG_SECURE:安卓无法抓取页面截屏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NC65添加自定义按钮
- 下一篇: win10进不去计算机配置,win10系