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();

}

catch (java.io.UnsupportedEncodingException 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:

/***

* 切换WEB页面查找元素

*/

public static void switchtoWeb(){

try {

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(){

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

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

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

}

/***

* 左滑1/2屏幕

*/

public static void slideLeft(){

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

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

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

}

/***

* 右滑1/2屏幕

*/

public static void slideRight(){

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

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

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);

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

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

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

}

/***

* 特殊下滑

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

*/

public static void slideDown(int i){

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

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

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

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);

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

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

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

}

/***

* 特殊右滑

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

*/

public static void slideRight(int i){

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

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

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

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 view的类型

* @param text的内容

* @return

*/

public static WebElement getViewbyXathwithtext(String view,String name){

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

}

/***

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

*/

public static String getScreen(){

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

String picname=finalElement.phoneScreens+df.format(new Date()).toString()+".png";

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

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

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){

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

String picname=finalElement.phoneScreens+name+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;

}

/***

* 检查网络

* @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名

* @return View

*/

public static WebElement getViewbyUitext(String name){

return demotestcase.driver.findElementByAndroidUIAutomator("new UiSelector().textContains(\""+name+"\")");

}

/***

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

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

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

*/

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

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

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

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

}

/***

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

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

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

*/

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

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

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

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

}

/***

* log记录

* @param 图片保存路径

* @param Exception参数

* @param AssertionError参数

* @param 测试用例

*/

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();

try {

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){}

try{

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

assertError.printStackTrace(pw);

} catch (Exception e){}

pw.flush();

pw.close();

file=new File(finalElement.errorlog);

if(!file.exists())

file.mkdirs();

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();

}

}


直接文本点击:

driver.findElementByAndroidUIAutomator("new UiSelector().textContains(\"发现\")").click();

driver.findElementByAndroidUIAutomator("new UiSelector().textContains(\"朋友圈\")").click();


访问远程共享文件夹bat

net use \\192.168.100.170 "5201314" /user:"office\yu.li"

copy \\192.168.100.170\Builds\FanliApp_Android\4.3\4.3.1.4\FanliAndroid-release-fanli.apk    D:\Apk\fanli.apk

原博:http://www.51testing.com/html/44/15020244-1435135.html

时间: 2024-12-19 14:36:45

Appium 常用操作的相关文章

APPIUM 常用API

(1)获取当前页面的activity名,比如: (.ui.login.ViewPage)  current_activity() 比如我们需要实现这个登录的功能时,主要思路为如果当前界面为登录页面时,就进行登录行为,否则就跳转到登录页面.其伪代码为: 1 if driver.current_activity == ".ui.login.ViewPage": 2 // To login_action 3 else: 4 // Trun to loginPage (2)获取当前页面的树形结

appium常用api

一.元素定位 需要注意的是每一种定位方式在界面上都可能存在多个属性值相同的元素 findElementById(String id) 通过元素的resource-id的值进行查找元素 AndroidElement ele=driver.findElementById(“com.zhihu.android:id/login_and_register”); findElementByName(String using 通过元素的text属性值或者content-desc属性值进行查找元素 Andro

Python 字典的特点和常用操作

一.字典帮助文档 >>> dir(dict) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt

postgresql的ALTER常用操作

postgresql版本:psql (9.3.4) 1.增加一列ALTER TABLE table_name ADD column_name datatype; 2.删除一列 ALTER TABLE table_name DROP column_name; 3.更改列的数据类型 ALTER TABLE table_name ALTER column_name TYPE datatype; 4.表的重命名 ALTER TABLE table_name RENAME TO new_name; 5.更

Mysql数据库常用操作

1.备份数据库 [[email protected] ~]# mysqldump -h 192.168.0.8 -uroot  -p'123456'  user >user.sql 2.查看mysql数据库字符集设置 mysql> show variables like 'character_set_%';+--------------------------+----------------------------+| Variable_name            | Value    

多路径软件常用操作(MPIO)

一:查看存储盘的路径 1. 查看MPIO的存储盘的路径 # lspath (适用于所有存储的MPIO路径查询) # mpio_get_config -Av (适用于DS3K/DS4K的MPIO路径查询) 2. 查看RDAC存储盘的路径 # fget_config -Av (适用于DS3K/DS4K的RDAC路径查询) 3.查看SDDPCM存储盘的路径 # pcmpath query device (适用于DS6K/DS8K和v7000的SDDPCM路径查询) 4. 查看当前操作系统自带的支持IB

Python学习笔记五:字符串常用操作,字典,三级菜单实例

字符串常用操作 7月19日,7月20日 ,7月22日,7月29日,8月29日,2月29日 首字母大写:a_str.capitalize() 统计字符串个数:a_str.count("x") 输出字符,不够的使用指定的字符补上,字符居中:a_str.center(50,"-") 判断字符串以什么结尾:a_str.endwith("xx") 将字符串中的tab转换为指定数目的空格:a_str.expandtabs(tabsize=30) 查找指定字符

jQuery的常用操作

梳理一下jQuery的常用操作 jQuery隐藏显示对象 id为test的元素的display修改成了"none",即隐藏了id为test的元素:$('#test').css('display','none') 或 $('#test').style.display="none" 我们经常用到的是切换一个元素的隐藏与现实,下面给出代码: var show = $('#test').css('display');//获取id为test的元素的display的值$('#t

MongoDB常用操作

1.MongoDB常用操作 1.1数据库的操作命令 1.创建数据库,使用命令 use 数据库名称 ,如 use sxf. *注意: 1.use 命令后跟的数据库名,如果存在就进入此数据库,如果不存在就创建,所以这种创建方式又叫隐式创建 2.使用命令use sxf创建数据库后,并没有真正生成对应的数据文件,如果此时退出,此数据库将被删除,只有在此数据库中创建集合后,才会真正生成数据文件 2. 删除当前数据库,使用命令 db.dropDatabase() 3.查看所有数据库,使用命令 show db