华为开发者平台测试相关功能汇总

2025-06-20 17:24:58
171次阅读
0个评论
最后修改时间:2025-07-21 23:38:47

一、沙盒测试(Sandbox Testing)

文档链接:​​文档中心​核心目的:通过配置测试账号模拟华为应用内支付、付费下载、游戏等服务的真实环境,实现免费测试。

1. 前提条件
2. 操作步骤
  1. 登录与进入页面
  • 登录 AppGallery Connect,选择 “用户与访问”,进入 “沙盒测试 > 测试账号”。
  1. 新增测试账号
  • 点击 “新增”,填写已注册的真实华为账号(手机号 / 邮箱)和名称(≤64 字符),点击 “确认”。
  1. 清除购买历史记录
  • 如需重复测试订阅或非消耗型商品,勾选账号后点击 “清除购买历史记录”。
  1. 模拟购买失败(仅支持 HarmonyOS NEXT)
  • 编辑账号时勾选 “中断此测试账号的购买流程”,可模拟购买中断;取消勾选恢复正常。
3. 关键功能
  • 中断购买流程:仅适用于 HarmonyOS NEXT 系统,用于验证网络异常等场景下的处理逻辑。

二、云测试(Cloud Testing)

文档链接:​​文档中心​核心目的:提供一站式移动应用测试服务,通过华为真机设备检测应用的兼容性、稳定性、性能、功耗及安全性问题。

1. 服务介绍
  • 解决问题:降低测试成本,解决设备机型不足、技术效率低等问题,出具专业测试报告。
  • 测试类型:兼容性测试、稳定性测试、性能测试(CPU / 内存 / 耗电 / 流量)、功耗测试、安全测试。
2. 使用场景
场景分类 场景说明
检测应用兼容性问题 通过不同华为真机设备检测 Release 版本应用的兼容性问题。
检测应用稳定性问题 通过长时间测试检测应用在真机上的稳定性(如崩溃、卡顿)。
检测应用性能问题 采集 CPU、内存、耗电量、流量等指标,分析性能薄弱点。
检测应用功耗问题 检测影响 App 功耗的关键指标,优化电池使用效率。
账号登录测试 在兼容性 / 性能 / 上架测试中预设账号密码,实现登录后完整测试。
设置自定义指令 预设操作指令,使测试按指定流程执行(如页面跳转、功能调用)。
3. 核心优势
  • 覆盖华为热门机型,无需自备设备,快速定位设备兼容性问题。

三、云调试(Cloud Debugging)

文档链接:​​文档中心​核心目的:提供云端设备调试解决方案,解决设备机型不足、管理困难及 bug 复现问题。

1. 服务介绍
  • 功能亮点:支持远程调试不同型号的华为真机设备,实时查看应用运行表现,降低设备采购与管理成本。
2. 使用场景
场景分类 场景说明
应用调试 上传应用至云端真机设备,直接调试功能逻辑、界面显示等问题。
主题调试 上传主题资源至真机设备,调试主题在不同机型上的显示效果(如壁纸、字体)。
预约测试 当设备资源紧张时,预约指定设备的调试时间,系统通过邮件 / 短信通知上线申请。
3. 核心功能
  • 预约测试:避免设备抢占,提前规划调试时间,提升效率。

四、三服务对比与关联

服务类型 沙盒测试 云测试 云调试
核心目标 模拟服务支付环境 检测应用运行质量 远程调试设备与复现 bug
测试环境 虚拟沙盒环境 华为真机设备集群 华为真机设备集群
典型场景 支付流程测试、订阅功能验证 兼容性测试、性能优化 功能逻辑调试、主题适配调试
设备依赖 无需真机,依赖沙盒账号 需要真机设备资源 需要真机设备资源
特殊功能 购买流程中断模拟(HarmonyOS) 自定义指令、账号登录预设 设备预约、实时调试

五、关键问题快速索引

  1. 沙盒测试的前提条件是什么?
  • 应用集成 HMS Core SDK 3.0 及以上,不同服务需参考对应测试文档。
  1. 云测试支持哪些测试类型?
  • 兼容性、稳定性、性能、功耗、安全测试,支持自定义指令和账号登录。
  1. 云调试的预约测试功能如何使用?
  • 设备资源紧张时,预约调试设备的日期和时段,系统通知后申请使用。
import http from '@ohos.net.http';
import common from '@ohos.app.ability.common';
import promptAction from '@ohos.promptAction';

@Entry
@Component
struct TestingDashboard {
  // 沙盒测试状态
  @State sandboxStatus: string = '未激活';
  // 云测试报告
  @State cloudTestReports: Array<CloudTestReport> = [];
  // 测试商品信息
  @State products: Array<iap.ProductInfo> = [];
  // 当前选中的商品ID
  @State selectedProductId: string = '';
  // 测试账号
  @State testAccount: string = 'testuser@example.com';

  // 页面构建
  build() {
    Column() {
      // 沙盒测试面板
      this.buildSandboxPanel()
      
      // 云测试报告
      this.buildCloudTestPanel()
    }
    .padding(20)
    .width('100%')
    .height('100%')
    .backgroundColor('#F5F7FA')
    .onPageShow(() => {
      this.loadSandboxStatus();
      this.loadCloudTestReports();
    })
  }

  // 构建沙盒测试面板
  @Builder
  buildSandboxPanel() {
    Column() {
      // 标题
      Text('沙盒测试')
        .fontSize(24)
        .fontWeight(FontWeight.Bold)
        .margin({ bottom: 20 })
        .alignSelf(HorizontalAlign.Start)
      
      // 状态卡片
      Row() {
        Text(`状态: ${this.sandboxStatus}`)
          .fontSize(18)
          .fontColor(this.sandboxStatus === '已激活' ? '#07C160' : '#FF9500')
        
        Blank()
        
        Button(this.sandboxStatus === '已激活' ? '停用沙盒' : '激活沙盒')
          .type(ButtonType.Capsule)
          .fontSize(14)
          .backgroundColor(this.sandboxStatus === '已激活' ? '#FF3B30' : '#007AFF')
          .onClick(() => this.toggleSandbox())
      }
      .padding(15)
      .width('100%')
      .backgroundColor(Color.White)
      .borderRadius(12)
      .shadow({ radius: 6, color: '#00000010' })
      
      // 测试账号设置
      TextInput({ placeholder: '输入沙盒测试账号' })
        .width('100%')
        .height(50)
        .margin({ top: 15 })
        .fontSize(16)
        .backgroundColor(Color.White)
        .borderRadius(8)
        .value(this.testAccount)
        .onChange(value => this.testAccount = value)
      
      // 商品选择
      if (this.products.length > 0) {
        Text('选择测试商品:')
          .fontSize(18)
          .margin({ top: 20, bottom: 10 })
          .alignSelf(HorizontalAlign.Start)
        
        Grid() {
          ForEach(this.products, (product) => {
            GridItem() {
              Column() {
                Text(product.productName)
                  .fontSize(16)
                  .maxLines(1)
                  .textOverflow({ overflow: TextOverflow.Ellipsis })
                Text(`¥${product.price}`)
                  .fontSize(14)
                  .fontColor('#888')
                  .margin({ top: 5 })
              }
              .padding(15)
              .width('100%')
              .backgroundColor(this.selectedProductId === product.productId ? '#E6F7FF' : Color.White)
              .borderRadius(8)
              .border({ width: this.selectedProductId === product.productId ? 2 : 1, color: this.selectedProductId === product.productId ? '#1890FF' : '#EEE' })
              .onClick(() => {
                this.selectedProductId = product.productId;
              })
            }
          })
        }
        .columnsTemplate('1fr 1fr')
        .rowsGap(10)
        .columnsGap(10)
        .height(200)
        .margin({ bottom: 20 })
        
        // 测试按钮
        Button('模拟购买')
          .width('80%')
          .height(50)
          .backgroundColor('#007AFF')
          .enabled(this.selectedProductId !== '')
          .onClick(() => this.simulatePurchase())
      } else {
        Text('正在加载商品信息...')
          .fontSize(16)
          .margin({ top: 20 })
      }
    }
    .padding(20)
    .width('100%')
    .backgroundColor('#FFFFFF')
    .borderRadius(16)
    .margin({ bottom: 25 })
    .shadow({ radius: 8, color: '#00000010' })
  }

  // 构建云测试面板
  @Builder
  buildCloudTestPanel() {
    Column() {
      // 标题
      Row() {
        Text('云测试报告')
          .fontSize(24)
          .fontWeight(FontWeight.Bold)
        
        Blank()
        
        Button('刷新报告')
          .type(ButtonType.Capsule)
          .fontSize(14)
          .backgroundColor('#007AFF')
          .onClick(() => this.loadCloudTestReports())
      }
      .width('100%')
      .margin({ bottom: 15 })
      
      // 报告列表
      if (this.cloudTestReports.length > 0) {
        List({ space: 15 }) {
          ForEach(this.cloudTestReports, (report) => {
            ListItem() {
              this.buildReportItem(report)
            }
          })
        }
        .width('100%')
        .height('40%')
      } else {
        Text('暂无测试报告')
          .fontSize(16)
          .margin({ top: 20 })
      }
      
      // 启动新测试按钮
      Button('启动新云测试')
        .type(ButtonType.Capsule)
        .width('80%')
        .height(50)
        .margin({ top: 20 })
        .backgroundColor('#34C759')
        .onClick(() => this.startNewCloudTest())
    }
    .padding(20)
    .width('100%')
    .backgroundColor('#FFFFFF')
    .borderRadius(16)
    .shadow({ radius: 8, color: '#00000010' })
  }

  // 构建报告项
  @Builder
  buildReportItem(report: CloudTestReport) {
    Column() {
      // 报告标题
      Row() {
        Text(report.testType)
          .fontSize(18)
          .fontWeight(FontWeight.Medium)
        
        Blank()
        
        Text(report.status)
          .fontColor(report.status === '通过' ? '#07C160' : '#FF9500')
      }
      .margin({ bottom: 10 })
      
      // 设备信息
      Text(`设备: ${report.deviceModel} | 系统: HarmonyOS ${report.osVersion}`)
        .fontSize(14)
        .fontColor('#666')
        .margin({ bottom: 5 })
      
      // 测试结果
      Text(`结果: ${report.passCount}通过 / ${report.failCount}失败 / ${report.warningCount}警告`)
        .fontSize(14)
        .fontColor('#666')
      
      // 查看详情按钮
      Button('查看详情')
        .width('100%')
        .height(40)
        .margin({ top: 10 })
        .onClick(() => this.viewReportDetail(report.id))
    }
    .padding(15)
    .width('100%')
    .backgroundColor('#F9F9F9')
    .borderRadius(12)
  }

  // 加载沙盒状态
  private async loadSandboxStatus() {
    try {
      // 模拟API调用获取沙盒状态
      await new Promise(resolve => setTimeout(resolve, 500));
      
      // 实际项目中应调用HMS SDK接口
      // const status = await iap.isSandboxActivated();
      this.sandboxStatus = '已激活';
      
      // 加载商品列表
      this.loadProducts();
    } catch (error) {
      console.error('获取沙盒状态失败:', error);
      promptAction.showToast({ message: '沙盒服务不可用', duration: 2000 });
    }
  }

  // 切换沙盒状态
  private toggleSandbox() {
    if (this.sandboxStatus === '已激活') {
      // 实际调用: await iap.disableSandbox();
      this.sandboxStatus = '未激活';
      promptAction.showToast({ message: '沙盒已停用', duration: 2000 });
    } else {
      // 实际调用: await iap.enableSandbox(this.testAccount);
      this.sandboxStatus = '已激活';
      promptAction.showToast({ message: '沙盒已激活', duration: 2000 });
    }
  }

  // 加载商品列表
  private async loadProducts() {
    try {
      // 实际项目中调用IAP SDK获取商品列表
      // this.products = await iap.getProducts(['product1', 'product2', 'product3']);
      
      // 模拟数据
      this.products = [
        { productId: 'com.product.basic', productName: '基础会员', price: '18.00' },
        { productId: 'com.product.pro', productName: '专业版', price: '58.00' },
        { productId: 'com.product.gold', productName: '黄金会员', price: '128.00' },
        { productId: 'com.product.diamond', productName: '钻石会员', price: '298.00' }
      ];
    } catch (error) {
      console.error('加载商品失败:', error);
    }
  }

  // 模拟购买
  private async simulatePurchase() {
    if (!this.selectedProductId) {
      promptAction.showToast({ message: '请选择商品', duration: 2000 });
      return;
    }
    
    try {
      // 实际购买流程
      // const purchaseData = await iap.createPurchaseIntent({
      //   productId: this.selectedProductId,
      //   sandbox: true
      // });
      
      // 模拟购买结果
      await new Promise(resolve => setTimeout(resolve, 1000));
      
      promptAction.showDialog({
        title: '购买成功',
        message: `您已成功购买: ${this.products.find(p => p.productId === this.selectedProductId)?.productName}`,
        buttons: [{ text: '确定' }]
      });
      
      // 清除购买历史(仅测试环境)
      // await iap.clearPurchaseHistory(this.testAccount);
      
    } catch (error) {
      console.error('购买失败:', error);
      promptAction.showToast({ message: '购买失败: ' + error.message, duration: 3000 });
    }
  }

  // 加载云测试报告
  private async loadCloudTestReports() {
    try {
      // 模拟API调用
      await new Promise(resolve => setTimeout(resolve, 800));
      
      // 模拟数据
      this.cloudTestReports = [
        {
          id: 'CT20240721001',
          testType: '兼容性测试',
          deviceModel: 'P60 Pro',
          osVersion: '4.0.0',
          status: '通过',
          passCount: 42,
          failCount: 0,
          warningCount: 3,
          timestamp: '2024-07-21 10:30'
        },
        {
          id: 'CT20240720002',
          testType: '性能测试',
          deviceModel: 'MatePad Pro',
          osVersion: '3.1.0',
          status: '警告',
          passCount: 8,
          failCount: 1,
          warningCount: 2,
          timestamp: '2024-07-20 15:45'
        }
      ];
    } catch (error) {
      console.error('加载报告失败:', error);
    }
  }

  // 查看报告详情
  private viewReportDetail(reportId: string) {
    // 实际项目中打开详细报告页面
    promptAction.showToast({ message: `打开报告: ${reportId}`, duration: 2000 });
  }

  // 启动新云测试
  private startNewCloudTest() {
    // 实际项目中调用AGC云测试API
    promptAction.showDialog({
      title: '启动云测试',
      message: '选择测试类型:',
      buttons: [
        { text: '兼容性测试' },
        { text: '性能测试' },
        { text: '稳定性测试' },
        { text: '取消' }
      ]
    }).then(result => {
      if (result.index < 3) {
        const testTypes = ['兼容性', '性能', '稳定性'];
        promptAction.showToast({ message: `已启动${testTypes[result.index]}测试`, duration: 3000 });
      }
    });
  }
}

// 云测试报告数据结构
class CloudTestReport {
  id: string = '';          // 报告ID
  testType: string = '';    // 测试类型
  deviceModel: string = ''; // 设备型号
  osVersion: string = '';   // 系统版本
  status: string = '';      // 测试状态
  passCount: number = 0;    // 通过项
  failCount: number = 0;    // 失败项
  warningCount: number = 0; // 警告项
  timestamp: string = '';   // 测试时间
}

·##HarmonyOS应用测试##商务##

收藏00

登录 后评论。没有帐号? 注册 一个。