/testing/selenium-core/scripts/selenium-browserdetect.js
JavaScript | 153 lines | 111 code | 20 blank | 22 comment | 34 complexity | ed38f4726ca7227b7da20a77f3845579 MD5 | raw file
1/* 2 * Copyright 2004 ThoughtWorks, Inc 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18// Although it's generally better web development practice not to use 19// browser-detection (feature detection is better), the subtle browser 20// differences that Selenium has to work around seem to make it 21// necessary. Maybe as we learn more about what we need, we can do this in 22// a more "feature-centric" rather than "browser-centric" way. 23 24var BrowserVersion = function() { 25 this.name = navigator.appName; 26 27 if (navigator.userAgent.indexOf('Mac OS X') != -1) { 28 this.isOSX = true; 29 } 30 31 if (navigator.userAgent.indexOf('Windows NT 6') != -1) { 32 this.isVista = true; 33 } 34 35 if (window.opera != null) { 36 this.browser = BrowserVersion.OPERA; 37 this.isOpera = true; 38 return; 39 } 40 41 var _getQueryParameter = function(searchKey) { 42 var str = location.search.substr(1); 43 if (str == null) return null; 44 var clauses = str.split('&'); 45 for (var i = 0; i < clauses.length; i++) { 46 var keyValuePair = clauses[i].split('=', 2); 47 var key = unescape(keyValuePair[0]); 48 if (key == searchKey) { 49 return unescape(keyValuePair[1]); 50 } 51 } 52 return null; 53 }; 54 55 var self = this; 56 57 var checkChrome = function() { 58 var loc = window.document.location.href; 59 try { 60 loc = window.top.document.location.href; 61 if (/^chrome:\/\//.test(loc)) { 62 self.isChrome = true; 63 } else { 64 self.isChrome = false; 65 } 66 } catch (e) { 67 // can't see the top (that means we might be chrome, but it's impossible to be sure) 68 self.isChromeDetectable = "no, top location couldn't be read in this window"; 69 if (_getQueryParameter('thisIsChrome')) { 70 self.isChrome = true; 71 } else { 72 self.isChrome = false; 73 } 74 } 75 76 77 } 78 79 80 81 if (this.name == "Microsoft Internet Explorer") { 82 this.browser = BrowserVersion.IE; 83 this.isIE = true; 84 try { 85 if (window.top.SeleniumHTARunner && window.top.document.location.pathname.match(/.hta$/i)) { 86 this.isHTA = true; 87 } 88 } catch (e) { 89 this.isHTADetectable = "no, top location couldn't be read in this window"; 90 if (_getQueryParameter('thisIsHTA')) { 91 self.isHTA = true; 92 } else { 93 self.isHTA = false; 94 } 95 } 96 if (navigator.appVersion.match(/MSIE 6.0/)) { 97 this.isIE6 = true; 98 } 99 if ("0" == navigator.appMinorVersion) { 100 this.preSV1 = true; 101 if (this.isIE6) { 102 this.appearsToBeBrokenInitialIE6 = true; 103 } 104 } 105 return; 106 } 107 108 if (navigator.userAgent.indexOf('Safari') != -1) { 109 this.browser = BrowserVersion.SAFARI; 110 this.isSafari = true; 111 this.khtml = true; 112 return; 113 } 114 115 if (navigator.userAgent.indexOf('Konqueror') != -1) { 116 this.browser = BrowserVersion.KONQUEROR; 117 this.isKonqueror = true; 118 this.khtml = true; 119 return; 120 } 121 122 if (navigator.userAgent.indexOf('Firefox') != -1) { 123 this.browser = BrowserVersion.FIREFOX; 124 this.isFirefox = true; 125 this.isGecko = true; 126 var result = /.*Firefox\/([\d\.]+).*/.exec(navigator.userAgent); 127 if (result) { 128 this.firefoxVersion = result[1]; 129 } 130 checkChrome(); 131 return; 132 } 133 134 if (navigator.userAgent.indexOf('Gecko') != -1) { 135 this.browser = BrowserVersion.MOZILLA; 136 this.isMozilla = true; 137 this.isGecko = true; 138 checkChrome(); 139 return; 140 } 141 142 this.browser = BrowserVersion.UNKNOWN; 143} 144 145BrowserVersion.OPERA = "Opera"; 146BrowserVersion.IE = "IE"; 147BrowserVersion.KONQUEROR = "Konqueror"; 148BrowserVersion.SAFARI = "Safari"; 149BrowserVersion.FIREFOX = "Firefox"; 150BrowserVersion.MOZILLA = "Mozilla"; 151BrowserVersion.UNKNOWN = "Unknown"; 152 153var browserVersion = new BrowserVersion();