天天看点

Appium日常笔记

   //启动

//   set up appium  

//   file classpathroot = new file(system.getproperty("user.dir"));  

//   file appdir = new file(classpathroot, "apps");  

//   file app = new file(appdir, "contactmanager.apk");  

   desiredcapabilities capabilities = new desiredcapabilities(); 

       capabilities.setcapability("device","android");

//       capabilities.setcapability(capabilitytype.browser_name, "");

       capabilities.setcapability("devicename", "xiaomi-mi_3-02214788");//小米

//       capabilities.setcapability("devicename", "52c7c049");//三星

//       capabilities.setcapability("devicename", "614ad249");//红米

       capabilities.setcapability("platformversion", "4.4.4");  

       capabilities.setcapability("platformname", "android");  

//       capabilities.setcapability("app", app.getabsolutepath());

       capabilities.setcapability("apppackage", "com.tencent.mm");  

       capabilities.setcapability("appactivity", "com.tencent.mm.ui.launcherui");  

       capabilities.setcapability("unicodekeyboard", "true");  

       capabilities.setcapability("resetkeyboard", "true");  

       driver = new androiddriver(new url("http://127.0.0.1:4723/wd/hub"), capabilities);

启动ios设备:

desiredcapabilities capabilities = new desiredcapabilities();

//capabilities.setcapability(capabilitytype.browser_name, "ios");

capabilities.setcapability(capabilitytype.version, "8.1");

capabilities.setcapability(capabilitytype.platform, "mac");

//capabilities.setcapability("device", "iphone simulator");

//capabilities.setcapability("app", "safai");

capabilities.setcapability("devicename", "pohoto");

     capabilities.setcapability("platformname", "ios");

driver = new remotewebdriver(new url("http://0.0.0.0:4723/wd/hub"), capabilities);

注:appium内还需要配置一些东西,udid/bundleid/force driver

寻找元素超时时间:

driver.manage().timeouts().implicitlywait(10,timeunit.seconds);

打印整个页面的元素:system.out.print(driver.getpagesource());

获取当前时间并截图,命名:

public static string getscreen(){

string fileroute="//liyu/testing/test/";

simpledateformat df = new simpledateformat("yyyy-mm-dd-hh-mm");

string picname=fileroute+df.format(new date()).tostring()+".png";

//picname=picname.replaceall(":", "-");

//picname=picname.replaceall(" ", "-");

       file screen = driver.getscreenshotas(outputtype.file);

       system.out.println(picname);

       file screenfile = new file(picname);

       try {

           fileutils.copyfile(screen, screenfile); 

           string time=df.format(new date()).tostring();

           system.out.println("当前时间"+time);

           return time;

       } catch (ioexception e) {

           e.printstacktrace();

       }

return null;

}

发送邮件:html/txt(要把图片放到iis路径拼接url)

public void mail() {

string value2="微信**系统: 本次**时间:"+getscreen();

string value="<img src=\"http://172.17.6.134:88/test/"+getscreen()+".png\">";

system.out.print(value);

string smtphost ="172.17.1.23";

string from = "[email protected]";

string to = "[email protected]";

string subject = value2; //subject javamail自动转码

stringbuffer themessage = new stringbuffer();

themessage.append("<h2><font color=red>**截图乳腺:</font></h2>");

themessage.append("<hr>");

themessage.append(value);

try {

mail.sendmessage(smtphost, from, to, subject, themessage.tostring());

catch (javax.mail.messagingexception exc) {

exc.printstacktrace();

public static void sendmessage(string smtphost,string from, string to, string subject, string messagetext)throws messagingexception,java.io.unsupportedencodingexception

{

// step  :  configure the mail session

system.out.println("configuring mail session for: " + smtphost);

java.util.properties props = new java.util.properties();

props.setproperty("mail.smtp.auth", "true");//指定是否需要smtp验证

props.setproperty("mail.smtp.host", smtphost);//指定smtp服务器

props.put("mail.transport.protocol", "smtp");

session mailsession = session.getdefaultinstance(props);

mailsession.setdebug(true);//是否在控制台显示debug信息

// step  :  construct the message

system.out.println("constructing message -  from=" + from + "  to=" + to);

internetaddress fromaddress = new internetaddress(from);

internetaddress toaddress = new internetaddress(to);

mimemessage testmessage = new mimemessage(mailsession);

testmessage.setfrom(fromaddress);

testmessage.addrecipient(javax.mail.message.recipienttype.to, toaddress);

testmessage.setsentdate(new java.util.date());

testmessage.setsubject(mimeutility.encodetext(subject,"gb2312","b"));

testmessage.setcontent(messagetext, "text/html;charset=gb2312");

system.out.println("message constructed");

// step  :  now send the message

transport transport = mailsession.gettransport("smtp");

transport.connect(smtphost, "[email protected]", "5201314");

transport.sendmessage(testmessage, testmessage.getallrecipients());

transport.close();

system.out.println("message sent!");

app内上滑:driver.swipe(250, 300, 250, 1400, 0); 

app内下滑:     driver.swipe(250,1400, 250,300 , 0); 

//driver.navigate().forward(); // 前进

//driver.navigate().back(); // 后退

driver.navigate().refresh(); // 刷新

string 字符串截取转成int:

string getscreen="2015-02-05-16-44";

string value="2015-02-05-16-49";

// int index= getscreen.substring(13, 1);

    string getscreen1 = getscreen.substring(14, 16);

    string  value2 = value.substring(14, 16);

system.out.print(getscreen1);

system.out.print(value2);

    integer.parseint(getscreen1);

    integer.parseint(value2);    

int sum =integer.parseint(value2) -integer.parseint(getscreen1); 

system.out.print(sum);

 动作action:

/***

*/

public static void switchtoweb(){

     set<string> contextnames = demotestcase.driver.getcontexthandles();

     for (string contextname : contextnames) {

       // 用于返回被测app是native_app还是webview,如果两者都有就是混合型app

       if(contextname.contains("webview")||contextname.contains("webview")){

       demotestcase.driver.context(contextname);

       system.out.println("跳转到web页 开始操作web页面"); 

     }

}catch (exception e) {

     e.printstacktrace();

* 上滑1/4屏幕

public static void slideup(){

int x=demotestcase.driver.manage().window().getsize().width;

int y=demotestcase.driver.manage().window().getsize().height;

demotestcase.driver.swipe(x/2, y/3*2, x/2, y/3*1, 0);

* 下滑1/4屏幕

public static void slidedown(){

demotestcase.driver.swipe(x/2, y/3*1, x/2, y/3*2, 0);

* 左滑1/2屏幕

public static void slideleft(){

demotestcase.driver.swipe(x/4*3, y/2, x/4*1, y/2, 0);

* 右滑1/2屏幕

public static void slideright(){

demotestcase.driver.swipe(x/4*1, y/2, x/4*3, y/2, 0);

* 特殊上滑

* @param 传入从左到右宽度的百分比(1-99之间)

public static void slideup(int i){

assert.assertfalse("上滑宽度传入错误", i<=0||i>=100);

demotestcase.driver.swipe(x/10*i, y/3*2, x/10*i, y/3*1, 0);

* 特殊下滑

public static void slidedown(int i){

assert.assertfalse("下滑宽度传入错误", i<=0||i>=100);

demotestcase.driver.swipe(x/10*i, y/3*1, x/10*i, y/3*2, 0);

* 特殊左滑

* @param 传入从上到下宽度的百分比(1-99之间)

public static void slideleft(int i){

assert.assertfalse("左滑宽度传入错误", i<=0||i>=100);

demotestcase.driver.swipe(x/4*3, y/10*i, x/4*2, y/10*i, 0);

* 特殊右滑

public static void slideright(int i){

demotestcase.driver.swipe(x/4*2, y/10*i, x/4*3, y/10*i, 0);

* xpath根据content-desc查找元素

* @param view的类型

* @param content-desc 的内容

* @return

public static webelement getviewbyxathwithcontentdesc(string view,string name){

return demotestcase.driver.findelementbyxpath("//"+view+"[contains(@content-desc,'"+name+"')]");

* xpath根据text查找元素

* @param text的内容

public static webelement getviewbyxathwithtext(string view,string name){

return demotestcase.driver.findelementbyxpath("//"+view+"[contains(@text,'"+name+"')]");

* 截图 文件名: 年月日时分秒

simpledateformat df = new simpledateformat("yyyy-mm-dd-hh-mm-ss");

string picname=finalelement.phonescreens+df.format(new date()).tostring()+".png";

        file screen = demotestcase.driver.getscreenshotas(outputtype.file);

        system.out.println(picname);

        file screenfile = new file(picname);

        try {

            fileutils.copyfile(screen, screenfile); 

        } catch (ioexception e) {

            e.printstacktrace();

        }

return picname;

* 截图 文件名: 内容-年月日时分秒

public static string getscreen(string name){

string picname=finalelement.phonescreens+name+df.format(new date()).tostring()+".png";

        return picname;

* 检查网络

* @return 是否正常

public static boolean checknet(){

string text=demotestcase.driver.getnetworkconnection().tostring();

if(text.contains("data: true"))

return true;

else

return false;

* 根据uiautomator底层方法得到对应desc的view

* @param desc名

* @return view

public static webelement getviewbyuidesc(string name){

return demotestcase.driver.findelementbyandroiduiautomator("new uiselector().descriptioncontains(\""+name+"\")");

* 根据uiautomator底层方法得到对应text的view

* @param text名

public static webelement getviewbyuitext(string name){

return demotestcase.driver.findelementbyandroiduiautomator("new uiselector().textcontains(\""+name+"\")");

* 绝对坐标 传入长宽的像素点

* @param 宽度从左到右的像素点

* @param 长度从上到下的像素点

public static void clickscreen(int i,int j){

demotestcase.driver.tap(1, i, j, 200);

* 相对坐标 传入长宽的百分比

* @param 宽度从左到右的百分比

* @param 长度从上到下的百分比

public static void clickscreen100(int i,int j){

demotestcase.driver.tap(1, x*i/100, y*j/100, 200);

* log记录

* @param 图片保存路径

* @param exception参数

* @param assertionerror参数

public static void getlog(string text, exception error, assertionerror asserterror, string testname){

simpledateformat df = new simpledateformat("mm-dd-hh-mm");

system.out.println("当前时间"+df.format(new date()));

string filename=finalelement.errorfile+testname+"-"+df.format(new date()).tostring()+".txt";

file file=new file(finalelement.errorfile);

if(!file.exists())

file.mkdirs();

   file f = new file(filename);

   if (!f.exists()) 

   f.createnewfile();

filewriter fw = new filewriter(f, true);

printwriter pw = new printwriter(fw);

pw.append(testname+" 测试failed\r\n");

pw.append("截图保存为:"+text+"\r\n");

try{

pw.append("eclipse报错为:\n"+error.tostring()+"\r\n");

error.printstacktrace(pw);

} catch (exception e){}

pw.append("断言报错为:"+asserterror.tostring()+"\r\n");

asserterror.printstacktrace(pw);

} catch (exception e){}  

pw.flush();

pw.close();

file=new file(finalelement.errorlog);

string cmd="cmd /c \"adb logcat -d  *:e *:s |grep \"com.yiguo.app\" >"+finalelement.errorlog+testname+"-"+df.format(new date()).tostring()+".txt\"";

//system.out.println(cmd);

runtime.getruntime().exec(cmd);

} catch (exception e) {

e.printstacktrace();

继续阅读