定向班第一期_Appium iOS 测试技术答疑_20190119

入门

GenerateDSYMFile /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app.dSYM /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app/UICatalog (in target: UICatalog)
    cd /Users/seveniruby/projects/ios-uicatalog
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/dsymutil /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app/UICatalog -o /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app.dSYM

CodeSign /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app (in target: UICatalog)
    cd /Users/seveniruby/projects/ios-uicatalog
    export CODESIGN_ALLOCATE=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate
    
Signing Identity:     "iPhone Developer: yansheng huang (LJGDR784H2)"
Provisioning Profile: "iOS Team Provisioning Profile: *"
                      (0c3891ec-b6a3-4bd8-bb3c-1774087952ff)

    /usr/bin/codesign --force --sign A747E05F425FF31AA6180C146F9BE554AA1983B6 --entitlements /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Intermediates.noindex/UICatalog.build/Debug-iphoneos/UICatalog.build/UICatalog.app.xcent --timestamp=none /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app

iOS基础知识

  • plist app信息、启动信息
  • 可加载的二进制文件
  • embeded mobile provision app认证与安装信息
  • 签名数据

XCode基础知识

  • developer id
  • simulator
  • command tools
  • build
  • sign

苹果开发者中心

Appium on iOS

启动log

[HTTP] --> POST /wd/hub/session
[HTTP] {"desiredCapabilities":{"app":"/Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app","deviceName":"iPhone X","platformName":"ios","platformVersion":"12.1","newCommandTimeout":0,"connectHardwareKeyboard":true}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{"app":"/Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app","deviceName":"iPhone X","platformName":"ios","platformVersion":"12.1","newCommandTimeout":0,"connectHardwareKeyboard":true},null,null]
[debug] [BaseDriver] Event 'newSessionRequested' logged at 1547903063976 (21:04:23 GMT+0800 (CST))
[Appium] Requested iOS support with version >= 10, using 'XCUITest' driver instead of UIAutomation-based driver, since the latter is unsupported on iOS 10 and up.
[Appium] Creating new XCUITestDriver (v2.104.0) session
[Appium] Capabilities:
[Appium]   app: /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app
[Appium]   deviceName: iPhone X
[Appium]   platformName: ios
[Appium]   platformVersion: 12.1
[Appium]   newCommandTimeout: 0
[Appium]   connectHardwareKeyboard: true
[debug] [BaseDriver] Creating session with MJSONWP desired capabilities: {"app":"/Users/seveniruby/L...
[BaseDriver] Session created with session id: 58d6a0c6-0360-48d2-8197-dc465bf64670
[HTTP] --> GET /wd/hub/sessions
[HTTP] {}
[debug] [GENERIC] Calling AppiumDriver.getSessions() with args: []
[debug] [GENERIC] Responding to client with driver.getSessions() result: []
[HTTP] <-- GET /wd/hub/sessions 200 5 ms - 40
[HTTP]
[debug] [XCUITest] Current user: 'seveniruby'
[iOSSim] Constructing iOS simulator for Xcode version 10.1 with udid '6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9'
[XCUITest] Determining device to run tests on: udid: '6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9', real device: false
[debug] [XCUITest] Xcode version set to '10.1'
[debug] [XCUITest] iOS SDK Version set to '12.1'
[debug] [BaseDriver] Event 'xcodeDetailsRetrieved' logged at 1547903084446 (21:04:44 GMT+0800 (CST))
[BaseDriver] Using local app '/Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app'
[debug] [BaseDriver] Event 'appConfigured' logged at 1547903084456 (21:04:44 GMT+0800 (CST))
[debug] [XCUITest] Checking whether app '/Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app' is actually present on file system
[debug] [XCUITest] App is present
[debug] [iOS] Getting bundle ID from app '/Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app': 'com.example.apple-samplecode.UICatalog'
[debug] [BaseDriver] Event 'resetStarted' logged at 1547903084462 (21:04:44 GMT+0800 (CST))
[XCUITest] Not scrubbing third party app in anticipation of uninstall
[debug] [BaseDriver] Event 'resetComplete' logged at 1547903085712 (21:04:45 GMT+0800 (CST))
[XCUITest] Continuing without capturing device logs: iOS Simulator with udid 6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9 is not running
[XCUITest] Setting up simulator
[debug] [iOS] No reason to set locale
[debug] [iOS] No iOS / app preferences to set
[debug] [iOSSim] Setting preferences of 6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9 Simulator to {"ConnectHardwareKeyboard":true}
[debug] [iOSSim] Setting common Simulator preferences to {"RotateWindowWhenSignaledByGuest":true,"ConnectHardwareKeyboard":true}
[debug] [iOSSim] Updated 6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9 Simulator preferences at '/Users/seveniruby/Library/Preferences/com.apple.iphonesimulator.plist' with {"DevicePreferences":{"6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9":{"ConnectHardwareKeyboard":true}},"RotateWindowWhenSignaledByGuest":true,"ConnectHardwareKeyboard":true}
[iOSSim] Booting Simulator with UDID 6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9...
[iOSSim] Starting Simulator UI with command: open -Fn /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app --args -CurrentDeviceUDID 6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9 -ConnectHardwareKeyboard 1

常见错误

2019-01-19 13:05:59:867 [XCUITest] Starting log capture for iOS Simulator with u
did '6F79DA37-DE1E-434D-A9DC-239C4B2DC8E9', using 'xcrun simctl spawn 6F79DA37-D
E1E-434D-A9DC-239C4B2DC8E9 log stream --style compact'
2019-01-19 13:06:00:814 [BaseDriver] Event 'logCaptureStarted' logged at 1547903
160814 (21:06:00 GMT+0800 (CST))
2019-01-19 13:06:00:815 [XCUITest] Verifying application platform
2019-01-19 13:06:00:818 [XCUITest] CFBundleSupportedPlatforms: ["iPhoneOS"]
2019-01-19 13:06:00:818 [XCUITest] *********************************
2019-01-19 13:06:00:818 [XCUITest] Simulator architecture appears to be unsuppor
ted by the '/Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyz
dbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app' applicatio
n. Make sure the correct deployment target has been selected for its compilation
 in Xcode.
2019-01-19 13:06:00:819 [XCUITest] Don't be surprised if the application fails t
o launch.
2019-01-19 13:06:00:819 [XCUITest] *********************************

面向模拟器构建

CodeSign /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphonesimulator/UICatalog.app (in target: UICatalog)
    cd /Users/seveniruby/projects/ios-uicatalog
    export CODESIGN_ALLOCATE=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate
    
Signing Identity:     "-"

    /usr/bin/codesign --force --sign - --entitlements /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Intermediates.noindex/UICatalog.build/Debug-iphonesimulator/UICatalog.build/UICatalog.app.xcent --timestamp=none /Users/seveniruby/Library/Developer/Xcode/DerivedData/UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphonesimulator/UICatalog.app

WebDriverAgent

2019-01-19 13:22:28:536 [XCUITest] Using WDA path: '/usr/local/lib/node_modules/appium/node_modules/_appium-xcuitest-driver@2.104.0@appium-xcuitest-driver/WebDriverAgent'
2019-01-19 13:22:28:536 [XCUITest] Using WDA agent: '/usr/local/lib/node_modules/appium/node_modules/_appium-xcuitest-driver@2.104.0@appium-xcuitest-driver/WebDriverAgent/WebDriverAgent.xcodeproj'

构建WebDriverAgent的流程

git clone https://github.com/facebook/WebDriverAgent.git
cd WebDriverAgent
bash -x ./Scripts/bootstrap.sh
xcodebuild -project WebDriverAgent.xcodeproj \\
           -scheme WebDriverAgentRunner \\
           -destination 'platform=iOS Simulator,name=iPhone 6' \\
           test

真机运行

    @Test
    public void UICatalogTestClickRealDevice() throws MalformedURLException, InterruptedException {
        DesiredCapabilities caps=new DesiredCapabilities();
        caps.setCapability("platformName", "ios");
        caps.setCapability("automationName", "xcuitest");
        //caps.setCapability("platformVersion", "11.2");
        caps.setCapability("deviceName", "汪水华的 iPad (2)");
        caps.setCapability("udid", "auto");

/*        caps.setCapability("app", "/Users/seveniruby/Library/Developer/Xcode/DerivedData/" +
                "UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphonesimulator/UICatalog.app");*/

        caps.setCapability("app", "/Users/seveniruby/Library/Developer/Xcode/DerivedData/" +
                "UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app");

        caps.setCapability("xcodeOrgId", "96NJEQL7Y2");
        caps.setCapability("xcodeSigningId", "iPhone Developer");

        AppiumDriver driver=new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println(driver.getPageSource());
        driver.findElementByAccessibilityId("Buttons").click();
        Thread.sleep(1000000);
    }

WebView测试

webview测试

    @Test
    public void UICatalogWebviewRealDevice() throws IOException {
        DesiredCapabilities caps=new DesiredCapabilities();
        caps.setCapability("platformName", "ios");
        caps.setCapability("automationName", "xcuitest");
        caps.setCapability("deviceName", "汪水华的 iPad (2)");
        caps.setCapability("udid", "auto");

        //webview或者web的配置
        caps.setCapability("startIWDP", "true");

        //真机运行编译依赖的配置
        caps.setCapability("app", "/Users/seveniruby/Library/Developer/Xcode/DerivedData/" +
                "UICatalog-ftyzdbgapjmxxobezrnrxsshpdqh/Build/Products/Debug-iphoneos/UICatalog.app");
        caps.setCapability("xcodeOrgId", "96NJEQL7Y2");
        caps.setCapability("xcodeSigningId", "iPhone Developer");

        AppiumDriver driver=new AppiumDriver(new URL("http://127.0.0.1:5723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println(driver.getPageSource());
        System.out.println("UICatalog click");
        System.out.println(driver.findElementsByAccessibilityId("UICatalog").size());
        driver.findElementByXPath("//*[contains(@type, 'Button') and @name='UICatalog']").click();
        //点击Web View
        driver.findElementByAccessibilityId("Web View").click();
        //打印原生page source
        System.out.println(driver.getPageSource());
        //切换到最后一个context,一般是webview
        driver.context(driver.getContextHandles().toArray()[1].toString());
        //打印webview的page source
        System.out.println(driver.getPageSource());
        //用css定位找元素点击
        driver.findElementByCssSelector(".cta").click();
        FileUtils.copyFile(
                driver.getScreenshotAs(OutputType.FILE).getCanonicalFile(),
                new File("webview.png"));
    }

真机上运行浏览器测试


    @Test
    public void browserRealDevice() throws IOException {
        DesiredCapabilities caps=new DesiredCapabilities();
        caps.setCapability("platformName", "ios");
        caps.setCapability("automationName", "xcuitest");
        //caps.setCapability("platformVersion", "11.2");
        caps.setCapability("deviceName", "xxx iPad (2)");
        caps.setCapability("udid", "auto");
        caps.setCapability("browserName", "Safari");
        caps.setCapability("startIWDP", "true");

        //caps.setCapability("xcodeOrgId", "96NJEQL7Y2");
        //caps.setCapability("xcodeSigningId", "iPhone Developer");

        AppiumDriver driver=new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.baidu.com");
        System.out.println(driver.getPageSource());
        driver.findElementByName("wd").sendKeys("testerhome 霍格沃兹测试学院");
        FileUtils.copyFile(
                driver.getScreenshotAs(OutputType.FILE).getCanonicalFile(),
                new File("ios.png"));
    }
关闭