周末在家无聊研究了一个关于Appium 远程控制从机执行的方法,分享给大家
分享的主题:说白了就是你的机器的case交给别的机器帮你执行!
1,首先大家先看下面这段,大家应该都知道要能使Appium运行起来必须先配置好相关的参数;
这是我个人的代码,有点丑勿见怪
<span class="kd" style="color: rgb(170, 34, 255); font-weight: bold;">public</span> <span class="kt" style="color: rgb(0, 187, 0); font-weight: bold;">void</span> <span class="nf" style="color: rgb(0, 160, 0);">setUp</span><span class="p">(</span><span class="o" style="color: rgb(102, 102, 102);">)</span> <span class="kd" style="color: rgb(170, 34, 255); font-weight: bold;">throws</span> <span class="n">Exception</span><span class="o" style="color: rgb(102, 102, 102);">{</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">//启动</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// set up appium </span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// File classpathRoot = new File(System.getProperty("user.dir")); </span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// File appDir = new File(classpathRoot, "apps"); </span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// File app = new File(appDir, "ContactManager.apk"); </span>
<span class="n">DesiredCapabilities</span> <span class="n">capabilities</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="k" style="color: rgb(170, 34, 255); font-weight: bold;">new</span> <span class="n">DesiredCapabilities</span><span class="o" style="color: rgb(102, 102, 102);">();</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"device"</span><span class="o" style="color: rgb(102, 102, 102);">,</span><span class="s" style="color: rgb(187, 68, 68);">"Android"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// capabilities.setCapability(CapabilityType.BROWSER_NAME, "");</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"deviceName"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"xiaomi-mi_3-02214788"</span><span class="o" style="color: rgb(102, 102, 102);">);</span><span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">//小米</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// capabilities.setCapability("deviceName", "52c7c049");//三星</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// capabilities.setCapability("deviceName", "614ad249");//红米</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"platformVersion"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"4.4.4"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"platformName"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"Android"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// capabilities.setCapability("app", app.getAbsolutePath());</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"appPackage"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"com.tencent.mm"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"appActivity"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"com.tencent.mm.ui.LauncherUI"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="cm" style="color: rgb(0, 136, 0); font-style: italic;">/* capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True"); */</span>
<span class="n">driver</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="k" style="color: rgb(170, 34, 255); font-weight: bold;">new</span> <span class="n">AndroidDriver</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="k" style="color: rgb(170, 34, 255); font-weight: bold;">new</span> <span class="n">URL</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"http://127.0.0.1:4723/wd/hub"</span><span class="o" style="color: rgb(102, 102, 102);">),</span> <span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">//进入发现页面</span>
2,大家看完之后留意下这个地方:
<span class="n">driver</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="k" style="color: rgb(170, 34, 255); font-weight: bold;">new</span> <span class="n">AndroidDriver</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="k" style="color: rgb(170, 34, 255); font-weight: bold;">new</span> <span class="n">URL</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"http://127.0.0.1:4723/wd/hub"</span><span class="o" style="color: rgb(102, 102, 102);">),</span> <span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
这个里面这个url:http://127.0.0.1:4723/wd/hub 就是本机开辟的一个4723的端口去与Appium交互的,知道这个很重要,因为我们这次就会用到它
拿到这段url后我们去用浏览器打开看一下这个:
如果出现这个的话证明4723端口开辟成功是成功的,当然你的Appium server 肯定先开着
3,用另外一台机器改一下他的appium server Address :
运行成功后回到原来的主机 打开浏览器输入:之前设置的appium server Address,出现下面的
如果是这样的话,那证明你配置成功咯!!
4,接下来就是运行Case啦,到主机上面随便找一个脚本只需要更改一个地方
<span class="kd" style="color: rgb(170, 34, 255); font-weight: bold;">public</span> <span class="kt" style="color: rgb(0, 187, 0); font-weight: bold;">void</span> <span class="nf" style="color: rgb(0, 160, 0);">setUp</span><span class="p">(</span><span class="o" style="color: rgb(102, 102, 102);">)</span> <span class="kd" style="color: rgb(170, 34, 255); font-weight: bold;">throws</span> <span class="n">Exception</span><span class="o" style="color: rgb(102, 102, 102);">{</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">//启动</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// set up appium </span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// File classpathRoot = new File(System.getProperty("user.dir")); </span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// File appDir = new File(classpathRoot, "apps"); </span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// File app = new File(appDir, "ContactManager.apk"); </span>
<span class="n">DesiredCapabilities</span> <span class="n">capabilities</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="k" style="color: rgb(170, 34, 255); font-weight: bold;">new</span> <span class="n">DesiredCapabilities</span><span class="o" style="color: rgb(102, 102, 102);">();</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"device"</span><span class="o" style="color: rgb(102, 102, 102);">,</span><span class="s" style="color: rgb(187, 68, 68);">"Android"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// capabilities.setCapability(CapabilityType.BROWSER_NAME, "");</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"deviceName"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"xiaomi-mi_3-02214788"</span><span class="o" style="color: rgb(102, 102, 102);">);</span><span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">//小米</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// capabilities.setCapability("deviceName", "52c7c049");//三星</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// capabilities.setCapability("deviceName", "614ad249");//红米</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"platformVersion"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"4.4.4"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"platformName"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"Android"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">// capabilities.setCapability("app", app.getAbsolutePath());</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"appPackage"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"com.tencent.mm"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="na" style="color: rgb(187, 68, 68);">setCapability</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"appActivity"</span><span class="o" style="color: rgb(102, 102, 102);">,</span> <span class="s" style="color: rgb(187, 68, 68);">"com.tencent.mm.ui.LauncherUI"</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="cm" style="color: rgb(0, 136, 0); font-style: italic;">/* capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True"); */</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">//改这里</span>
<span class="n">driver</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="k" style="color: rgb(170, 34, 255); font-weight: bold;">new</span> <span class="n">AndroidDriver</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="k" style="color: rgb(170, 34, 255); font-weight: bold;">new</span> <span class="n">URL</span><span class="o" style="color: rgb(102, 102, 102);">(</span><span class="s" style="color: rgb(187, 68, 68);">"http://172.17.6.173:4723/wd/hub"</span><span class="o" style="color: rgb(102, 102, 102);">),</span> <span class="n">capabilities</span><span class="o" style="color: rgb(102, 102, 102);">);</span>
<span class="c1" style="color: rgb(0, 136, 0); font-style: italic;">//进入发现页面</span>
改好后各项参数都对应起来点击运行一个普通的case,你会发现你的另外一台机器上的设备自动运行起来了!!!!
你可以把这个url 封装起来然后开多个虚拟机和多版本的模拟器进行并发分布式测试!具体方法以后分享
祝大家成功!
有什么问题随便时找我!