question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Is it possible to define host resolution rules per browser instance?

See original GitHub issue

In my daily work, I often have to tweak the /etc/hosts file for routing production site URLs to the development sites for testing. Is it possible to launch a browser instance with a set of customized host resolution rules? For example,

const browser = await chromium.launch( {
    hosts: {
        'example.com': '127.0.0.1',  // all the request to example.com will then be routed to my local machine.
    }
} );

I believe it’s possible by dockerizing my playwright testing program, but would like to see if there is a lightweight solution for this.

Thanks 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
PiotrCzaplacommented, May 8, 2021

Tried both variants, but still an issue with Chrome:

args: [
    // '--host-rules="MAP 127.0.0.1 dev.smthg'
    '--host-resolver-rules="MAP 127.0.0.1 dev.smthg'
]

I have Github Actions workflow and inside it I’m updating the etc/hosts file manually. Firefox is working fine, but looks like Chrome doesn’t see this file at all… The same happens locally with Windows 10

I think the code doesn’t work due to additional quote inside of the rule.

 args: [ '--host-resolver-rules="MAP 127.0.0.1 dev.smthg"' ]  // the double quote cause the issue

This argument should looks as follows in playwright:

args: [ '--host-resolver-rules=MAP 127.0.0.1 dev.smthg' ]
1reaction
philschcommented, Oct 7, 2022
const browser = await chromium.launch({
  args: [ '--host-resolver-rules=MAP * 127.0.0.1' ]
});

Worked for me to direct all domains to be resolved by my local machine.

For example this loaded the content from my local running webserver:

const page = await browser.newPage();
const response = await page.goto('http://test.server.local/');
const text = await response.text();
expect(text).toContain('...')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Browser with its own hosts file? - Super User
Any solution which simply rewrites the host file doesn't work because of the requirement to run multiple browser windows at the same time...
Read more >
DNS attributes for your VPC - Amazon Virtual Private Cloud
You can resolve the Private IP DNS name (IPv4 only) hostnames of other instances in other VPCs as long as the instances are...
Read more >
Understanding and Using The Hosts File-Beginners Guide
The hosts file performs a name to IP address mapping and was used on early computer networks for name resolution before DNS was...
Read more >
What is DNS? How Domain Name System works - TechTarget
For example, if someone types "example.com" into a web browser, a server behind the ... provide the information necessary to connect users to...
Read more >
Altering hostname-IP mapping within a browser - Server Fault
In Chromium, you can use the --host-resolver-rules option to specify a list of mappings. For example:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found