appium 5-27屏幕旋转、

1.屏幕切换

注意:应用一定要支持横竖屏切换,否则无效果,

  public void testBrowser() throws InterruptedException {
        Thread.sleep(10000);
        System.out.println("竖屏");
        driver.rotate(ScreenOrientation.PORTRAIT);
        Thread.sleep(3000);
        System.out.println("横屏");
        driver.rotate(ScreenOrientation.LANDSCAPE);
    }

2.应用切换到后台

   public void testHoutai(){
        System.out.println("切换后台");
        driver.runAppInBackground(Duration.ofSeconds(3));
        System.out.println("返回前台");
        driver.launchApp();
    }

3.手机解锁

注意:手机存在解锁密码,会解锁失败。如果存在密码,可以点击电源键,点亮屏幕后再解锁

    public void testLock(){
        driver.lockDevice();
        driver.unlockDevice();
    }

4.来电和短信,仅限模拟器

    public void SMS(){
        //接收短信
        driver.sendSMS("13854748784","hello world");
        //来电
        driver.makeGsmCall("13311112222",GsmCallActions.CALL);
    }

5.关闭应用,启动应用

   public void testStop() throws InterruptedException{
        Thread.sleep(5000);
        driver.closeApp();
        System.out.println("closeapp");
        Thread.sleep(5000);
        driver.launchApp();
        System.out.println("launchapp");
        Thread.sleep(6000);
    }

6.开启\关闭飞行模式

    public void testFlyMode() throws InterruptedException {
        driver.toggleAirplaneMode();
        Thread.sleep(6000);
    }

7.截屏

public void screenshot() throws IOException {
        FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE),(new File("1.png")));
    }

8.执行脚本

查看示例
更多
还是不会用,提示org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Appium server must have relaxed security flag set in order to run any shell commands

9.滑动

  public void testAction() throws InterruptedException{
        Thread.sleep(6000);
        TouchAction action = new TouchAction(driver);
        action.press(PointOption.point(500,1500)).
                waitAction(WaitOptions.waitOptions(Duration.ofSeconds(3))).
                moveTo(PointOption.point(500,500)).release().perform();
    }

SwipeClass.java封装滑动方法

public class SwipeClass {
    static Duration duration=Duration.ofSeconds(1);
    public void swipeToUp(AndroidDriver driver){
        int width  = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction act = new TouchAction(driver).press(PointOption.point(width/2,height*3/4)).waitAction(WaitOptions.waitOptions(duration)).moveTo(PointOption.point(width/2,height/4)).release();
        act.perform();
    }
    public void swipeToDown(AndroidDriver driver){
        int width  = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction act = new TouchAction(driver).press(PointOption.point(width/2,height*1/4)).waitAction(WaitOptions.waitOptions(duration)).moveTo(PointOption.point(width/2,height*3/4)).release();
        act.perform();
    }
    public void swipeToLeft(AndroidDriver driver){
        int width  = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction act = new TouchAction(driver).press(PointOption.point(width*4/5,height/2)).waitAction(WaitOptions.waitOptions(duration)).moveTo(PointOption.point(width/5,height/2)).release();
        act.perform();
    }
    public void swipeToRight(AndroidDriver driver){
        int width  = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction act = new TouchAction(driver).press(PointOption.point((int)width/5,(int)height/2)).waitAction(WaitOptions.waitOptions(duration)).moveTo(PointOption.point(width*4/5,height/2)).release();
        act.perform();
    }
}

测试代码继承滑动类,使用方法

public class TestBrowser extends SwipeClass{
    public void testSwipe() throws InterruptedException{
        Thread.sleep(6000);
        swipeToUp(driver);
    }
}

10.页面加载中,控件的位置会发生变化

public void testPositon() {
        driver.findElementByXPath("//*[@text='跳过']").click();
        for(int i=0;i<3;i++) {
            MobileElement zixuan = (MobileElement) driver.findElementByXPath("//*[@text=\"自选\"]");
            System.out.println(zixuan.getCenter());
        }
    }
posted on 2018-10-22 14:20  singleSpace  阅读(292)  评论(0编辑  收藏  举报