1、代码
package com.crazylz.appnium;
import java.net.URL;
import java.time.Duration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
public class LaoyuegouloginTest {
private AppiumDriver driver;
@Before
public void setup() throws Exception {
// set the basic value,Initialize and start Appium Session
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.BROWSER_NAME, "");
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "1d9cabe");
cap.setCapability("platformVersion", "7.1.2");
cap.setCapability("appPackage", "com.laoyuegou.android");
cap.setCapability("appActivity", "com.laoyuegou.android.main.activity.ReSplashActivity");
//cap.setCapability("appWaitActivity", ".main.activity.ReSplashActivity");
cap.setCapability("sessionOverride", true);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}
@Test
public void move() {
try {
Thread.sleep(6000);
} catch (Exception e) {
e.printStackTrace();
}
int width=driver.manage().window().getSize().width; //获取屏幕最大宽度
int height=driver.manage().window().getSize().height; //获取屏幕最大高度
Duration duration = Duration.ofSeconds(1);
WebElement fisrtpage=driver.findElementById("com.laoyuegou.android:id/ie");
//或者
// driver.findElementById("com.laoyuegou.android:id/ie");
// for(int i = 0; i< 4; i++) {
// new TouchAction(driver).press(width-10,height/2).waitAction(duration).moveTo(width/4,height/2).release().perform();
// }
for(int i = 0; i< 4; i++) {
new TouchAction(driver).press(fisrtpage,width-10,height/2).waitAction(duration).moveTo(width/4,height/2).release().perform();
}
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
2、注意点,设置轮播图的时候一定要设置时间间隔,我一开始没设置就没有跑起来
TouchAction(driver).press(fisrtpage,width-10,height/2).waitAction(duration).moveTo(width/4,height/2).release().perform();
appium 5 之后swipe函数被删掉,不能使用,可以使用TouchAction