App自动化测试

录制

# This sample code uses the Appium python client v2
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

# For W3C actions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput

caps = {}
caps["platformName"] = "android"
caps["appium:appPackage"] = "com.google.android.deskclock"
caps["appium:appActivity"] = "com.android.deskclock.DeskClock"
caps["appium:ensureWebviewsHavePages"] = True
caps["appium:nativeWebScreenshot"] = True
caps["appium:newCommandTimeout"] = 3600
caps["appium:connectHardwareKeyboard"] = True

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)

el1 = driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="Cities")
el1.click()
el2 = driver.find_element(by=AppiumBy.ID, value="com.google.android.deskclock:id/search_src_text")
el2.send_keys("shanghai")
el3 = driver.find_element(by=AppiumBy.ID, value="com.google.android.deskclock:id/city_name")
el3.click()
el4 = driver.find_element(by=AppiumBy.ID, value="com.google.android.deskclock:id/city_name")
el4.click()

driver.quit()
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SampleTest {

  private AndroidDriver driver;

  @Before
  public void setUp() throws MalformedURLException {
    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability("platformName", "android");
    desiredCapabilities.setCapability("appium:appPackage", "com.google.android.deskclock");
    desiredCapabilities.setCapability("appium:appActivity", "com.android.deskclock.DeskClock");
    desiredCapabilities.setCapability("appium:ensureWebviewsHavePages", true);
    desiredCapabilities.setCapability("appium:nativeWebScreenshot", true);
    desiredCapabilities.setCapability("appium:newCommandTimeout", 3600);
    desiredCapabilities.setCapability("appium:connectHardwareKeyboard", true);

    URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");

    driver = new AndroidDriver(remoteUrl, desiredCapabilities);
  }

  @Test
  public void sampleTest() {
    MobileElement el1 = (MobileElement) driver.findElementByAccessibilityId("Cities");
    el1.click();
    MobileElement el2 = (MobileElement) driver.findElementById("com.google.android.deskclock:id/search_src_text");
    el2.sendKeys("shanghai");
    MobileElement el3 = (MobileElement) driver.findElementById("com.google.android.deskclock:id/city_name");
    el3.click();
    MobileElement el4 = (MobileElement) driver.findElementById("com.google.android.deskclock:id/city_name");
    el4.click();
  }

  @After
  public void tearDown() {
    driver.quit();
  }
}

appium inspect

{
  "platformName": "android",
  "appium:appPackage": "com.google.android.deskclock",
  "appium:appActivity": "com.android.deskclock.DeskClock"
}

远程模拟器

driver = webdriver.Remote("http://192.168.146.43:4723/wd/hub", caps)
driver.implicitly_wait(5)