Sunday, May 12, 2024
171
rated 0 times [  175] [ 4]  / answers: 1 / hits: 78547  / 15 Years ago, thu, august 20, 2009, 12:00:00

I'm looking for a way to programmatically change navigator.userAgent on the fly. In my failed attempt to get an automated javascript unit tester, I gave up and attempted to begin using fireunit. Immediately, I've slammed into one of the walls of using an actual browser for javascript testing.



Specifically, I need to change navigator.userAgent to simulate a few hundred userAgent strings to ensure proper detection and coverage on a given function. navigator.userAgent is readonly, so I seem stuck! How can I mock navigator.userAgent? User Agent Switcher (plugin) can switch FF's useragent, but can I do it within javascript?


More From » unit-testing

 Answers
12

Try:



navigator.__defineGetter__('userAgent', function(){
return 'foo' // customized user agent
});

navigator.userAgent; // 'foo'


Tried it in FF2 and FF3.


[#98863] Tuesday, August 18, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neo

Total Points: 117
Total Questions: 100
Total Answers: 95

Location: Albania
Member since Fri, Jan 28, 2022
2 Years ago
neo questions
Mon, Jun 28, 21, 00:00, 3 Years ago
Wed, Sep 30, 20, 00:00, 4 Years ago
Thu, Aug 20, 20, 00:00, 4 Years ago
Sun, Aug 2, 20, 00:00, 4 Years ago
;