|
| 1 | +/* |
| 2 | + * Copyright 2002-2007 the original author or authors. |
| 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 | +package org.suren.autotest.web.framework.selenium; |
| 18 | + |
| 19 | +import static org.suren.autotest.web.framework.settings.DriverConstants.DRIVER_CHROME; |
| 20 | +import static org.suren.autotest.web.framework.settings.DriverConstants.DRIVER_FIREFOX; |
| 21 | +import static org.suren.autotest.web.framework.settings.DriverConstants.DRIVER_IE; |
| 22 | +import static org.suren.autotest.web.framework.settings.DriverConstants.DRIVER_OPERA; |
| 23 | +import static org.suren.autotest.web.framework.settings.DriverConstants.DRIVER_PHANTOM_JS; |
| 24 | +import static org.suren.autotest.web.framework.settings.DriverConstants.DRIVER_SAFARI; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.util.Iterator; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Properties; |
| 30 | + |
| 31 | +import org.openqa.selenium.Proxy; |
| 32 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 33 | +import org.openqa.selenium.firefox.FirefoxProfile; |
| 34 | +import org.openqa.selenium.ie.InternetExplorerDriver; |
| 35 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | +import org.suren.autotest.web.framework.settings.DriverConstants; |
| 39 | +import org.suren.autotest.web.framework.util.BrowserUtil; |
| 40 | + |
| 41 | +/** |
| 42 | + * 浏览器配置 |
| 43 | + * @author suren |
| 44 | + * @date 2017年5月16日 下午9:18:03 |
| 45 | + */ |
| 46 | +public class CapabilityConfig |
| 47 | +{ |
| 48 | + private static final Logger logger = LoggerFactory.getLogger(CapabilityConfig.class); |
| 49 | + |
| 50 | + private Map<String, DesiredCapabilities> engineCapMap; |
| 51 | + private Properties enginePro; //引擎参数集合 |
| 52 | + |
| 53 | + /** |
| 54 | + * @param engineCapMap 用于保存浏览器配置的返回结果 |
| 55 | + * @param enginePro 引擎配置集合 |
| 56 | + */ |
| 57 | + public CapabilityConfig(Map<String, DesiredCapabilities> engineCapMap, |
| 58 | + Properties enginePro) |
| 59 | + { |
| 60 | + this.engineCapMap = engineCapMap; |
| 61 | + this.enginePro = enginePro; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * 加载所有支持浏览器的配置 |
| 66 | + */ |
| 67 | + public void config() |
| 68 | + { |
| 69 | + firefox(); |
| 70 | + |
| 71 | + chrome(); |
| 72 | + |
| 73 | + ie(); |
| 74 | + |
| 75 | + { |
| 76 | + String proFile = System.getProperty("firefox.profile", null); |
| 77 | + FirefoxProfile profile = new FirefoxProfile(proFile != null ? new File(proFile) : null); |
| 78 | + fireFoxPreSet(profile); |
| 79 | + } |
| 80 | + |
| 81 | + { |
| 82 | + DesiredCapabilities capability = DesiredCapabilities.safari(); |
| 83 | + engineCapMap.put(DRIVER_SAFARI, capability); |
| 84 | + } |
| 85 | + |
| 86 | + { |
| 87 | + DesiredCapabilities capability = DesiredCapabilities.operaBlink(); |
| 88 | + engineCapMap.put(DRIVER_OPERA, capability); |
| 89 | + } |
| 90 | + |
| 91 | + { |
| 92 | + DesiredCapabilities capability = DesiredCapabilities.phantomjs(); |
| 93 | + engineCapMap.put(DRIVER_PHANTOM_JS, capability); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * 火狐浏览器配置 |
| 99 | + */ |
| 100 | + private void firefox() |
| 101 | + { |
| 102 | + DesiredCapabilities capability = DesiredCapabilities.firefox(); |
| 103 | + capability.setCapability("marionette", true); |
| 104 | + engineCapMap.put(DRIVER_FIREFOX, capability); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * 谷歌浏览器 |
| 109 | + * chrome://version/ |
| 110 | + */ |
| 111 | + private void chrome() |
| 112 | + { |
| 113 | + DesiredCapabilities capability = DesiredCapabilities.chrome(); |
| 114 | + |
| 115 | + ChromeOptions options = new ChromeOptions(); |
| 116 | + Iterator<Object> chromeKeys = enginePro.keySet().iterator(); |
| 117 | + Proxy proxy = new Proxy(); |
| 118 | + while(chromeKeys.hasNext()) |
| 119 | + { |
| 120 | + String key = chromeKeys.next().toString(); |
| 121 | + if(!key.startsWith("chrome")) |
| 122 | + { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if(key.startsWith("chrome.args")) |
| 127 | + { |
| 128 | + String arg = key.replace("chrome.args.", "") + "=" + enginePro.getProperty(key); |
| 129 | + if(arg.endsWith("=")) |
| 130 | + { |
| 131 | + arg = arg.substring(0, arg.length() - 1); |
| 132 | + } |
| 133 | + options.addArguments(arg); |
| 134 | + logger.info(String.format("chrome arguments : [%s]", arg)); |
| 135 | + } |
| 136 | + else if(key.startsWith("chrome.cap.proxy.http")) |
| 137 | + { |
| 138 | + String val = enginePro.getProperty(key); |
| 139 | + |
| 140 | + proxy.setHttpProxy(val); |
| 141 | + } |
| 142 | + else if(key.startsWith("chrome.cap.proxy.ftp")) |
| 143 | + { |
| 144 | + String val = enginePro.getProperty(key); |
| 145 | + |
| 146 | + proxy.setFtpProxy(val); |
| 147 | + } |
| 148 | + else if(key.startsWith("chrome.cap.proxy.socks")) |
| 149 | + { |
| 150 | + String val = enginePro.getProperty(key); |
| 151 | + |
| 152 | + proxy.setSocksProxy(val); |
| 153 | + } |
| 154 | + else if(key.startsWith("chrome.cap.proxy.socks.username")) |
| 155 | + { |
| 156 | + String val = enginePro.getProperty(key); |
| 157 | + |
| 158 | + proxy.setSocksUsername(val); |
| 159 | + } |
| 160 | + else if(key.startsWith("chrome.cap.proxy.socks.password")) |
| 161 | + { |
| 162 | + String val = enginePro.getProperty(key); |
| 163 | + |
| 164 | + proxy.setSocksPassword(val); |
| 165 | + } |
| 166 | + else if(key.startsWith("chrome.binary")) |
| 167 | + { |
| 168 | + options.setBinary(enginePro.getProperty(key)); |
| 169 | + } |
| 170 | + } |
| 171 | + capability.setCapability("proxy", proxy); |
| 172 | + capability.setCapability(ChromeOptions.CAPABILITY, options); |
| 173 | + |
| 174 | + engineCapMap.put(DRIVER_CHROME, capability); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * ie浏览器 |
| 179 | + */ |
| 180 | + private void ie() |
| 181 | + { |
| 182 | + String initialUrl = enginePro.getProperty(DriverConstants.INITIAL_URL, |
| 183 | + "http://surenpi.com"); |
| 184 | + |
| 185 | + DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); |
| 186 | + capability.setCapability( |
| 187 | + InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); |
| 188 | + capability.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, initialUrl); |
| 189 | + capability.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, false); |
| 190 | + engineCapMap.put(DRIVER_IE, capability); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * 设定firefox首选项 |
| 195 | + * @param profile |
| 196 | + */ |
| 197 | + private void fireFoxPreSet(FirefoxProfile profile) |
| 198 | + { |
| 199 | + BrowserUtil browserUtil = new BrowserUtil(); |
| 200 | + Map<String, Boolean> boolMap = browserUtil.getFirefoxPreBoolMap(); |
| 201 | + Iterator<String> boolIt = boolMap.keySet().iterator(); |
| 202 | + while(boolIt.hasNext()) |
| 203 | + { |
| 204 | + String key = boolIt.next(); |
| 205 | + |
| 206 | + profile.setPreference(key, boolMap.get(key)); |
| 207 | + } |
| 208 | + |
| 209 | + Map<String, Integer> intMap = browserUtil.getFirefoxPreIntMap(); |
| 210 | + Iterator<String> intIt = intMap.keySet().iterator(); |
| 211 | + while(intIt.hasNext()) |
| 212 | + { |
| 213 | + String key = intIt.next(); |
| 214 | + |
| 215 | + profile.setPreference(key, intMap.get(key)); |
| 216 | + } |
| 217 | + |
| 218 | + Map<String, Integer> strMap = browserUtil.getFirefoxPreIntMap(); |
| 219 | + Iterator<String> strIt = intMap.keySet().iterator(); |
| 220 | + while(strIt.hasNext()) |
| 221 | + { |
| 222 | + String key = strIt.next(); |
| 223 | + |
| 224 | + profile.setPreference(key, strMap.get(key)); |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments