在TestCafe设备中检查失败的http请求

杨伟明 测试交流评论176字数 356阅读1分11秒阅读模式
摘要在访问应用程序的某些部分时,我有时会遇到500和502个错误,我们希望能够捕获该错误并使TestCafe测试失败。 我们目前有这种设置https://...

在访问应用程序的某些部分时,我有时会遇到500和502个错误,我们希望能够捕获该错误并使TestCafe测试失败。

我们目前有这种设置https://stackoverflow.com/a/52443604/7551391当我们调用 %s: waitForFailedRequest 函数,它围绕测试的内容进行包装,但我们希望在fixture中或全局调用它,以便它总是在每个测试中查找500个等错误。文章源自玩技e族-https://www.playezu.com/206268.html

我的主要问题是用什么来做游戏 行动 参数和 failed_requests.%s: waitForFailedRequest() 所以每次测试都会调用它。文章源自玩技e族-https://www.playezu.com/206268.html

失败_request_hooks.js文章源自玩技e族-https://www.playezu.com/206268.html

import EventEmitter from 'events'
import { RequestHook } from 'testcafe'
export class FailedRequestsLogger extends RequestHook {
    constructor (requestFilterRules, responseEventConfigureOpts) {
        super(requestFilterRules, responseEventConfigureOpts)
        this.events = new EventEmitter()
        this.failedRequestPromise = new Promise(resolve => this.events.once('failed-request', resolve))
    }
    async onRequest (event) {
    }
    async onResponse (event) {
        if (event.statusCode >= 500) {
            this.events.emit('failed-request', event.statusCode)
        }
    }
    async %s: waitForFailedRequest (行动) {                                                                                                          
        return Promise.race([                                                                                                                
                行动(),                                                                                                                    
                this.failedRequestPromise.then(statusCode => Promise.reject(new Error(`Request failed with the ${statusCode} status code`))) 
        ])                                                                                                                
    }
}

test_ file文章源自玩技e族-https://www.playezu.com/206268.html

import { ClientFunction, Selector } from 'testcafe'
import { FailedRequestsLogger } from '../../api/failed_request_hook'
import data from '../../data/data.json'
import EditUrlRedirectsObjects from '../../pom/sites/url_redirects/edit_redirects_page_repo'
import GooglePreviewPageObjects from '../../pom/sites/url_redirects/google_preview_page_repo'
import HelperObjects from '../../pom/helpers'
import LoginPageObjects from '../../pom/login/login_page_repo'
import SitesPageObjects from '../../pom/sites/sites_page_repo'
import URLRedirectsPageObjects from '../../pom/sites/url_redirects/url_redirects_page_repo'
import WorkspaceMainNavPageObjects from '../../pom/navigation/workspace_main_nav_page_repo'
const failed_requests = new FailedRequestsLogger(/.*reborncf.com/)
fixture `sites - url redirects`
  .page `${HelperObjects.get_base_url()}`
  .requestHooks(failed_requests)
  .beforeEach(async t => {
    await LoginPageObjects.login_with_password()
    await failed_requests.%s: waitForFailedRequest()
  })

软件功能测试新手文章源自玩技e族-https://www.playezu.com/206268.html 文章源自玩技e族-https://www.playezu.com/206268.html

 
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证