image.test.js 3.5 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 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
  let page;
  
  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
  
  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
    }
    
    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
    await page.waitFor(300);
79 80 81 82 83 84 85 86 87 88 89 90
    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'
    });
91
    await page.waitFor(300);
92 93 94 95 96 97 98 99 100
    expect(await page.data('eventError')).toEqual({
      tagName: 'IMAGE',
      type: 'error'
    });
    await page.setData({
      autoTest: false
    });
  });

101 102 103 104 105 106
  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()
  });
107

108 109 110 111
  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 已提交
112
    const image = await program.screenshot({fullPage: true})
113 114
    expect(image).toSaveImageSnapshot()
  });
shutao-dc's avatar
shutao-dc 已提交
115
});