如何获得操纵up的重定向状态?

时间:2019-04-15 06:00:12

标签: jestjs puppeteer

我正在尝试使用puppeteer编写Jest测试

override func setSelected(_ selected: Bool, animated: Bool) {
    guard isSelected != selected else {
        return
    }
    super.setSelected(selected, animated: animated)
    if animated {...}
}

// how can I call animated with true?
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    guard isHighlighted != highlighted else {
        return
    }
    super.setHighlighted(highlighted, animated: animated)
    if animated {...}
}


// overriding the touch handlers doesn't feel right.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    setHighlighted(true, animated: true)
    super.touchesBegan(touches, with: event)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    setHighlighted(false, animated: true)
    setSelected(!isSelected, animated: true)
    super.touchesEnded(touches, with: event)
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    setHighlighted(false, animated: true)
    super.touchesCancelled(touches, with: event)
}


但是响应状态为describe('Downloads', () => { it(`should refirect to download page for ios`, async () => { await page.setUserAgent('exotic'); let response = await page.goto(`http://localhost:8888/downloads`, {waitUntil: "networkidle0"}); let url = page.url(); expect(response.status()).toBe(303); expect(url).toBe(`http://localhost:8888/downloads/ios`); }); }); ,因为200返回了goto的响应

如何获取重定向状态代码?

1 个答案:

答案 0 :(得分:1)

您可以为此使用request.redirectChain()。它返回一个包含所有请求的数组。然后,您可以像这样访问第一个请求(和响应):

const response = await page.goto('...');

const chain = response.request().redirectChain();
const redirectRequest = chain[0];
const redirectResponse = await redirectRequest.response();

expect(redirectResponse.status()).toBe(303);
expect(redirectResponse.url()).toBe('...');