garris / BackstopJS

Catch CSS curve balls.
http://backstopjs.org
MIT License
6.66k stars 602 forks source link

How to pass the userAgent value in the backstop.json file #1483

Open judy-akers opened 1 year ago

judy-akers commented 1 year ago

Our company has recently installed CloudFlare on our platform to set rate limiting. I need to be able to set a custom userAgent value so we can identify the backstop tests as good traffic vs. a bad actor trying to do something malicious.

We currently do not commit (via .gitignore) the backstop.json file and /backstop_data/ folder; we do commit a default.backstop.json file to be saved with our various github repositories that has the various scenarios for each site.

Our default.backup.json files already has a "onReadyScript": "puppet/onReady.js". I tried adding it with "onReadyScript": "async (chromy, scenario) => { await chromy.userAgent('Your User-Agent String'); }" in the main body of the script - it did not recognize or find chromy.on.

I did try to include both the puppet/onReady.js and the await chromy.userAgent arguments in a single onReadyScript using "onReadyScript": "chromy.on('ready', function() { action1(); action2(); });" format: My attempts were:

I have also tried setting "userAgent": "Your User-Agent String" in several places: in the viewports section, scenarios, and in the engineOptions.

None of these have worked for me.The only way I have successfully set userAgent is via onReady.js changes (but we do not commit that file to our repositories). I need help to find a way to pass a userAgent value in the backstop.json file. Thank you in advance.

garris commented 1 year ago

Hi Judy, The chomy version of backstop is quite old -- I recommend you use a more recent version and use either puppeteer or playwright engines. That will be more reliable for you.

Also -- I recommend using a JS config file if you want to create dynamic configs -- but first use a static value instead of trying to read data from a file -- just to make sure everything is working.

more info on JS configs here... https://github.com/garris/BackstopJS#working-with-your-config-file

judy-akers commented 1 year ago

My attempts to use chromy.on was provided by https://chat.openai.com/ answers to my questions on BackstopJS. I am currently using BackstopJS v6.2.1 and my scripts are using Puppeteer.

So my fix was to it add to the engineOptions args property:

"engineOptions": { "args": ["--no-sandbox", "--user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 - [your-user-agent-value['"] }, Note the single quotes around the value to be set as full user-agent - browser values and your-user-agent-value.

It was necessary to include the full description of the browsers with the because without that (just using "--user-agent-"[your-user-agent-value['") the backstop run gave a warning message: WARNING! CHROME VERSION 62 OR GREATER IS REQUIRED. PLEASE UPDATE YOUR CHROME APP!

This is an example of browser values. Your Milage May Vary for the actual browser values.