提交 77835e69 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

test(downloadFile): 优化测试逻辑

上级 9cf1a96e
...@@ -3,18 +3,29 @@ const PAGE_PATH = '/pages/API/download-file/download-file' ...@@ -3,18 +3,29 @@ const PAGE_PATH = '/pages/API/download-file/download-file'
describe('ExtApi-DownloadFile', () => { describe('ExtApi-DownloadFile', () => {
let page; let page;
let res; let res;
let timeout = 3000
let waitForStartTime
async function waitCallbackTriggredOrTimeout(){
waitForStartTime = Date.now()
await page.waitFor(async () => {
const callbackTriggred = await page.data('jest_callback_triggred')
return callbackTriggred || (Date.now() - waitForStartTime > timeout)
})
}
beforeAll(async () => { beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH) page = await program.reLaunch(PAGE_PATH)
await page.waitFor(600); await page.waitFor('view');
await page.callMethod('jest_downloadFile'); await page.callMethod('jest_downloadFile');
await page.waitFor(2000); await waitCallbackTriggredOrTimeout()
res = await page.data('jest_result'); res = await page.data('jest_result');
}); });
beforeEach(async () => { beforeEach(async () => {
await page.setData({ await page.setData({
jest_result: false jest_result: false,
jest_callback_triggred: false
}) })
}); });
...@@ -24,7 +35,7 @@ describe('ExtApi-DownloadFile', () => { ...@@ -24,7 +35,7 @@ describe('ExtApi-DownloadFile', () => {
it('Check Special characters Url download file', async () => { it('Check Special characters Url download file', async () => {
res = await page.callMethod('jest_special_characters_download') res = await page.callMethod('jest_special_characters_download')
await page.waitFor(2000); await waitCallbackTriggredOrTimeout()
res = await page.data('jest_result'); res = await page.data('jest_result');
expect(res).toBe(true) expect(res).toBe(true)
}); });
...@@ -35,7 +46,7 @@ describe('ExtApi-DownloadFile', () => { ...@@ -35,7 +46,7 @@ describe('ExtApi-DownloadFile', () => {
) { ) {
it('Check uni.env', async () => { it('Check uni.env', async () => {
await page.callMethod('jest_downloadFile_with_uni_env'); await page.callMethod('jest_downloadFile_with_uni_env');
await page.waitFor(2000); await waitCallbackTriggredOrTimeout()
res = await page.data('jest_result'); res = await page.data('jest_result');
expect(res).toBe(true); expect(res).toBe(true);
}); });
...@@ -47,7 +58,7 @@ describe('ExtApi-DownloadFile', () => { ...@@ -47,7 +58,7 @@ describe('ExtApi-DownloadFile', () => {
if (!process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios') || version > 15) { if (!process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios') || version > 15) {
it('Check Download File In UTS Module', async () => { it('Check Download File In UTS Module', async () => {
res = await page.callMethod('jest_uts_module_invoked') res = await page.callMethod('jest_uts_module_invoked')
await page.waitFor(2000); await waitCallbackTriggredOrTimeout()
res = await page.data('jest_result'); res = await page.data('jest_result');
expect(res).toBe(true) expect(res).toBe(true)
}) })
...@@ -70,13 +81,13 @@ describe('ExtApi-DownloadFile', () => { ...@@ -70,13 +81,13 @@ describe('ExtApi-DownloadFile', () => {
it('Check Set Cookie', async () => { it('Check Set Cookie', async () => {
res = await page.callMethod('jest_set_cookie') res = await page.callMethod('jest_set_cookie')
await page.waitFor(2000); await waitCallbackTriggredOrTimeout()
res = await page.data('jest_result'); res = await page.data('jest_result');
expect(res).toBe(true) expect(res).toBe(true)
}); });
it('Check Delete Cookie', async () => { it('Check Delete Cookie', async () => {
res = await page.callMethod('jest_delete_cookie') res = await page.callMethod('jest_delete_cookie')
await page.waitFor(2000); await waitCallbackTriggredOrTimeout()
res = await page.data('jest_result'); res = await page.data('jest_result');
expect(res).toBe(true) expect(res).toBe(true)
}); });
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
</view> </view>
<view v-else style="margin: 10px;"> <view v-else style="margin: 10px;">
<text class="uni-hello-text">点击按钮下载服务端示例图片(下载网络文件到本地临时目录)</text> <text class="uni-hello-text">点击按钮下载服务端示例图片(下载网络文件到本地临时目录)</text>
<button type="primary" @tap="downloadImage">下载</button> <button type="primary" @tap="downloadImagenal">下载</button>
</view> </view>
<view style="margin: 10px;"> <view style="margin: 10px;">
<text class="uni-hello-text">下载接口的Content-Disposition中的filename非法值例子</text> <text class="uni-hello-text">下载接口的Content-Disposition中的filename非法值例子</text>
...@@ -37,7 +37,8 @@ ...@@ -37,7 +37,8 @@
imageSrc: '', imageSrc: '',
task: null as DownloadTask | null, task: null as DownloadTask | null,
//自动化测试例专用 //自动化测试例专用
jest_result: false jest_result: false,
jest_callback_triggred: false
} }
}, },
onLoad() { onLoad() {
...@@ -87,10 +88,12 @@ ...@@ -87,10 +88,12 @@
uni.downloadFile({ uni.downloadFile({
url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png", url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
success: () => { success: () => {
this.jest_result = true this.jest_result = true
this.jest_callback_triggred = true
}, },
fail: () => { fail: () => {
this.jest_result = false this.jest_result = false
this.jest_callback_triggred = true
} }
}); });
}, },
...@@ -101,9 +104,11 @@ ...@@ -101,9 +104,11 @@
filePath: `${uni.env.CACHE_PATH}/a/b/`, filePath: `${uni.env.CACHE_PATH}/a/b/`,
success: () => { success: () => {
this.jest_result = true this.jest_result = true
this.jest_callback_triggred = true
}, },
fail: () => { fail: () => {
this.jest_result = false this.jest_result = false
this.jest_callback_triggred = true
} }
}); });
}, },
...@@ -118,9 +123,11 @@ ...@@ -118,9 +123,11 @@
firstIpv4: false, firstIpv4: false,
success: () => { success: () => {
this.jest_cookie_download(true) this.jest_cookie_download(true)
this.jest_callback_triggred = true
}, },
fail: () => { fail: () => {
this.jest_result = false; this.jest_result = false;
this.jest_callback_triggred = true
}, },
}); });
}, },
...@@ -135,9 +142,11 @@ ...@@ -135,9 +142,11 @@
firstIpv4: false, firstIpv4: false,
success: () => { success: () => {
this.jest_cookie_download(false) this.jest_cookie_download(false)
this.jest_callback_triggred = true
}, },
fail: () => { fail: () => {
this.jest_result = false; this.jest_result = false;
this.jest_callback_triggred = true
}, },
}); });
}, },
...@@ -157,9 +166,11 @@ ...@@ -157,9 +166,11 @@
testInovkeDownloadFile({ testInovkeDownloadFile({
success: (res : any) => { success: (res : any) => {
this.jest_result = true this.jest_result = true
this.jest_callback_triggred = true
}, },
fail: (err : any) => { fail: (err : any) => {
this.jest_result = false this.jest_result = false
this.jest_callback_triggred = true
} }
} as CommonOptions) } as CommonOptions)
// #endif // #endif
...@@ -167,11 +178,13 @@ ...@@ -167,11 +178,13 @@
jest_special_characters_download() { jest_special_characters_download() {
uni.downloadFile({ uni.downloadFile({
url: "https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/1789834995055525889-你好%23你好.png", url: "https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/1789834995055525889-你好%23你好.png",
success: (res : DownloadFileSuccess) => { success: (res : DownloadFileSuccess) => {
this.jest_result = true; this.jest_result = true;
this.jest_callback_triggred = true
}, },
fail: () => { fail: () => {
this.jest_result = false; this.jest_result = false;
this.jest_callback_triggred = true
} }
}); });
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册