java-appium,jxl调用Excel中的数据,输入到文本框中。

第一次写博客,请多多指教,使用appium调用Excel中的数据,输入到文本框中。

我分享的是我最近在研究的成果。

1、通过appium自动化添加好友。

首先需要做的是,搭建appium环境,这里我就不说了,百度有很多。

这里我使用的语言是java。

首先我们需要的是jar包,因为appium搭建是需要配合selenium使用。

读取Excel中的数据,需要用到jxl.jar包。

下面这是我的jar。

jxl.jar

testng-6.9.9.jar

selenium-java-3.3.1.zip

扫描二维码关注公众号,回复: 3409931 查看本文章

selenium-server-standalone-3.3.1.jar

java-client-54.1.2.jar

2、接下来就是手机连接电脑,启动appium。然后就是编写脚本,

下面是本人自己通过各种搜索到的,然后再自己编写的代码。

package weixing;




import org.openqa.selenium.By;


import org.openqa.selenium.NoSuchElementException;


import org.openqa.selenium.remote.DesiredCapabilities;


import io.appium.java_client.android.AndroidDriver;


import java.net.MalformedURLException;


import java.net.URL;


import java.io.File;  


import java.io.IOException;  


import jxl.Cell;  


import jxl.Sheet;  


import jxl.Workbook;  


import jxl.read.biff.BiffException;  


@SuppressWarnings("rawtypes")
public class tianJia {

       
static boolean isContentAppeared(AndroidDriver driver,String content) {  //通过xpath定位元素判断操作进行,因为微信有时会出现查找不到好友,还有就是已经添加过该好友的情况,所以添加用来做判断。
       
       boolean status=true;
       
   try {  
   
       driver.findElement(By.xpath(content)); 
       
       status = true;  
       }
   catch(NoSuchElementException e){  
   
       status = false;  
   }
return status;  
}  

public static void main(String[] args) throws MalformedURLException, InterruptedException,BiffException, IOException{
//*****************************************注意1.
        DesiredCapabilities capabilities = new DesiredCapabilities();  
        
        capabilities.setCapability("deviceName", "53751994");//
        
        capabilities.setCapability("automationName", "Appium");
        
        capabilities.setCapability("platformName", "Android");
        
        capabilities.setCapability("platformVersion", "6.0.1");
        
        capabilities.setCapability("appPackage", "com.tencent.mm");
        
        capabilities.setCapability("appActivity", "com.tencent.mm.ui.LauncherUI");
        
        capabilities.setCapability("noReset", "True");
        
        capabilities.setCapability("unicodeKeyboard", "True");//设置输入的编码
        
        capabilities.setCapability("resetKeyboard", "True");
        
        capabilities.setCapability("autoWebview", "True");    //支持自动切换webview
        
        capabilities.setCapability("recreateChromeDriverSessions", "True");
      
        AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

//从上面*****************************注意1,到这里,这是启动微信的关键参数。以下是操作步骤。
        
        Thread.sleep(4000);//设置等待时间
       
        driver.findElementById("com.tencent.mm:id/fr").click();//点击+
        
        Thread.sleep(1000);
        
        driver.findElementByName("添加朋友").click();//点击添加朋友
        
        Thread.sleep(1000);
        
        driver.findElementById("com.tencent.mm:id/h9").click();//点击输入框
        
        Thread.sleep(1000);
        
    Workbook book=Workbook.getWorkbook(new File("C:\\test.xls"));//调用Excel中的数据的路径,这里是Excel文件的路径,注,这里只能是以   .xls    结尾,也就是    右键单击>新建> 新建Microsoft Excel 97-2003 工作表.xls
     
    Sheet sheet=book.getSheet(0);//获得第一个工作表对象      getShoot(1)获取Excel文件转中的   第二个工作表。
   
    for(int a=0;a>=0;a++){//这里开时循环,自动化循环添加好友。
    //******************************************************************
    Cell cell1=sheet.getCell(0,a);//(0.0)得到第一列第一行的单元格   (0.1)得到第一列第二行的单元格    (1.0)得到第二列第一行的单元格
   
    String result=cell1.getContents();//     定义结果的数据类型
   
    System.out.println(result);//打印结果                 到这里,是获取Excel中的数据。
    //****************************************************************************
        driver.findElementById("com.tencent.mm:id/h9").sendKeys(result);//输入电话号码
        
        Thread.sleep(2000);
        
        driver.findElementById("com.tencent.mm:id/aqb").click();//点击搜索
        
        Thread.sleep(3000);
        
        if(isContentAppeared(driver,"//android.widget.Button[@resource-id=\"com.tencent.mm:id/ah1\"]" )==true){ //调用上面的的isContentAppeared(driver,“    xpath”),判断     添加到通讯录       元素是否存在
       
        driver.findElementById("com.tencent.mm:id/ah1").click();//点击添加到通讯录
       
        Thread.sleep(2000);
       
            driver.findElementById("com.tencent.mm:id/chg").clear();//清除备注名
            
            Thread.sleep(2000);
            //***********************************************再次获取表中的数据
            Cell beiZhu=sheet.getCell(1,a);//得到第二列
            
            Cell beiZhu1=sheet.getCell(2,a);//得到
            
        String result1=beiZhu.getContents();
       
        String result2=beiZhu1.getContents();
       
        System.out.print(result1+"("+result2+")");
       
        driver.findElementById("com.tencent.mm:id/chg").sendKeys(result1+"("+result2+")");//输入备注名
       
            Thread.sleep(2000);
            
            driver.findElementById("com.tencent.mm:id/gv").click();//点击发送
            
            Thread.sleep(1000);
            
            driver.findElementById("com.tencent.mm:id/hd").click();//点击返回
            
            Thread.sleep(1000);
            
            driver.findElementById("com.tencent.mm:id/h9").clear();//清空
            
            Thread.sleep(2000);
            
            System.out.print("成功发送验证信息!");
            
        }
        else if(isContentAppeared(driver,"//android.widget.Button[@resource-id=\"com.tencent.mm:id/ah2\"]")==true){
       
        driver.findElementById("com.tencent.mm:id/hd").click();//点击返回
       
        Thread.sleep(2000);
       
        driver.findElementById("com.tencent.mm:id/h9").clear();//清空
       
            Thread.sleep(2000);
            
            System.out.print("用户已经添加为好友!");
        }
        else{
       
        driver.findElementById("com.tencent.mm:id/h9").clear();//清空
       
        System.out.print("该用户不存在");
        }
        
        System.out.print("              又添加"+a+"行!");
        
    }
    
        driver.quit();
        
        System.out.println("添加完成");
}
}

3、然后运行一下,只要appium搭建和使用没问题,应该可以跑起来。


猜你喜欢

转载自blog.csdn.net/qq_39046854/article/details/79181462