playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] Unable to redirect url using Page.route

Open sarut0bi opened this issue 6 years ago • 11 comments

hi, i tried to make a redirection on XHR on another url unsucessfully here is what i tried unsuccessfully:

await page.route('https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]', (request) => {
  request.continue({url: 'https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]'});
}
await page.goto('https://www.wikipedia.org/');

and

await page.route('https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]', (request) => {
  request._url = 'https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]';
  request.continue();
}
await page.goto('https://www.wikipedia.org/');

Could you provide me a good way to do it?

sarut0bi avatar Mar 23 '20 19:03 sarut0bi

Hi @sarut0bi, it is not possible to override the url in the continue method (overrides doc). However, you could fulfill the request with the desired image (docs)

arjunattam avatar Mar 23 '20 20:03 arjunattam

i ve fulfilled it with a 301 :)

        request.fulfill(    {status: 301,
          headers: {Location: 'https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]' });

sarut0bi avatar Mar 24 '20 08:03 sarut0bi

That's a better solution! :)

arjunattam avatar Mar 24 '20 15:03 arjunattam

Hi! Thank you for the solution! It works great for me while running chromium and firefox, but does not work for WebKit :( Do you know what could be the problem? ps: it not the cors problem, but i tried adding header 'Access-control-allow-origin': '*', Playwright 1.5.2 WebKit 14.0 Ubuntu 18.04

AnnaIvakhnik avatar Nov 11 '20 08:11 AnnaIvakhnik

There is a bug in WebKit implementation of fulfill, I'll have a look.

yury-s avatar Nov 11 '20 18:11 yury-s

Request interception on mac is handled within network stack so it doesn't kick in when request if "fulfilled" which leads to wrong behavior there. Instead of trying to work around that (which could end up in another implementation of redirect logic in the browser) we decided to allow to override url in route.continue().

yury-s avatar Nov 16 '20 18:11 yury-s

hello @yury-s ! Thank you for your implementation. I have installed 1.7.0-next.1605581873574 and got url override param at continue method of route and set its value as you are doing at https://github.com/yury-s/playwright/blob/688c13ff41056eb8df137520fb7781c54f01a8a8/test/request-continue.spec.ts . But it looks like it does not really update url, i have added await page.on('response', (response) => { if (response.url().includes("artifactName")) { console.log(response for url ${response.url()} status is ${response.status()}) } }); and saw that it is still navigating to original url. This verification method works fine for chromium and firefox with fulfilll workaround. :(

AnnaIvakhnik avatar Nov 17 '20 09:11 AnnaIvakhnik

@AnnaIvakhnik this is kind of working is intended, the actual request is sent to the overridden url but on the browser side it all looks like original url (response.url() will still return original url). In particular, if it was main frame navigation the new location.href would still return original url but the content and the entire response would served using the new url. I can see that it may be confusing and sounds like you expect the behavior as if the server returned a redirect response.

Do you believe the that current behavior where the overridden url is only seen on the server side would still address your use case or there is a strong reason why the url should be changed in the browser too? I'd like to learn more about your use case, it would help us evaluate if having url override in its current form makes sense at all. Given the technical limitations of the network stack it's unlikely we'll be able to support fulfilling with redirect responses in WebKit and with that in mind it'd be nice if the existing logic worked at least for most common scenarios.

yury-s avatar Nov 17 '20 18:11 yury-s

Hi! @yury-s thank you for the clarification! yep, sorry, it was confusing, believed page.on('response') is like another independent listener which can inform me on all responses from the server. Just tried to be sure, my request url override works as expected since it is not possible to check while breakpoint test (in Network dev tools tab, because of the playwright 'proxy' disconnecting behaviour after dev tools starting, which is not a bug, as I found from another ticket). Is there a way to make sure i am getting responses from chosen url without devtools and page.on('response') ? My case: we have some versioned artefact, we want to test not on global availability version, but for example on the specific release candidate and I want to have all requests to the server, which has in url name of artefact, go not to original (ga) version, but on version i am overriding.

AnnaIvakhnik avatar Nov 18 '20 12:11 AnnaIvakhnik

Is there a way to make sure i am getting responses from chosen url without devtools and page.on('response') ?

You could inspect response.headers() in case the server returns a version-specific header or response.body()/response.json() those will contain actual response.

yury-s avatar Nov 18 '20 17:11 yury-s

Thank you!

AnnaIvakhnik avatar Nov 19 '20 10:11 AnnaIvakhnik