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

3 4 5
describe('component-native-image', () => {
  const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
  const isMP = platformInfo.startsWith('mp')
6
  let page;
7
  let start = 0;
8 9 10 11 12 13 14
  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_ 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28
  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_ 已提交
29

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

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

38 39 40 41 42 43 44 45
  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 已提交
46

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

56 57 58 59 60 61 62
  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 已提交
63 64
      await page.setData({
        loadError: false,
65 66 67 68 69 70 71
        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 已提交
72
    })
73
  }
74

75 76 77 78 79
  it('test event load', async () => {
    await page.setData({
      autoTest: true,
      imageSrc: 'https://request.dcloud.net.cn/api/http/contentType/image/png'
    });
80 81 82 83
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventLoad')) || (Date.now() - start > 1000);
    });
84
    expect(await page.data('eventLoad')).toEqual({
85
      tagName: isMP ? undefined : 'IMAGE',
86 87 88 89 90 91 92 93 94 95
      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'
    });
96 97 98 99
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventError')) || (Date.now() - start > 1000);
    });
100 101 102 103
    expect(await page.data('eventError')).toEqual({
      tagName: isMP ? undefined : 'IMAGE',
      type: 'error'
    });
lizhongyi_'s avatar
lizhongyi_ 已提交
104

105 106 107 108 109
    await page.setData({
      autoTest: false
    });
  });

110 111 112 113 114 115
  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()
  });
116

117 118 119 120
  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 已提交
121
    const image = await program.screenshot({fullPage: true})
122 123
    expect(image).toSaveImageSnapshot()
  });
shutao-dc's avatar
shutao-dc 已提交
124
});