Exemple #1
0
  def _SetPbxprojForXctest(self):
    """Sets the dummy project's pbxproj for xctest."""
    pbxproj_plist_obj = plist_util.Plist(self.pbxproj_file_path)
    pbxproj_objects = pbxproj_plist_obj.GetPlistField('objects')

    # Sets the build setting for app under test and unit test bundle signing.
    # 1) If run with iphonesimulator, don't need to set any fields in build
    # setting. xcodebuild will sign bundles with identity '-' and no
    # provisioning profile by default.
    # 2) If runs with iphoneos and the app under test's embedded provisioning
    # profile is 'iOS Team Provisioning Profile: *', set build setting for using
    # Xcode managed provisioning profile to sign bundles.
    # 3) If runs with iphoneos and the app under test's embedded provisioning
    # profile is specific, set build setting with using app under test's
    # embedded provisioning profile.
    if self._sdk == ios_constants.SDK.IPHONEOS:
      aut_build_setting = pbxproj_objects[
          'AppUnderTestBuildConfig']['buildSettings']
      test_build_setting = pbxproj_objects[
          'XCTestBundleBuildConfig']['buildSettings']
      aut_build_setting['CODE_SIGNING_REQUIRED'] = 'YES'
      aut_build_setting['PRODUCT_BUNDLE_IDENTIFIER'] = bundle_util.GetBundleId(
          self._app_under_test_dir)
      embedded_provision = provisioning_profile.ProvisiongProfile(
          os.path.join(self._app_under_test_dir, 'embedded.mobileprovision'),
          self._work_dir,
          keychain_path=self._keychain_path)
      embedded_provision.Install()
      # Case 2)
      if embedded_provision.name.startswith('iOS Team Provisioning Profile: '):
        aut_build_setting['CODE_SIGN_IDENTITY'] = 'iPhone Developer'
        test_build_setting['CODE_SIGN_IDENTITY'] = 'iPhone Developer'
        app_under_test_dev_team = bundle_util.GetDevelopmentTeam(
            self._app_under_test_dir)
        aut_build_setting['DEVELOPMENT_TEAM'] = app_under_test_dev_team
        test_build_setting['DEVELOPMENT_TEAM'] = app_under_test_dev_team
      else:
        # Case 3)
        app_under_test_sign_identity = bundle_util.GetCodesignIdentity(
            self._app_under_test_dir)
        aut_build_setting['CODE_SIGN_IDENTITY'] = app_under_test_sign_identity
        test_build_setting['CODE_SIGN_IDENTITY'] = app_under_test_sign_identity
        (aut_build_setting[
            'PROVISIONING_PROFILE_SPECIFIER']) = embedded_provision.name

    # Sets the app under test and test bundle.
    test_project_build_setting = pbxproj_objects[
        'TestProjectBuildConfig']['buildSettings']
    app_under_test_name = os.path.splitext(
        os.path.basename(self._app_under_test_dir))[0]
    pbxproj_objects['AppUnderTestTarget']['name'] = app_under_test_name
    pbxproj_objects['AppUnderTestTarget']['productName'] = app_under_test_name
    test_project_build_setting['APP_UNDER_TEST_NAME'] = app_under_test_name
    test_bundle_name = os.path.splitext(
        os.path.basename(self._test_bundle_dir))[0]
    pbxproj_objects['XCTestBundleTarget']['name'] = test_bundle_name
    pbxproj_objects['XCTestBundleTarget']['productName'] = test_bundle_name
    test_project_build_setting['XCTEST_BUNDLE_NAME'] = test_bundle_name

    pbxproj_plist_obj.SetPlistField('objects', pbxproj_objects)
Exemple #2
0
  def _SetPbxprojForXcuitest(self):
    """Sets the dummy project's pbxproj for xcuitest."""
    pbxproj_plist_obj = plist_util.Plist(self.pbxproj_file_path)
    pbxproj_objects = pbxproj_plist_obj.GetPlistField('objects')

    # Sets the build setting of test bundle for generated XCTRunner.app signing.
    # 1) If run with iphonesimulator, don't need to set any fields in build
    # setting. xcodebuild will sign the XCTRunner.app with identity '-' and no
    # provisioning profile by default.
    # 2) If runs with iphoneos and the app under test's embedded provisioning
    # profile is 'iOS Team Provisioning Profile: *', set build setting for using
    # Xcode managed provisioning profile to sign the XCTRunner.app.
    # 3) If runs with iphoneos and the app under test's embedded provisioning
    # profile is specific, set build setting for using app under test's
    # embedded provisioning profile to sign the XCTRunner.app. If the
    # provisioning profile is not installed in the Mac machine, also installs
    # it.
    # 4) The test bundle's provisioning profile can be overwrited by method
    # SetTestBundleProvisioningProfile.
    if self._sdk == ios_constants.SDK.IPHONEOS:
      build_setting = pbxproj_objects[
          'XCUITestBundleBuildConfig']['buildSettings']
      build_setting['PRODUCT_BUNDLE_IDENTIFIER'] = bundle_util.GetBundleId(
          self._test_bundle_dir)
      build_setting['DEVELOPMENT_TEAM'] = bundle_util.GetDevelopmentTeam(
          self._test_bundle_dir)
      embedded_provision = provisioning_profile.ProvisiongProfile(
          os.path.join(self._app_under_test_dir, 'embedded.mobileprovision'),
          self._work_dir,
          keychain_path=self._keychain_path)
      embedded_provision.Install()
      # Case 2)
      if embedded_provision.name.startswith('iOS Team Provisioning Profile: '):
        build_setting['CODE_SIGN_IDENTITY'] = 'iPhone Developer'
      else:
        # Case 3)
        build_setting['CODE_SIGN_IDENTITY'] = bundle_util.GetCodesignIdentity(
            self._app_under_test_dir)
        (build_setting[
            'PROVISIONING_PROFILE_SPECIFIER']) = embedded_provision.name

    # Sets the app under test and test bundle.
    test_project_build_setting = pbxproj_objects[
        'TestProjectBuildConfig']['buildSettings']
    app_under_test_name = os.path.splitext(
        os.path.basename(self._app_under_test_dir))[0]
    pbxproj_objects['AppUnderTestTarget']['name'] = app_under_test_name
    pbxproj_objects['AppUnderTestTarget']['productName'] = app_under_test_name
    test_project_build_setting['APP_UNDER_TEST_NAME'] = app_under_test_name
    test_bundle_name = os.path.splitext(
        os.path.basename(self._test_bundle_dir))[0]
    pbxproj_objects['XCUITestBundleTarget']['name'] = test_bundle_name
    pbxproj_objects['XCUITestBundleTarget']['productName'] = test_bundle_name
    test_project_build_setting['XCUITEST_BUNDLE_NAME'] = test_bundle_name

    pbxproj_plist_obj.SetPlistField('objects', pbxproj_objects)
Exemple #3
0
    def _GenerateTestRootForXcuitest(self):
        """Generates the test root for XCUITest.

    The approach constructs xctestrun.plist and uitest runner app from Xcode.
    Then copies app under test, test bundle, xctestrun.plist and uitest
    runner app to test root directory.
    """
        platform_library_path = os.path.join(
            xcode_info_util.GetSdkPlatformPath(self._sdk), 'Developer/Library')
        uitest_runner_app = self._GetUitestRunnerAppFromXcode(
            platform_library_path)

        runner_app_frameworks_dir = os.path.join(uitest_runner_app,
                                                 'Frameworks')
        os.mkdir(runner_app_frameworks_dir)
        xctest_framework = os.path.join(runner_app_frameworks_dir,
                                        'XCTest.framework')
        shutil.copytree(
            os.path.join(platform_library_path, 'Frameworks/XCTest.framework'),
            xctest_framework)
        if xcode_info_util.GetXcodeVersionNumber() >= 900:
            xct_automation_framework = os.path.join(
                runner_app_frameworks_dir, 'XCTAutomationSupport.framework')
            shutil.copytree(
                os.path.join(
                    platform_library_path,
                    'PrivateFrameworks/XCTAutomationSupport.framework'),
                xct_automation_framework)

        self._PrepareUitestInRunerApp(uitest_runner_app)

        if self._on_device:
            runner_app_embedded_provision = os.path.join(
                uitest_runner_app, 'embedded.mobileprovision')
            use_customized_provision = False
            if self._signing_options:
                customized_runner_app_provision = self._signing_options.get(
                    'xctrunner_app_provisioning_profile')
                if customized_runner_app_provision:
                    shutil.copyfile(customized_runner_app_provision,
                                    runner_app_embedded_provision)
                    use_customized_provision = True
                if self._signing_options.get(
                        'xctrunner_app_enable_ui_file_sharing'):
                    try:
                        # Don't resign the uitest runner app here since it will be resigned
                        # with passing entitlements and identity later.
                        bundle_util.EnableUIFileSharing(uitest_runner_app,
                                                        resigning=False)
                    except ios_errors.BundleError as e:
                        logging.warning(str(e))

            # If customized runner app provision is not provided, runner app will
            # use app under test's embedded provision as embedded provision.
            if not use_customized_provision:
                app_under_test_embedded_provision = os.path.join(
                    self._app_under_test_dir, 'embedded.mobileprovision')
                shutil.copyfile(app_under_test_embedded_provision,
                                runner_app_embedded_provision)

            test_bundle_team_id = bundle_util.GetDevelopmentTeam(
                self._test_bundle_dir)
            full_test_bundle_id = '%s.%s' % (test_bundle_team_id,
                                             bundle_util.GetBundleId(
                                                 self._test_bundle_dir))
            entitlements_dict = {
                'application-identifier': full_test_bundle_id,
                'com.apple.developer.team-identifier': test_bundle_team_id,
                'get-task-allow': True,
                'keychain-access-groups': [full_test_bundle_id],
            }
            entitlements_plist_path = os.path.join(uitest_runner_app,
                                                   'RunnerEntitlements.plist')
            plist_util.Plist(entitlements_plist_path).SetPlistField(
                None, entitlements_dict)

            test_bundle_signing_identity = bundle_util.GetCodesignIdentity(
                self._test_bundle_dir)
            bundle_util.CodesignBundle(xctest_framework,
                                       identity=test_bundle_signing_identity)
            if xcode_info_util.GetXcodeVersionNumber() >= 900:
                bundle_util.CodesignBundle(
                    xct_automation_framework,
                    identity=test_bundle_signing_identity)
            bundle_util.CodesignBundle(
                uitest_runner_app,
                entitlements_plist_path=entitlements_plist_path,
                identity=test_bundle_signing_identity)

            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        test_envs = {
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/'
            'Library/Frameworks' % platform_name,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/'
            'Library/Frameworks' % platform_name
        }
        self._xctestrun_dict = {
            'IsUITestBundle': True,
            'SystemAttachmentLifetime': 'keepNever',
            'TestBundlePath': self._test_bundle_dir,
            'TestHostPath': uitest_runner_app,
            'UITargetAppPath': self._app_under_test_dir,
            'UserAttachmentLifetime': 'keepNever',
            'TestingEnvironmentVariables': test_envs
        }
Exemple #4
0
    def _GenerateTestRootForXcuitest(self):
        """Generates the test root for XCUITest.

    The approach constructs xctestrun.plist and uitest runner app from Xcode.
    Then copies app under test, test bundle, xctestrun.plist and uitest
    runner app to test root directory.
    """
        platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
        platform_library_path = os.path.join(platform_path,
                                             'Developer/Library')
        uitest_runner_app = self._GetUitestRunnerAppFromXcode(
            platform_library_path)
        self._PrepareUitestInRunerApp(uitest_runner_app)

        if self._on_device:
            runner_app_embedded_provision = os.path.join(
                uitest_runner_app, 'embedded.mobileprovision')
            use_customized_provision = False
            if self._signing_options:
                customized_runner_app_provision = self._signing_options.get(
                    'xctrunner_app_provisioning_profile')
                if customized_runner_app_provision:
                    shutil.copyfile(customized_runner_app_provision,
                                    runner_app_embedded_provision)
                    use_customized_provision = True
                if self._signing_options.get(
                        'xctrunner_app_enable_ui_file_sharing'):
                    try:
                        # Don't resign the uitest runner app here since it will be resigned
                        # with passing entitlements and identity later.
                        bundle_util.EnableUIFileSharing(uitest_runner_app,
                                                        resigning=False)
                    except ios_errors.BundleError as e:
                        logging.warning(str(e))

            # If customized runner app provision is not provided, runner app will
            # use app under test's embedded provision as embedded provision.
            if not use_customized_provision:
                app_under_test_embedded_provision = os.path.join(
                    self._app_under_test_dir, 'embedded.mobileprovision')
                shutil.copyfile(app_under_test_embedded_provision,
                                runner_app_embedded_provision)

            test_bundle_team_id = bundle_util.GetDevelopmentTeam(
                self._test_bundle_dir)
            full_test_bundle_id = '%s.%s' % (test_bundle_team_id,
                                             bundle_util.GetBundleId(
                                                 self._test_bundle_dir))
            entitlements_dict = {
                'application-identifier': full_test_bundle_id,
                'com.apple.developer.team-identifier': test_bundle_team_id,
                'get-task-allow': True,
                'keychain-access-groups': [full_test_bundle_id],
            }
            entitlements_plist_path = os.path.join(uitest_runner_app,
                                                   'RunnerEntitlements.plist')
            plist_util.Plist(entitlements_plist_path).SetPlistField(
                None, entitlements_dict)

            test_bundle_signing_identity = bundle_util.GetCodesignIdentity(
                self._test_bundle_dir)

            runner_app_frameworks_dir = os.path.join(uitest_runner_app,
                                                     'Frameworks')
            os.mkdir(runner_app_frameworks_dir)
            _CopyAndSignFramework(
                os.path.join(platform_library_path,
                             'Frameworks/XCTest.framework'),
                runner_app_frameworks_dir, test_bundle_signing_identity)
            xcode_version_num = xcode_info_util.GetXcodeVersionNumber()
            if xcode_version_num >= 900:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_library_path,
                        'PrivateFrameworks/XCTAutomationSupport.framework'),
                    runner_app_frameworks_dir, test_bundle_signing_identity)
            if xcode_version_num >= 1100:
                _CopyAndSignLibFile(
                    os.path.join(platform_path,
                                 _LIB_XCTEST_SWIFT_RELATIVE_PATH),
                    runner_app_frameworks_dir, test_bundle_signing_identity)
            bundle_util.CodesignBundle(
                uitest_runner_app,
                entitlements_plist_path=entitlements_plist_path,
                identity=test_bundle_signing_identity)

            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        developer_path = '__PLATFORMS__/%s.platform/Developer' % platform_name
        test_envs = {
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:{developer}/Library/Frameworks:'
            '{developer}/Library/PrivateFrameworks'.format(
                developer=developer_path),
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:%s/usr/lib' % developer_path
        }

        # Fixes failures for UI test targets that depend on Swift libraries when running with Xcode 11
        # on pre-iOS 12.2 simulators.
        # Example failure message this resolves: "The bundle couldn’t be loaded because it is damaged
        # or missing necessary resources."
        swift5FallbackLibsDir = xcode_info_util.GetSwift5FallbackLibsDir()
        if swift5FallbackLibsDir:
            test_envs["DYLD_FALLBACK_LIBRARY_PATH"] = swift5FallbackLibsDir

        self._xctestrun_dict = {
            'IsUITestBundle':
            True,
            'SystemAttachmentLifetime':
            'keepAlways',
            'TestBundlePath':
            self._test_bundle_dir,
            'TestHostPath':
            uitest_runner_app,
            'UITargetAppPath':
            self._app_under_test_dir,
            'UserAttachmentLifetime':
            'keepAlways',
            'TestingEnvironmentVariables':
            test_envs,
            'DependentProductPaths':
            [self._app_under_test_dir, self._test_bundle_dir],
        }