言火 發表於 2019-8-21 14:01:00

Appium+python自动化(三十四)- 有图有真相,很美很精彩 - 屏幕截图和Android APP类型简介(超详解)

<h2>简介</h2>
<p class="p"><span style="font-family: 微软雅黑">在实际自动化项目运行过程中,很多时候</span>App可以会出现各种异常,为了更好的定位问题,除了捕捉日志我们还需要对运行时的设备状态来进行截屏。从而达到一种“<strong>有图有真相</strong>”的效果。</p>
<h2><strong>截图方法</strong></h2>
<h3><strong><span style="font-family: 微软雅黑">方法</span>1</strong></h3>
<p class="p">save_screenshot() 该方法直接保存当前屏幕截图到当前脚本所在文件位置。</p>
<p class="pre"><strong>driver</strong>.save_screenshot('login.png')</p>
<h4 class="pre">查看方法1源码:</h4>
<p class="pre"><img src="https://img2018.cnblogs.com/blog/1232840/201908/1232840-20190802114306941-732076664.png" alt=""></p>
<h3><strong><span style="font-family: 微软雅黑">方法</span>2</strong></h3>
<p class="p">get_screenshot_as_file(self, filename)</p>
<p class="p">将截图保留到指定文件路径</p>
<p class="pre"><strong>driver</strong>.get_screenshot_as_file('./images/login.png')</p>
<h4 class="pre">查看方法2源码:</h4>
<p class="pre"><img src="https://img2018.cnblogs.com/blog/1232840/201908/1232840-20190802114404007-1644775662.png" alt=""></p>
<h2><strong>实践案例</strong></h2>
<h3><strong>测试场景</strong></h3>
<p class="p"><span style="font-family: 微软雅黑">在考研帮</span>App登录页面输入用户名和密码之后截图,分别保存到当前文件和指定的文件路径。</p>
<h3><strong>代码实现</strong></h3>
<p><strong><img src="https://img2018.cnblogs.com/blog/1232840/201908/1232840-20190821135713474-968113699.png" alt=""></strong></p>
<h3><strong>参考代码</strong></h3>
<p class="p">&nbsp;</p>
<div class="cnblogs_code">
<pre># coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">
# </span><span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行

# </span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span>Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">8</span>-<span style="color: rgba(128, 0, 128, 1)">21</span><span style="color: rgba(0, 0, 0, 1)">
@author: 北京</span>-宏哥   QQ交流群:<span style="color: rgba(128, 0, 128, 1)">707699217</span><span style="color: rgba(0, 0, 0, 1)">
Project:学习和使用appium自动化测试</span>-<span style="color: rgba(0, 0, 0, 1)">屏幕截图
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span># <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
</span><span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)">appium import webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> selenium.common.exceptions import NoSuchElementException

desired_caps</span>=<span style="color: rgba(0, 0, 0, 1)">{}
desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">platformName</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Android</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">deviceName</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">127.0.0.1:62025</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">platforVersion</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">5.1.1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">automationName</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">uiautomator2</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">

desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">app</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C:\Users\DELL\Downloads\kaoyanbang.apk</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">appPackage</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">com.tal.kaoyan</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">appActivity</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">com.tal.kaoyan.ui.activity.SplashActivity</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">

desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">noReset</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">False</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">unicodeKeyboard</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">True</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
desired_caps[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">resetKeyboard</span><span style="color: rgba(128, 0, 0, 1)">'</span>]=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">True</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">

driver</span>=webdriver.Remote(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://localhost:4723/wd/hub</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,desired_caps)
driver.implicitly_wait(</span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">)

#定义的点击“取消”按钮方法
def check_cancelBtn():
    print(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">check cancelBtn</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
      cancelBtn </span>= driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">android:id/button2</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    except NoSuchElementException:
      print(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">no cancelBtn</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">:
      cancelBtn.click()

#定义的点击“跳过”按钮方法
def check_skipBtn():
    print(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">check skipBtn</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
      skipBtn </span>= driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">com.tal.kaoyan:id/tv_skip</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    except NoSuchElementException:
      print(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">no skipBtn</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">:
      skipBtn.click()

#调用点击“取消”按钮方法
check_cancelBtn()

#调用点击“跳过”按钮方法
check_skipBtn()

driver.find_element_by_id(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">com.tal.kaoyan:id/login_email_edittext</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).clear()

driver.find_element_by_id(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">com.tal.kaoyan:id/login_email_edittext</span><span style="color: rgba(128, 0, 0, 1)">'</span>).send_keys(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">55555</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

driver.find_element_by_id(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">com.tal.kaoyan:id/login_password_edittext</span><span style="color: rgba(128, 0, 0, 1)">'</span>).send_keys(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">zxw2018</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

driver.save_screenshot(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">login.png</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

driver.get_screenshot_as_file(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./images/login.png</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

driver.find_element_by_id(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">com.tal.kaoyan:id/login_login_btn</span><span style="color: rgba(128, 0, 0, 1)">'</span>).click()</pre>
</div>
<p>注意:如果指定路径出现中文字符时,需要加上u’文件路径’进行转码</p>
<h2><strong>App三种类型与区别</strong></h2>
<h3><strong><span style="font-family: 微软雅黑">原生应用程序:(</span>Native App)</strong></h3>
<p class="p"><span style="font-family: 微软雅黑">原生</span>APP是什么?原生APP就是利用Android、iOS平台官方的开发语言、开发类库、工具进行开发。比如安卓的java语言,iOS的object-c 语言。在应用性能上和交互体验上应该是最好的。</p>
<p class="p">优点:</p>
<p class="p">1、可访问手机所有功能、可实现功能最齐全;</p>
<p class="p">2、运行速度快、性能高,绝佳的用户体验;</p>
<p class="p">3、支持大量图形和动画。不卡,反应快。</p>
<p class="p">4、比较快捷地使用设备端提供的接口,处理速度上有优势。</p>
<p class="p">缺点:</p>
<p class="p"><span style="font-family: 微软雅黑">1、在过去主要是成本高、周期长,</span>Android和iOS都需要单独开发。</p>
<p>2、更新版本需要重新下载安装包。</p>
<h3><strong><span style="font-family: 微软雅黑">混合应用程序(</span>Hybrid App)</strong></h3>
<p class="p"><span style="font-family: 微软雅黑">即利用了原生</span>APP的开发技术还应用了HTML5开发技术,是原生和HTML5技术的混合应用。混合比例不限。</p>
<p class="p">优点:</p>
<p class="p">1、开发周期短;</p>
<p class="p">2、功能更新发布快;</p>
<p class="p">缺点:</p>
<p class="p">1、用户体验不如本地应用;</p>
<p class="p">2、性能稍慢(需要连接网络);</p>
<h3><strong>Web版APP (Web App)</strong></h3>
<p class="p"><span style="font-family: 微软雅黑">本质就是浏览器功能的叠加,用普通</span>Web开发语言开发的,通过浏览器运行。</p>
<p class="p">优势:</p>
<p class="p">1、支持范围广;</p>
<p class="p">2、开发成本低、周期短。</p>
<p class="p">缺点:</p>
<p class="p">1、对联网要求高,离线不能做任何操作;</p>
<p class="p">2、功能有限;</p>
<p class="p">3、运行速度慢,页面不能承载太多东西;</p>
<p class="p">4、图片和动画支持性不高;</p>
<p class="p">5、如果用户使用更多的新型浏览器,那么就会出现运行问题。</p>
<h3><strong>Web App、Hybrid App、Native App 技术特性</strong></h3>
<p><strong><img src="https://img2018.cnblogs.com/blog/1232840/201908/1232840-20190821134642447-1952881156.png" alt=""></strong></p>
<h3><strong><span style="font-family: 微软雅黑">手游</span>app与手机App</strong></h3>
<p class="p"><span style="font-family: 微软雅黑">手游一般使用引擎开发,现在著名的有</span>cocos2d和unity3d。两者都是使用引擎自带的语言进行开发,主流的分别是c++和c#,虽然在开发过程中也有按钮等控件的概念,但当运行时由引擎渲染后就变成了一副简单的图片:</p>
<p class="p"><span style="font-family: 微软雅黑">手机</span>App一般使用Android SDK开发,使用Java编写。通过Android提供的服务,我们可以获取App当前窗口的视图信息,进而查找和操作按钮等控件,以完成自动化测试,如Uiautomator。这个过程是标准化的,从技术上来说没有任何难度,因此各个公司各个App自动化测试的方法都大同小异。</p>
<h2>小结</h2>
<p>&nbsp;  好了,这部分的内容比较简单,小伙伴们和同学们掌握上边的两个截图的方法,了解一下Android APP的类型就可以。</p>
<p><span style="font-size: 18px"><strong><strong>您的肯定就是我进步的动力。</strong>如果你感觉还不错,就请鼓励一下吧!记得点波<span style="font-size: 18pt; color: rgba(255, 0, 0, 1)">&nbsp;推荐</span>&nbsp;哦!!!(点击右边的小球即可!<span style="color: rgba(255, 0, 0, 1)">(^__^)</span>&nbsp;嘻嘻……)</strong></span></p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201908/1232840-20190816135641371-1314831001.gif" alt=""></p>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp;个人公众号&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>微信群</strong>&nbsp;(微信群已满100,可以加宏哥的微信拉你进群,请备注:进群)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</strong></p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201906/1232840-20190613114641312-1547169156.png" alt="" width="158" height="158">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1232840/201908/1232840-20190805130945632-1533848172.png" alt="" width="182" height="182"></p>

</div>
<div id="MySignature" role="contentinfo">
    <div id="MySignature" style="display: block">
        <div style="font-size: 13px; border: 1px dashed rgb(45, 161, 45); padding: 10px 15px; background-color: rgb(248, 248, 248)">
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家在移动端也能看到我分享的博文,现已注册个人微信公众号,扫描左下方二维码即可,欢迎大家关注,提前解锁更多测试干货!有时间会及时分享相关技术博文。
                </label>
                <br>
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家互动讨论相关技术问题,刚刚建立了咱们的专门的微信群交流互动群,群内会分享交流测试领域前沿知识。请您扫描中间的微信二维码进群
                </label>
                <br>
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家互动讨论相关技术问题,现已组建专门的微信群,由于微信群满100,请您扫描右下方宏哥个人微信二维码拉你进群
                        <label style="font-weight: bold; color: red; font-size: 15px">
                                (请务必备注:已关注公众号进群)平时上班忙(和你一样),所以加好友不及时,请稍安勿躁~
                        </label>
                        ,欢迎大家加入这个大家庭,我们一起畅游知识的海洋。
                </label>
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;感谢您花时间阅读此篇文章,如果您觉得这篇文章你学到了东西也是为了犒劳下博主的码字不易不妨打赏一下吧,让博主能喝上一杯咖啡,在此谢过了!
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;如果您觉得阅读本文对您有帮助,请点一下左下角
               
                        “推荐”
               
                按钮,您的
                <label style="font-weight: bold; color: red; font-size: 15px">
                        “推荐”
                </label>
                将是我最大的写作动力!另外您也可以选择
               
                        【
                        <strong>
                                关注我
                        </strong>
                        】
               
                ,可以很方便找到我!
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;本文版权归作者和博客园共有,来源网址:
               
                        https://www.cnblogs.com/du-hong
               
                欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!
        </div>
        <div style="text-align: center; margin-top: 10px">
                <p style=" font-weight: bolder; color: red; ">
                        公众号(关注宏哥)&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace; &NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        微信群(扫码进群) &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;客服微信
                </p>
                <img style="width: 200px;padding-right: 50px;" alt="个人微信公众号" src="https://img2018.cnblogs.com/common/1741949/201911/1741949-20191119095948011-608816619.png">
                <img style="width: 200px;padding-right: 65px;" alt="微信群" src="https://img2024.cnblogs.com/blog/1232840/202506/1232840-20250610113707419-637869921.png">
                <img style="width: 200px" alt="个人微信" src="https://img2018.cnblogs.com/common/1741949/201911/1741949-20191106101257091-849954564.png">
        </div>
</div><br><br>
来源:https://www.cnblogs.com/du-hong/p/11286888.html
頁: [1]
查看完整版本: Appium+python自动化(三十四)- 有图有真相,很美很精彩 - 屏幕截图和Android APP类型简介(超详解)