image.test.js 4.1 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2 3
// uni-app自动化测试教程: uni-app自动化测试教程: https://uniapp.dcloud.net.cn/worktile/auto/hbuilderx-extension/

describe('component-native-image', () => {
4
  let page;
5
  let start = 0;
6 7 8 9 10 11 12
  async function getWindowInfo() {
    const windowInfoPage = await program.reLaunch('/pages/API/get-window-info/get-window-info')
    await windowInfoPage.waitFor(600);
    return await windowInfoPage.callMethod('jest_getWindowInfo')
  }
  const screenshotParams = { fullPage: true }
  let windowInfo
lizhongyi_'s avatar
lizhongyi_ 已提交
13

14 15 16 17 18 19 20 21 22 23 24 25 26
  beforeAll(async () => {
    if (!process.env.UNI_AUTOMATOR_APP_WEBVIEW) {
      screenshotParams.fullPage = false
      windowInfo = await getWindowInfo()
      let offsetY = '0'
      if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('android')) {
        offsetY = `${windowInfo.statusBarHeight + 44}`
      }
      if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios')) {
        offsetY = `${windowInfo.safeAreaInsets.top + 44}`
      }
      screenshotParams.offsetY = offsetY
    }
lizhongyi_'s avatar
lizhongyi_ 已提交
27

28 29 30
    page = await program.reLaunch('/pages/component/image/image');
    await page.waitFor(600);
  });
shutao-dc's avatar
shutao-dc 已提交
31

32 33 34
  it('check_image_load', async () => {
    expect(await page.data('loadError')).toBe(false)
  });
shutao-dc's avatar
shutao-dc 已提交
35

36 37 38 39 40 41 42 43
  it('check_image_load_url', async () => {
    await page.setData({
      loadError: false,
      imageSrc: 'https://request.dcloud.net.cn/api/http/contentType/image/png'
    })
    await page.waitFor(300);
    expect(await page.data('loadError')).toBe(false)
  })
shutao-dc's avatar
shutao-dc 已提交
44

45 46 47 48
  it('check_image_load_error', async () => {
    await page.setData({
      loadError: false,
      imageSrc: 'testerror.jpg'
shutao-dc's avatar
shutao-dc 已提交
49
    })
50 51 52
    await page.waitFor(300);
    expect(await page.data('loadError')).toBe(true)
  })
雪洛's avatar
雪洛 已提交
53

54 55 56 57 58 59 60
  if (process.env.uniTestPlatformInfo.startsWith('android') && !process.env.UNI_AUTOMATOR_APP_WEBVIEW) {
    it('check-cookie', async () => {
      await page.setData({
        autoTest: true,
        setCookieImage: 'https://cdn.dcloud.net.cn/img/shadow-grey.png'
      });
      await page.waitFor(1000);
shutao-dc's avatar
shutao-dc 已提交
61 62
      await page.setData({
        loadError: false,
63 64 65 66 67 68 69
        verifyCookieImage: 'https://request.dcloud.net.cn/img/shadow-grey.png'
      });
      await page.waitFor(1000);
      expect(await page.data('loadError')).toBe(false);
      await page.setData({
        autoTest: false
      });
shutao-dc's avatar
shutao-dc 已提交
70
    })
71
  }
72

73 74 75 76 77
  it('test event load', async () => {
    await page.setData({
      autoTest: true,
      imageSrc: 'https://request.dcloud.net.cn/api/http/contentType/image/png'
    });
78 79 80 81
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventLoad')) || (Date.now() - start > 1000);
    });
lizhongyi_'s avatar
lizhongyi_ 已提交
82 83 84 85 86 87 88 89
    if(process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      expect(await page.data('eventLoad')).toEqual({
        type: 'load',
        width: 10,
        height: 10
      });
      return
    }
90 91 92 93 94 95 96 97 98 99 100 101
    expect(await page.data('eventLoad')).toEqual({
      tagName: 'IMAGE',
      type: 'load',
      width: 10,
      height: 10
    });
  });

  it('test event error', async () => {
    await page.setData({
      imageSrc: 'https://request.dcloud.net.cn/api/http/contentType/404.png'
    });
102 103 104 105
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventError')) || (Date.now() - start > 1000);
    });
lizhongyi_'s avatar
lizhongyi_ 已提交
106 107 108 109 110 111 112 113 114 115 116
    if(process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      expect(await page.data('eventError')).toEqual({
        type: 'error'
      });
    }else {
      expect(await page.data('eventError')).toEqual({
        tagName: 'IMAGE',
        type: 'error'
      });
    }

117 118 119 120 121
    await page.setData({
      autoTest: false
    });
  });

122 123 124 125 126 127
  it('path-screenshot', async () => {
    const page = await program.navigateTo('/pages/component/image/image-path');
    await page.waitFor(3000);
    const image = await program.screenshot(screenshotParams)
    expect(image).toSaveImageSnapshot()
  });
128

129 130 131 132
  it('mode-screenshot', async () => {
    if (process.env.android_cpu_type === 'x86_64') return
    const page = await program.navigateTo('/pages/component/image/image-mode');
    await page.waitFor(1000);
DCloud-WZF's avatar
DCloud-WZF 已提交
133
    const image = await program.screenshot({fullPage: true})
134 135
    expect(image).toSaveImageSnapshot()
  });
shutao-dc's avatar
shutao-dc 已提交
136
});