问题已经解决,把代码和大家共享一下,希望可以帮助到其他人。
MoveScreenTest
import io.appium.java_client.android.AndroidDriver;
import org.openqa.jetty.jetty.servlet.SessionManager;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterSuite;
import java.io.File;
import java.net.URL;
import java.util.Set;
public class MoveScreenTest{
public static AndroidDriver driver;
@BeforeTest
public static void setUp() throws Exception {
//set up appium
File classpathRoot = new File(System.getProperty("user.dir"));
//获取用户当前的工作目录
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "QSHealthMD.apk");
//File app = new File("D:\\QSHealthMD.apk")
//或者可获取当前win系统中某路径下的apk
DesiredCapabilities capabilities = new DesiredCapabilities();
//告诉appium启动的是浏览器还是移动设备,设备的名称、类型、版本等
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
//定义移动浏览器的名称
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName","4d000e1544325027");
capabilities.setCapability("platformVersion", "5.0");
//capabilities.setCapability("app", app.getAbsolutePath());
//安装绝对路径下的app到移动设备中
capabilities.setCapability("appPackage", "com.qshealthcare.qshc");
capabilities.setCapability("appActivity", "com.qshealthcare.qshc.ui.activity.WelcomeActivity");
//获取app的首页,可通过appipum工具获取
//capabilities.setCapability("unicodeKeyboard","True");
//支持中文输入法
//capabilities.setCapability("restKeyboard","True")
//重置输入法为系统默认
//capabilities.setCapability("noSign","True")
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@AfterTest
public void tearDown() throws Exception{
System.out.println(driver);
//driver.quit();
Thread.sleep(3000);
}
@Test
public void lockscreen() throws InterruptedException {
//获取当前的混合应用类型,判断如果是webview就跳转进入webview
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW")){
//driver.context(contextName);
driver=(AndroidDriver) driver.context(contextName);
//AndroidDriver adriver =(AndroidDriver) driver.context("NATIVE_APP");
}else
{
System.out.println("no WEBVIEW");
}
}
Thread.sleep(20000);
int width=driver.manage().window().getSize().width;
int height=driver.manage().window().getSize().height;
//指定一个滑动的x和y坐标
driver.swipe(width*4/5, height/2, width/5, height/2, 1000);
Thread.sleep(2000);
WebElement loginBtn=driver.findElementByName("社区");
loginBtn.click();
System.out.println("跳转界面");
Thread.sleep(5000);
}
}
SkipTest
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
public class SkipTest{
@BeforeTest
public void setUp() throws Exception {
}
@AfterTest
public void tearDown() throws Exception{
MoveScreenTest.driver.quit();
}
@Test
public void swipe() throws InterruptedException {
Set<String> contextNames = MoveScreenTest.driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW")){
MoveScreenTest.driver=(AndroidDriver) MoveScreenTest.driver.context(contextName);
}else
{
System.out.println("no WEBVIEW");
}
}
Thread.sleep(3000);
WebElement loginBtn=MoveScreenTest.driver.findElementByName("首页");
loginBtn.click();
System.out.println("跳转界面");
Thread.sleep(3000);
for(int i=0;i<10;i++)
{
String demo="向红丁教授的“三五”防糖法";
System.out.println(i);
Thread.sleep(3000);
WebElement result=MoveScreenTest.driver.findElementByXPath("//android.widget.ListView/android.widget.RelativeLayout[contains(@index,3)]/android.widget.RelativeLayout[contains(@index,1)]/android.widget.RelativeLayout[contains(@index,1)]/android.widget.TextView[contains(@index,0)]");
String resulttext=result.getAttribute("text");
System.out.println( resulttext);
if(!demo.equals(resulttext)){
MoveScreenTest.driver.swipe(702,1136,702,1090,500);
System.out.println("没找到");
continue;
}
else{
System.out.println("找到了");
result.click();
break;
}
}
System.out.println("退出");
Thread.sleep(5000);
}
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test" verbose="10" >
<test name="testcase1" >
<classes>
<class name="MoveScreenTest"/>
<class name="SkipTest"/>
</classes>
</test>
</suite>
@monkey 猴哥,看看这个格式行吗?
这个问题我已经解决了,现在把实现的代码展现一下,希望能帮助到大家,如果大家有更好的方法希望不吝赐教!
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import java.util.Set;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class leizhong {
public AndroidDriver driver;
public leizhong(AndroidDriver driver){
this.driver=driver;
}
@Test
public void swpie() throws Exception{
//获取当前的混合应用类型,判断如果是 webview 跳转进入 webview
Set contextNames = driver.getContextHandles();
for(String contextName : contextNames){
System.out.println(contextName);
if(contextName.contains("WEBVIEW")){
driver=(AndroidDriver) driver.context(contextName);
}else{
System.out.println("no WEBVIEW");
}
}
Thread.sleep(2000);
driver=(AndroidDriver) driver.context("NATIVE_APP");
WebElement loginBtn=driver.findElementByName("社区");
loginBtn.click();
Thread.sleep(20000);
System.out.println("社区");
Thread.sleep(2000);
WebElement loginBtn1=driver.findElementByName("糖友锻炼");
loginBtn1.click();
System.out.println("糖友锻炼");;
Thread.sleep(2000);
try{
File file = new File("d:\test.xls");
InputStream in = new FileInputStream(file);
Workbook workbook = Workbook.getWorkbook(in);
Sheet sheet = workbook.getSheet(0);
int rowLength=sheet.getRows();
System.out.println(rowLength);
for(int i=1;i<rowLength;i++){
Thread.sleep(3000);
driver.findElementByXPath("//android.widget.ImageView[contains(@index,2)]").click();
Thread.sleep(2000);
WebElement title=driver.findElementByName("最多 30 个字");
Cell c10=sheet.getCell(0,i);
String strc10=c10.getContents();
System.out.println(strc10);
title.sendKeys(strc10);
Thread.sleep(20000);
Cell c21=sheet.getCell(1,i);
String strc21=c21.getContents();
System.out.println(strc21);
driver.findElementById("com.qshealthcare.qshc:id/activity_publish_new_main_title_content_et").sendKeys(strc21);
Thread.sleep(2000);
driver.findElementByName("发布").click();
Thread.sleep(1000);
}
}catch(Exception e){
System.out.println("failed!");
}
System.out.println("success");
}
}
之前看过一些 java 的资料,难道是通过 poi 的方式来读取数据吗,那样是不是太麻烦了?
不错,内容很丰富!!!!