wkeeling / selenium-wire

Extends Selenium's Python bindings to give you the ability to inspect requests made by the browser.
MIT License
1.86k stars 242 forks source link

AWS Device Farm with Remote WebDriver #501

Open 1UpGus opened 2 years ago

1UpGus commented 2 years ago

Hi Will,

Before anything I want to thank you for your time and your hard work on this awesome tool. It really makes Selenium complete. Tks And also I'd like to say that I've read almost all the issues listed here trying to solve this before create a new one.

In my project, to be able to access a testing environment website I need to send a header request, otherwise I get a 404 error. I'm running Selenium-wire using Jenkins on a server and running the browser on AWS Device Farm. The thing is, some sites don't need the header and I can access normally, and for those the next config are working fine:

if browser_name == "chrome":
        options = webdriver.ChromeOptions()
        options.add_argument("--ignore-certificate-errors")
        options.add_argument("--no-sandbox")
        devicefarm_client = boto3.client("devicefarm", region_name="us-west-2")
        testgrid_url_response = devicefarm_client.create_test_grid_url(
            projectArn="arn:aws:devicefarm:us-west-2:111122223333:testgrid-project:123e4567-e89b-12d3-a456-426655440000",  # < exemple project's Amazon Resource Name (ARN)
            expiresInSeconds=300,
        )
        desired_capabilities = DesiredCapabilities.CHROME
        desired_capabilities["platform"] = "windows"
        driver = webdriver.Remote(
            testgrid_url_response["url"], desired_capabilities, options=options,
            seleniumwire_options={'auto_config': False, 'addr': '127.0.0.1'}
        )
        driver.set_window_size(1920, 1080)
        driver.implicitly_wait(30)

        driver.get("...")

For the site where I need to use the header I first start just adding the 'interceptor' function, and then I tried many other things:

elif browser_name == "chrome_1":
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--ignore-certificate-errors")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument('--proxy-server="IP-of-the-machine-running-Jenkins":8087')        
        devicefarm_client = boto3.client("devicefarm", region_name="us-west-2")
        testgrid_url_response = devicefarm_client.create_test_grid_url(
            projectArn="arn:aws:devicefarm:us-west-2:111122223333:testgrid-project:123e4567-e89b-12d3-a456-426655440000",  # < exemple project's Amazon Resource Name (ARN)
            expiresInSeconds=300,
        )
        desired_capabilities = DesiredCapabilities.CHROME
        desired_capabilities["platform"] = "windows"
        driver = webdriver.Remote(
            testgrid_url_response["url"], desired_capabilities, options=chrome_options, 
            seleniumwire_options={'auto_config': False, 'addr': '127.0.0.1', 'port': 8087} # < Here I've tried 0.0.0.0, IP of Jenkins machine, etc...
        )
        driver.set_window_size(1920, 1080)
        driver.implicitly_wait(30)

        def interceptor(request):
            request.headers['x-abc-abcdef'] = 'the-header-value'
        driver.request_interceptor = interceptor

        driver.get("...")

Plus, locally the 'interceptor' function with header work just fine in granting me access.

If you -or anybody- could throw some light here I'd be immensely grateful!

Thanks!

wkeeling commented 2 years ago

Thanks @1UpGus for raising this.

The configuration for chome_1 looks ok as far as I can tell. Do you see anything if you print out driver.requests after the driver.get(...) call? e.g.

driver.get("...")
print(f'Captured requests: {driver.requests}')
1UpGus commented 2 years ago

With that config I am getting ERR_TIMED_OUT straight away. So I commented out the chrome options setting the proxy and set the 'addr' with the IP of the machine running Jenkins (which is running SW):

elif browser_name == "chrome_1":
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--ignore-certificate-errors")
        chrome_options.add_argument("--no-sandbox")
        # chrome_options.add_argument('--proxy-server="IP-of-the-machine-running-Jenkins":8087')        
        devicefarm_client = boto3.client("devicefarm", region_name="us-west-2")
        testgrid_url_response = devicefarm_client.create_test_grid_url(
            projectArn="arn:aws:devicefarm:us-west-2:111122223333:testgrid-project:123e4567-e89b-12d3-a456-426655440000",  # < exemple project's Amazon Resource Name (ARN)
            expiresInSeconds=300,
        )
        desired_capabilities = DesiredCapabilities.CHROME
        desired_capabilities["platform"] = "windows"
        driver = webdriver.Remote(
            testgrid_url_response["url"], desired_capabilities, options=chrome_options, 
            seleniumwire_options={'auto_config': False, 'addr': '"IP-of-the-machine-running-Jenkins"', 'port': 8087}
        )
        driver.set_window_size(1920, 1080)
        driver.implicitly_wait(30)

        def interceptor(request):
            request.headers['x-abc-abcdef'] = 'the-header-value'
        driver.request_interceptor = interceptor

        driver.get("...")

I can see that the header is not there

image

Tbh Idk what I am missing

Anticope12 commented 2 years ago

Following an update :)