线下班第二期_Appium 脚本编写_20180304

有问题请回复此贴


    @Before
    public void beforeApi() throws MalformedURLException {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "iPhone 6");
        capabilities.setCapability("appPackage", "io.appium.android.apis");
        capabilities.setCapability("appActivity", ".ApiDemos");
        capabilities.setCapability("automationName", "uiautomator2");
        driver=new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }


    @Test
    public void testToast() throws InterruptedException {
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        WebDriverWait wait = new WebDriverWait(driver, 10);
        Dimension screenSize=driver.manage().window().getSize();

        driver.findElementByAccessibilityId("Views").click();
        Thread.sleep(2000);
        (new TouchAction(driver))
                .press( (int)(screenSize.width*0.5), (int)(screenSize.height*0.8))
                .moveTo((int)(screenSize.width*0.5), (int)(screenSize.height*0.3))
                .release()
                .perform();
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@content-desc='Popup Menu']"))).click();
        //driver.findElementByAccessibilityId("Popup Menu").click();
        driver.findElementByAccessibilityId("Make a Popup!").click();
        //locate("//*[contains(@text, 'MAKE')]").click();
        Thread.sleep(2000);
        driver.findElementByXPath("//*[contains(@text,'Search')]").click();

        String toastXPath="//*[@class='android.widget.Toast']";
        System.out.println(wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(toastXPath))));
        System.out.println(wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(toastXPath))).getText());
        System.out.println(driver.findElementByXPath(toastXPath).getText());
        //System.out.println(locate(toastXPath).getText());
        for(int i =0;i <10;i++){
            int size=driver.findElementsByXPath(toastXPath).size();
            System.out.println(size);
            Thread.sleep(500);
        }

    }


    @Test
    public void testWait(){
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElementByXPath("//*[@text='自选']").click();
    }
    @Test
    public void testWait2(){
        WebDriverWait wait=new WebDriverWait(driver, 10);
        for(int i=0;i<10;i++) {
            System.out.println(wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='自选']"))).getLocation());
        }
        driver.findElementByXPath("//*[@text='自选']").click();
    }



/*
        driver.performTouchAction(
                new TouchAction(driver)
                        .press((int)(width * 0.5), (int)(height * 0.5))
                        .waitAction(Duration.ofSeconds(1))
                        //.moveTo((screenWidth * (endX-startX)).toInt, (screenHeight * (endY-startY)).toInt)
                        .moveTo( (int)(width * 0.5), (int)(height * 0.9))
                        .release()
        );*/

        Thread.sleep(5000);

        (new TouchAction(driver))
                .press((int)(width * 0.5), (int)(height * 0.5))
                .waitAction(Duration.ofSeconds(1))
                .moveTo((int)(width*0.5), (int)(height*0.8))
                //.waitAction(Duration.ofSeconds(2))
                .release().perform();
        WebDriverWait wait=new WebDriverWait(driver, 10);
        System.out.println(wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[contains(@text, '更新')]"))).getText());

XPath

测试网址: Free Online XPath Tester / Evaluator - FreeFormatter.com

WebView

chromedriver说明: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md