0

We have some QUnit javascript tests running in Visual Studio using the Chutzpah test adapter. Everything was working fine until we changed our api (the one being tested by the js files) recently, and added some validations over the UserAgent http header. When I tried to update the tests to change/mock the user agent I realized it was not directly possible even by overriding the default browser property.

After a few days of scavenging, I finally found what exactly is happening. Chutzpah is creating a phantomjs page object for the test files to run on. This is being done on a base javascript file (chutzpahRunner.js) located at the Chutzpah adapter installation path. These are the last lines on the file, that effectively start the tests:

...
// Allows local files to make ajax calls to remote urls
page.settings.localToRemoteUrlAccessEnabled = true; //(default false) 

// Stops all security (for example you can access content in other domain IFrames)
page.settings.webSecurityEnabled = false; //(default true)

page.open(testFile, pageOpenHandler);
...

Phatomjs supports changing the user agent header by specifying it in the page settings object. If I edit this chutzpahRunner.js file in my machine, and manually set the user agent there, like this:

page.settings.userAgent = "MyCustomUserAgent";

My tests start to work again. The problem is that this is not in the project itself, and thus cannot be shared with the rest of the team.

Is it possible to change the properties of the phantomjs objects created by Chutzpah to run the tests? I'd like to either change them from inside my own tests, or from another script file I could embed on the pipeline.

Community
  • 1
  • 1
julealgon
  • 7,072
  • 3
  • 32
  • 77

1 Answers1

1

Without a code change in Chutzpah it is not possible to set those properties on the PhantomJS object. Please file an issue at https://github.com/mmanela/chutzpah asking for this functionality and then fork/patch Chutzpah to add it (or wait for a developer on the project to hopefully get to this).

Update: I pushed a fix for this issue. Once this is released you can use the following in a Chutzpah.json file:

{
  "userAgent": "myUserAgent"
}
Matthew Manela
  • 16,572
  • 3
  • 64
  • 66
  • Hey there Matthew. If you read my post carefully, you will notice I tried the override method already, and it does not work, unfortunatelly. As for your first option, I'll see if I have a little time to describe the problem as an issue in the project, but I'll certainly not be able to patch it myself since my managers would not approve that I spend my job time on it, and I don't have a working PC at home. Today I made my last atempt at this, by trying to replace the default base template html to generate a modified test harness, but I finally realized it was in vain... – julealgon Oct 20 '14 at 23:23
  • since the file I had to modify was being passed directly to the phantom executable, and I had no access to that at all. For the moment, we have decided to just delete the tests that needed to rely on the User-Agent header and move on. I was quite sad at the end of the process though... I think I spent a lot of time to understand the whole process (I know very little about javascript to begin with, and it was a teammate that left the company that setup these tests), but in the end there was nothing feasible I could do. Since you say it's impossible without code changes, I'm closing this. – julealgon Oct 20 '14 at 23:29
  • When I tested that code snippet I put in my answer it was able to change the userAgent property. I didn't test though if it sends it to the server. Did you confirm that it doesn't? – Matthew Manela Oct 20 '14 at 23:58
  • Yeah, it does change the property as expected, but every request still has the default User-Agent sent. Phantom does not respect it or there is some other detail in there that just doesn't work. The only way I found that was 100% reliable was changing that property on the page object, unfortunatelly. – julealgon Oct 21 '14 at 01:10
  • Ok, then best option is file your issues on the Chutzpah github page and hopefully I can get to it. – Matthew Manela Oct 21 '14 at 01:56