Best Selenium code snippet using org.openqa.selenium.MutableCapabilities.setCapability
Source:DriverType.java
...47 },48 CHROME {49 public MutableCapabilities getDesiredCapabilities(Proxy proxySettings) {50 DesiredCapabilities capabilities = DesiredCapabilities.chrome();51 capabilities.setCapability("chrome.switches", Arrays.asList("--no-default-browser-check"));52 HashMap<String, Object> chromePreferences = new HashMap<String, Object>();53 chromePreferences.put("profile.password_manager_enabled", "false");54 chromePreferences.put("profile.default_content_settings.popups", 0);55 chromePreferences.put("download.default_directory", Constant.getDownloadsPath());56 chromePreferences.put("ssl.error_override_allowed", true);57 //chromePreferences.put("useAutomationExtension", false);58 59 ChromeOptions options = new ChromeOptions();60 //options.addArguments("start-maximized");61 //TODO Adding this line in attempt to solve the browser spawning issue on jenkins 62 options.addArguments("--no-sandbox");63 64 options.setExperimentalOption("useAutomationExtension", false);65 options.setExperimentalOption("prefs", chromePreferences);66 capabilities.setCapability(ChromeOptions.CAPABILITY, options);67 /*capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);*/68 //capabilities.setCapability("chrome.prefs", chromePreferences);69 return addProxySettings(capabilities, proxySettings);70 }71 public RemoteWebDriver getWebDriverObject(MutableCapabilities capabilities) {72 if(System.getProperty("webdriver.chrome.driver")==null){73 System.setProperty("webdriver.chrome.driver", Constant.getChromeDriverLocation("win"));74 }75 return new ChromeDriver(capabilities);76 }77 },78 IE {79 public MutableCapabilities getDesiredCapabilities(Proxy proxySettings) {80 DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();81 capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);82 capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);83 capabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, true);84 capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);85 capabilities.setCapability("requireWindowFocus", true);86 return addProxySettings(capabilities, proxySettings);87 }88 public RemoteWebDriver getWebDriverObject(MutableCapabilities capabilities) {89 if(System.getProperty("webdriver.ie.driver")==null){90 System.setProperty("webdriver.ie.driver", Constant.getIEDriverLocation());91 }92 InternetExplorerOptions options = new InternetExplorerOptions();93 options.merge(capabilities);94 return new InternetExplorerDriver(options);95 }96 },97 EDGE {98 public MutableCapabilities getDesiredCapabilities(Proxy proxySettings) {99 DesiredCapabilities capabilities = DesiredCapabilities.edge();100 return addProxySettings(capabilities, proxySettings);101 }102 public RemoteWebDriver getWebDriverObject(MutableCapabilities capabilities) {103 if(System.getProperty("webdriver.edge.driver")==null){104 System.setProperty("webdriver.edge.driver", Constant.getEdgeDriverLocation());105 }106 EdgeOptions options = new EdgeOptions();107 options.merge(capabilities);108 return new EdgeDriver(options);109 }110 },111 SAFARI {112 public MutableCapabilities getDesiredCapabilities(Proxy proxySettings) {113 DesiredCapabilities capabilities = DesiredCapabilities.safari();114 capabilities.setCapability("safari.cleanSession", true);115 return addProxySettings(capabilities, proxySettings);116 }117 public RemoteWebDriver getWebDriverObject(MutableCapabilities capabilities) {118 return new SafariDriver(capabilities);119 }120 },121 OPERA {122 public MutableCapabilities getDesiredCapabilities(Proxy proxySettings) {123 DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();124 return addProxySettings(capabilities, proxySettings);125 }126 public RemoteWebDriver getWebDriverObject(MutableCapabilities capabilities) {127 return new OperaDriver(capabilities);128 }129 },130 PHANTOMJS {131 public MutableCapabilities getDesiredCapabilities(Proxy proxySettings) {132 DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();133 final List<String> cliArguments = new ArrayList<String>();134 cliArguments.add("--web-security=false");135 cliArguments.add("--ssl-protocol=any");136 cliArguments.add("--ignore-ssl-errors=true");137 capabilities.setCapability("phantomjs.cli.args", applyPhantomJSProxySettings(cliArguments, proxySettings));138 capabilities.setCapability("takesScreenshot", true);139 return capabilities;140 }141 public RemoteWebDriver getWebDriverObject(MutableCapabilities capabilities) {142 return new PhantomJSDriver(capabilities);143 }144 },145 HTMLUNIT {146 public MutableCapabilities getDesiredCapabilities(Proxy proxySettings) {147 DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();148 /*final List<String> cliArguments = new ArrayList<String>();149 cliArguments.add("--web-security=false");150 cliArguments.add("--ssl-protocol=any");151 cliArguments.add("--ignore-ssl-errors=true");152 capabilities.setCapability("phantomjs.cli.args", applyPhantomJSProxySettings(cliArguments, proxySettings));153 capabilities.setCapability("takesScreenshot", true);*/154 return capabilities;155 }156 public WebDriver getWebDriverObject(MutableCapabilities capabilities) {157 return new HtmlUnitDriver(capabilities);158 }159 },160 CHROME_HEADLESS {161 public MutableCapabilities getDesiredCapabilities(Proxy proxySettings) {162 //NEWER163 DesiredCapabilities capabilities = DesiredCapabilities.chrome();164 capabilities.setCapability("chrome.switches", Arrays.asList("--no-default-browser-check"));165 HashMap<String, Object> chromePreferences = new HashMap<String, Object>();166 chromePreferences.put("profile.password_manager_enabled", "false");167 chromePreferences.put("profile.default_content_settings.popups", 0);168 chromePreferences.put("download.default_directory", Constant.getDownloadsPath());169 chromePreferences.put("ssl.error_override_allowed", true);170 171 ChromeOptions options = new ChromeOptions();172 options.addArguments("--headless");173 174 //TODO Adding this line in attempt to solve the browser spawning issue on jenkins 175 options.addArguments("--no-sandbox");176 177 options.setExperimentalOption("useAutomationExtension", false);178 options.setExperimentalOption("prefs", chromePreferences);179 capabilities.setCapability(ChromeOptions.CAPABILITY, options);180 /*capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);*/181 //capabilities.setCapability("chrome.prefs", chromePreferences);182 return addProxySettings(capabilities, proxySettings);183 184 185 186 //OLDER187 /*HashMap<String, Object> chromePreferences = new HashMap<String, Object>();188 chromePreferences.put("profile.password_manager_enabled", "false");189 chromePreferences.put("profile.default_content_settings.popups", 0);190 chromePreferences.put("download.default_directory", Constant.getDownloadsPath());191 chromePreferences.put("ssl.error_override_allowed", true);192 193 ChromeOptions chromeOptions = new ChromeOptions();194 chromeOptions.addArguments("--headless");195 DesiredCapabilities capabilities = DesiredCapabilities.chrome();196 capabilities.setCapability("chrome.switches", Arrays.asList("--no-default-browser-check"));197 capabilities.setCapability("chrome.prefs", chromePreferences);198 capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);199 return addProxySettings(capabilities, proxySettings);*/200 }201 public RemoteWebDriver getWebDriverObject(MutableCapabilities capabilities) {202 if(System.getProperty("webdriver.chrome.driver")==null){203 System.setProperty("webdriver.chrome.driver", Constant.getChromeDriverLocation("win"));204 }205 ChromeOptions options = new ChromeOptions();206 options.merge(capabilities);207 return new ChromeDriver(options);208 }209 };210 protected MutableCapabilities addProxySettings(MutableCapabilities capabilities, Proxy proxySettings) {211 if (null != proxySettings) {212 capabilities.setCapability(PROXY, proxySettings);213 }214 return capabilities;215 }216 protected List<String> applyPhantomJSProxySettings(List<String> cliArguments, Proxy proxySettings) {217 if (null == proxySettings) {218 cliArguments.add("--proxy-type=none");219 } else {220 cliArguments.add("--proxy-type=http");221 cliArguments.add("--proxy=" + proxySettings.getHttpProxy());222 }223 return cliArguments;224 }225}...
Source:BrowserSelector.java
...31 {32 MutableCapabilities sauceOptions = new MutableCapabilities();33 ChromeOptions caps = new ChromeOptions();34 caps.setExperimentalOption("w3c",true);35 caps.setCapability("platformName","Windows 10");36 caps.setCapability("browserVersion","77.0");37 caps.setCapability("sauce:options",sauceOptions);38 try39 {40 driver = new RemoteWebDriver(new URL(URL), caps);41 }42 catch (MalformedURLException e)43 {44 e.printStackTrace();45 }46 }47 else if(browser.equalsIgnoreCase("IE"))48 {49 DesiredCapabilities caps = DesiredCapabilities.internetExplorer();50 caps.setCapability("platform","Windows 10");51 caps.setCapability("version","11");52 try53 {54 driver = new RemoteWebDriver(new URL(URL), caps);55 }56 catch (MalformedURLException e)57 {58 e.printStackTrace();59 }60 }61 else if (browser.equalsIgnoreCase("FireFox"))62 {63 DesiredCapabilities caps = DesiredCapabilities.firefox();64 caps.setCapability("platform","Windows 7");65 caps.setCapability("version","56");66 caps.setCapability("name","Testing on Firefox 56");67 try68 {69 driver = new RemoteWebDriver(new URL(URL),caps);70 }71 catch (MalformedURLException e)72 {73 e.printStackTrace();74 }75 }76 else if(browser.equalsIgnoreCase("Safari"))77 {78 DesiredCapabilities caps=DesiredCapabilities.safari();79 caps.setCapability("platform","os x 10.10");80 caps.setCapability("version","8.0");81 try82 {83 driver = new RemoteWebDriver(new URL(URL), caps);84 }85 catch (MalformedURLException e)86 {87 e.printStackTrace();88 }89 }90 else if(browser.equalsIgnoreCase("edge"))91 {92 DesiredCapabilities caps = DesiredCapabilities.edge();93 caps.setCapability("platform", "window 10");94 caps.setCapability("version", "16.16299");95 try96 {97 driver = new RemoteWebDriver(new URL(URL), caps);98 }99 catch (MalformedURLException e)100 {101 e.printStackTrace();102 }103 }104 else105 {106 System.out.println("wrong browser name or empty:"+browser);107 }108 }109 else110 {111 String browser = loadProps.getProperty("browser");112 if(browser.equalsIgnoreCase("chrome")){113 System.setProperty("webdriver.chrome.driver","src\\test\\Resources\\BrowserDriver\\chromedriver77.exe");114 driver = new ChromeDriver();115 }else if(browser.equalsIgnoreCase("firefox")){116 System.setProperty("webdriver.gecko.driver","src\\test\\Resources\\BrowserDriver\\geckodriver.exe");117 driver = new FirefoxDriver();118 }else if(browser.equalsIgnoreCase("edge")){119 System.setProperty("webdriver.edge.driver","src\\test\\Resources\\BrowserDriver\\IEDriverServer.exe");120 driver = new EdgeDriver();121 }122 }123 }124}125 /*If Sauce Lab is true...126 if (SAUCE_LAB) {127 System.out.println("Running in SauceLab......... with browser" + browser);128 if (browser.equalsIgnoreCase("Chrome"))129 {130 MutableCapabilities sauceOptions = new MutableCapabilities();131 ChromeOptions browserOptions = new ChromeOptions();132 browserOptions.setExperimentalOption("w3c", true);133 browserOptions.setCapability("platformName", "Windows 10");134 browserOptions.setCapability("browserVersion", "76.0");135 browserOptions.setCapability("sauce:options", sauceOptions);136 /*DesiredCapabilities caps = DesiredCapabilities.chrome();137 caps.setCapability("platform", "Windows 10");138 caps.setCapability("version", "76.0");*/139 /*try {140 driver = new RemoteWebDriver(new URL(URL), browserOptions);141 } catch (MalformedURLException e) {142 e.printStackTrace();143 }144 }145 else if (browser.equalsIgnoreCase("IE"))146 {147 MutableCapabilities sauceOptions = new MutableCapabilities();148 InternetExplorerOptions browserOptions = new InternetExplorerOptions();149 browserOptions.setCapability("platformName", "Windows 10");150 browserOptions.setCapability("browserVersion", "11.285");151 browserOptions.setCapability("sauce:options", sauceOptions);152 /*DesiredCapabilities caps = DesiredCapabilities.internetExplorer();153 caps.setCapability("platform", "Windows 10");154 caps.setCapability("Version", "11.285");*/155 /*try {156 driver = new RemoteWebDriver(new URL(URL),browserOptions);157 } catch (MalformedURLException e) {158 e.printStackTrace();159 }160 }161 else if (browser.equalsIgnoreCase("FireFox"))162 {163 MutableCapabilities sauceOptions = new MutableCapabilities();164 FirefoxOptions browserOptions = new FirefoxOptions();165 browserOptions.setCapability("platformName", "Windows 10");166 browserOptions.setCapability("browserVersion", "69.0");167 browserOptions.setCapability("sauce:options", sauceOptions);168 /*DesiredCapabilities caps = DesiredCapabilities.firefox();169 caps.setCapability("platform","Windows 10");170 caps.setCapability("version","69.0");171 caps.setCapability("name","Testing on FireFox 69.0");*/172 /*try {173 driver = new RemoteWebDriver(new URL(URL),browserOptions);174 } catch (MalformedURLException e) {175 e.printStackTrace();176 }177 }178 else if (browser.equalsIgnoreCase("safari"))179 {180 MutableCapabilities sauceOptions = new MutableCapabilities();181 SafariOptions browserOptions = new SafariOptions();182 browserOptions.setCapability("platformName", "macOS 10.14");183 browserOptions.setCapability("browserVersion", "12.0");184 browserOptions.setCapability("sauce:options", sauceOptions);185 /*DesiredCapabilities caps = DesiredCapabilities.safari();186 caps.setCapability("platform","macOS 10.14");187 caps.setCapability("version","12.0");*/188 /*try {189 driver = new RemoteWebDriver(new URL(URL),browserOptions);190 } catch (MalformedURLException e) {191 e.printStackTrace();192 }193 }194 else if (browser.equalsIgnoreCase("edge"))195 {196 MutableCapabilities sauceOptions = new MutableCapabilities();197 EdgeOptions browserOptions = new EdgeOptions();198 browserOptions.setCapability("platformName", "Windows 10");199 browserOptions.setCapability("browserVersion", "16.16299");200 browserOptions.setCapability("sauce:options", sauceOptions);201 /*DesiredCapabilities caps = DesiredCapabilities.edge();202 caps.setCapability("platform","Windows 10");203 caps.setCapability("version", "16.16299");*/204 /*try {205 driver = new RemoteWebDriver(new URL(URL),browserOptions);206 } catch (MalformedURLException e) {207 e.printStackTrace();208 }209 }210 else211 {212 System.out.println("Wrong Browser name or empty: " + browser);213 }214 }215 }216}*/...
Source:BrowserManager.java
...35 //sauce lab open In (Firefox) and create virtual machine for automation36 MutableCapabilities sauceOptions = new MutableCapabilities();37 ChromeOptions browserOptions = new ChromeOptions();38 browserOptions.setExperimentalOption("w3c", true);39 browserOptions.setCapability("platformName", "Windows 8");//start in windows 10 OS.40 browserOptions.setCapability("browserVersion", "84.0");//chrome version 84.041 browserOptions.setCapability("sauce:options", sauceOptions);42 try {43 driver = new RemoteWebDriver(new URL(URL),browserOptions);44 } catch (MalformedURLException e) {45 e.printStackTrace();46 }47 }48 //browser name match ( Firefox ) run this browser49 else if (browserName.equalsIgnoreCase("Firefox")) {50 //sauce lab open In (Firefox) and create virtual machine for automation51 MutableCapabilities sauceOptions = new MutableCapabilities();52 FirefoxOptions browserOptions = new FirefoxOptions();53 browserOptions.setCapability("platformName", "Windows 10");//start in windows 10 OS.54 browserOptions.setCapability("browserVersion", "79.0");//chrome version 79.055 browserOptions.setCapability("sauce:options", sauceOptions);56 try {//Exception handling when remote the browser57 driver = new RemoteWebDriver(new URL(URL), browserOptions);58 } catch (MalformedURLException e) {59 e.printStackTrace();60 }61 //browser name match ( InternetExplorer ) run this browser62 } else if (browserName.equalsIgnoreCase("InternetExplorer")) {63 //sauce lab open In (InternetExplorer) and create virtual machine for automation64 MutableCapabilities sauceOptions = new MutableCapabilities();65 InternetExplorerOptions browserOptions = new InternetExplorerOptions();66 browserOptions.setCapability("platformName", "Windows 10");//start in windows 10 OS.67 browserOptions.setCapability("browserVersion", "11.285");//chrome version 11.2868 browserOptions.setCapability("sauce:options", sauceOptions);69 try {//Exception handling when remote the browser70 driver = new RemoteWebDriver(new URL(URL), browserOptions);71 } catch (MalformedURLException e) {72 e.printStackTrace();73 }74 //browser name match ( Safari ) run this browser75 } else if (browserName.equalsIgnoreCase("Safari")) {76 //sauce lab open In (Safari) and create virtual machine for automation77 MutableCapabilities sauceOptions = new MutableCapabilities();78 sauceOptions.setCapability("screenResolution", "1024x768");//set screenResolution79 SafariOptions browserOptions = new SafariOptions();80 browserOptions.setCapability("platformName", "macOS 10.15");//start in Mac OS.81 browserOptions.setCapability("browserVersion", "13.1");//safari version is 12.082 browserOptions.setCapability("sauce:options", sauceOptions);83 try {//Exception handling when remote the browser84 driver = new RemoteWebDriver(new URL(URL), browserOptions);85 } catch (MalformedURLException e) {86 e.printStackTrace();87 }88 } else {89 //String concatenation and string value print90 System.out.println(" You Enter Wrong Browser Name Or Empty , Please Enter Valid Browser Name !!!" + browserName.equalsIgnoreCase(""));91 }92 } else {93 //else sauce lab false then start this condition and start automation in Local Device.94 System.out.println("*** Automation Start With ( *** Local Device *** ) && " + " ( *** " + browserName + " *** )");//print string value95 //if else if condition for cross browsing96 //browser name match ( Chrome ) run this browser97 if (browserName.equalsIgnoreCase("Chrome")) {98// DesiredCapabilities capabilities = DesiredCapabilities.chrome();99// ChromeOptions options = new ChromeOptions();100// options.addArguments("--incognito");101 // capabilities.setCapability(ChromeOptions.CAPABILITY, options);102 System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\BrowserDriver\\chromedriver.exe");103 driver = new ChromeDriver();104 //browser name match ( Firefox ) run this browser105 } else if (browserName.equalsIgnoreCase("Firefox")) {106 System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\BrowserDriver\\geckodriver.exe");107 // DesiredCapabilities capabilities = DesiredCapabilities.firefox();108 // capabilities.setCapability("browser.privatebrowsing.autostart", true);109 driver = new FirefoxDriver();110 //browser name match ( InternetExplorer ) run this browser111 } else if (browserName.equalsIgnoreCase("InternetExplorer")) {112 // DesiredCapabilities cap = DesiredCapabilities.internetExplorer();113 // Set ACCEPT_SSL_CERTS variable to true114 // cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);115 // Set the driver path116 System.setProperty("webdriver.ie.driver", "src\\test\\resources\\BrowserDriver\\IEDriverServer.exe");117 driver = new InternetExplorerDriver();118 //object of InternetExplorerDriver or InternetExplorer open119 } else {120 //String concatenation and string value print121 System.out.println("You Enter Wrong Browser Name Or Empty ,Please Enter Valid Browser Name !!!" + browserName.equalsIgnoreCase(""));122 }123 }124 driver.manage().window().maximize();//run time windows size maximize125 driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);//wait for 60 second before all test case126 driver.get(loadProp.getProperty("URL"));//type URL by get property127 }128 public void closeBrowser() {...
Source:CapabilitiesHelper.java
...18 }19 public static Capabilities getSafari11(boolean legacy) {20 SafariOptions safari = new SafariOptions();21 if (legacy) {22 safari.setCapability("version", "11.0");23 safari.setCapability("platform", "macOS 10.12");24 } else {25 safari.setCapability("browserVersion", "11.0");26 safari.setCapability("platformName", "macOS 10.12");27 }28 MutableCapabilities options = new MutableCapabilities();29 options.setCapability("name", "Safari 11");30 options.setCapability("seleniumVersion", "3.4.0");31 return setSauceCredentials(safari, legacy, options);32 }33 public static Capabilities getSafari12(boolean legacy) {34 SafariOptions safari = new SafariOptions();35 if (legacy) {36 safari.setCapability("version", "12.1");37 safari.setCapability("platform", "macOS 10.13");38 } else {39 safari.setCapability("browserVersion", "12.1");40 safari.setCapability("platformName", "macOS 10.13");41 }42 MutableCapabilities options = new MutableCapabilities();43 options.setCapability("name", "Safari 12");44 options.setCapability("seleniumVersion", "3.4.0");45 return setSauceCredentials(safari, legacy, options);46 }47 public static Capabilities getEdge18() {48 EdgeOptions edge = new EdgeOptions();49 edge.setCapability("browserVersion", "18.17763");50 edge.setCapability("platformName", "Windows 10");51 MutableCapabilities options = new MutableCapabilities();52 options.setCapability("name", "Edge 18");53 options.setCapability("avoidProxy", true);54 options.setCapability("screenResolution", "1920x1080");55 return setSauceCredentials(edge, false, options);56 }57 public static Capabilities getIE11(boolean legacy) {58 InternetExplorerOptions ie = new InternetExplorerOptions();59 if (legacy) {60 ie.setCapability("version", "11.285");61 ie.setCapability("platform", "Windows 10");62 } else {63 ie.setCapability("browserVersion", "11.285");64 ie.setCapability("platformName", "Windows 10");65 }66 MutableCapabilities options = new MutableCapabilities();67 options.setCapability("screenResolution", "1920x1080");68 options.setCapability("name", "IE 11");69 return setSauceCredentials(ie, legacy, options);70 }71 public static Capabilities getSamsungGalaxyS8() {72 DesiredCapabilities device = new DesiredCapabilities();73 device.setCapability("browserName", "");74 device.setCapability("name", "Android Demo");75 device.setCapability("platformName", "Android");76 device.setCapability("platformVersion", "7.0");77 device.setCapability("appiumVersion", "1.9.1");78 device.setCapability("deviceName", "Samsung Galaxy S8 FHD GoogleAPI Emulator");79 device.setCapability("automationName", "uiautomator2");80 device.setCapability("newCommandTimeout", 600);81 return setSauceCredentials(device);82 }83 public static Capabilities getIphoneXS() {84 DesiredCapabilities device = new DesiredCapabilities();85 device.setCapability("browserName", "");86 device.setCapability("name", "iOS Native Demo");87 device.setCapability("platformName", "iOS");88 device.setCapability("platformVersion", "13.0");89 device.setCapability("appiumVersion", "1.17.1");90 device.setCapability("deviceName", "iPhone XS Simulator");91 return setSauceCredentials(device);92 }93 public static Capabilities getAndroid8ChromeEmulator(boolean headless) {94 ChromeOptions androidEmulator = new ChromeOptions().setHeadless(headless);95 androidEmulator.addArguments("hide-scrollbars");96 Map<String, Object> deviceMetrics = new HashMap<>();97 deviceMetrics.put("width", 384);98 deviceMetrics.put("height", 512);99 deviceMetrics.put("pixelRation", 2);100 Map<String, Object> mobileEmulation = new HashMap<>();101 mobileEmulation.put("deviceMetrics", deviceMetrics);102 mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 8.0.0; Android SDK built for x86_64 Build/OSR1.180418.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36");103 androidEmulator.setExperimentalOption("mobileEmulation", mobileEmulation);104 return androidEmulator;105 }106 public static Capabilities setSauceCredentials(Capabilities caps, boolean legacy, Capabilities options) {107 MutableCapabilities sauceOpts = new MutableCapabilities();108 sauceOpts.setCapability("username", System.getenv("SAUCE_USERNAME"));109 sauceOpts.setCapability("accessKey", System.getenv("SAUCE_ACCESS_KEY"));110 sauceOpts = sauceOpts.merge(options);111 if (legacy) {112 return caps.merge(sauceOpts);113 } else {114 MutableCapabilities sauce = new MutableCapabilities();115 sauce.setCapability("sauce:options", sauceOpts);116 return caps.merge(sauce);117 }118 }119 public static Capabilities setSauceCredentials(Capabilities caps) {120 return setSauceCredentials(caps, true, new MutableCapabilities());121 }122}...
Source:W3CTestNG.java
...44 */45 public void createDriverOptions(MutableCapabilities options, String browserVersion, String platformName) throws MalformedURLException{46 String username = System.getenv("SAUCE_USERNAME");47 String accesskey = System.getenv("SAUCE_ACCESS_KEY");48 options.setCapability("browserVersion", browserVersion);49 options.setCapability("platformName", platformName);50 MutableCapabilities sauceOptions = new MutableCapabilities();51 sauceOptions.setCapability("seleniumVersion", "3.11.0");52 sauceOptions.setCapability("name", "W3CTestNG");53 sauceOptions.setCapability("build", "W3C");54 options.setCapability("sauce:options", sauceOptions);55 driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + "@ondemand.saucelabs.com/wd/hub"), options);56 }57 @AfterMethod58 public void teardown(ITestResult result){59 driver.quit();60 }61 @Test(dataProvider = "data")62 public void simpleCase(MutableCapabilities options, String browserVersion, String platformName) throws MalformedURLException {63 this.createDriverOptions(options, browserVersion, platformName);64 GuineaPigPage page = GuineaPigPage.visitPage(driver);65 String title = page.driver.getTitle();66 Assert.assertTrue(title.contains("Sauce"));67 }68}...
Source:SauceLabController.java
...28 public void setupController()29 {30 String sauceURL =helper.readSauceLabUrl();31 MutableCapabilities capabilities = new MutableCapabilities();32 capabilities.setCapability("sauce:options", getSauceOpts());33 capabilities.setCapability("goog:chromeOptions", getChromeOptions());34 capabilities.setCapability("browserName", "chrome");35 capabilities.setCapability("platformVersion", "Windows 10");36 capabilities.setCapability("browserVersion", helper.readBrowserVersion());37 try38 {39 driver = new RemoteWebDriver(new URL(sauceURL), capabilities);40 }41 catch (MalformedURLException ex)42 {43 }44 }45 private ChromeOptions getChromeOptions()46 {47 ChromeOptions chromeOpts = new ChromeOptions();48 chromeOpts.setExperimentalOption("w3c", true);49 return chromeOpts;50 }51 private MutableCapabilities getSauceOpts()52 {53 String sauceUserName = helper.readSauceLabUserName();54 String sauceAccessKey = helper.readSauceLabAccessKey();55 List<String> tags = Arrays.asList("sauceDemo", "demoTest", "module4", "javaTest");56 MutableCapabilities sauceOpts = new MutableCapabilities();57 sauceOpts.setCapability("username", sauceUserName);58 sauceOpts.setCapability("accessKey", sauceAccessKey);59 /** In order to use w3c you must set the seleniumVersion **/60 sauceOpts.setCapability("seleniumVersion", "3.141.59");61 sauceOpts.setCapability("name", "4-best-practices");62 sauceOpts.setCapability("tags", tags);63 sauceOpts.setCapability("maxDuration", 3600);64 sauceOpts.setCapability("commandTimeout", 600);65 sauceOpts.setCapability("idleTimeout", 1000);66 sauceOpts.setCapability("build", "Onboarding Sample App - Java-Junit5");67 return sauceOpts;68 }69 @Override70 public void teardownController() {71 }72}...
Source:BaseTest.java
...40 41 public void openBrowser() throws IOException {42 43 MutableCapabilities sauceOpts = new MutableCapabilities();44// sauceOpts.setCapability("name", "jaimin");45// sauceOpts.setCapability("build", "Java-W3C-Examples");46// sauceOpts.setCapability("seleniumVersion", "3.141.59");47 sauceOpts.setCapability("username", "jaiminka");48 sauceOpts.setCapability("accessKey", "7e60f207-059d-48d0-a73e-f3e15bce4ce8");49 MutableCapabilities browserOption = new MutableCapabilities();50 browserOption.setCapability(CapabilityType.BROWSER_NAME, browser);51 52 browserOption.setCapability("platformName", platform);53 browserOption.setCapability("browserVersion", version);54 browserOption.setCapability("sauce:options", sauceOpts);55 try {56 57 driver = new RemoteWebDriver(new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub"), browserOption);58 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);59 } catch (MalformedURLException e) {60 e.printStackTrace();61 }62 63 64 }65 public void closeBroser() {66 driver.close();67 }68 public void openAmazon() {...
Source:RunInCloud.java
...1819 public static void main(String[] args) throws MalformedURLException {20 21 MutableCapabilities sauceOptions = new MutableCapabilities();22 sauceOptions.setCapability("browserName", "safari");2324 SafariOptions browserOptions = new SafariOptions();25 browserOptions.setCapability("platformName", "macOS 10.15");26 browserOptions.setCapability("browserVersion", "latest");27 browserOptions.setCapability("sauce:options", sauceOptions);28 29 /*30 * MutableCapabilities sauceOptions = new MutableCapabilities();31 * sauceOptions.setCapability("browserName", "chrome"); ChromeOptions32 * browserOptions = new ChromeOptions();33 * 34 * browserOptions.setExperimentalOption("w3c", true);35 * browserOptions.setCapability("platformName", "Windows 10");36 * browserOptions.setCapability("browserVersion", "88.0");37 * browserOptions.setCapability("sauce:options", sauceOptions);38 */39 40 41 42 RemoteWebDriver driver = new RemoteWebDriver(new URL("https://a389862:924f8895-cd5f-404c-821f-50b2a8567d71@ondemand.us-west-1.saucelabs.com:443/wd/hub"), sauceOptions);43 44 45 driver.manage().window().maximize();46 driver.get("http://leaftaps.com/opentaps/control/main");47 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);48 driver.findElement(By.id("username")).sendKeys("demosalesmanager");49 driver.findElement(By.id("password")).sendKeys("crmsfa");50 driver.findElement(By.className("decorativeSubmit")).click();51
...
setCapability
Using AI Code Generation
1package com.automation;2import org.openqa.selenium.MutableCapabilities;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.edge.EdgeDriver;6import org.openqa.selenium.firefox.FirefoxDriver;7import org.openqa.selenium.ie.InternetExplorerDriver;8import org.openqa.selenium.opera.OperaDriver;9import org.openqa.selenium.remote.CapabilityType;10import org.openqa.selenium.remote.DesiredCapabilities;11import org.openqa.selenium.safari.SafariDriver;12public class DesiredCapabilitiesDemo {13 public static WebDriver driver;14 public static String browser = "chrome";15 public static void main(String[] args) {16 if(browser.equals("chrome")) {17 System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe");18 driver = new ChromeDriver();19 }20 else if(browser.equals("firefox")) {21 System.setProperty("webdriver.gecko.driver", "C:\\Users\\User\\Downloads\\geckodriver-v0.29.1-win64\\geckodriver.exe");22 driver = new FirefoxDriver();23 }24 else if(browser.equals("edge")) {25 System.setProperty("webdriver.edge.driver", "C:\\Users\\User\\Downloads\\edgedriver_win64\\msedgedriver.exe");26 driver = new EdgeDriver();27 }28 else if(browser.equals("ie")) {29 System.setProperty("webdriver.ie.driver", "C:\\Users\\User\\Downloads\\IEDriverServer_x64_3.150.1\\IEDriverServer.exe");30 driver = new InternetExplorerDriver();31 }32 else if(browser.equals("opera")) {33 System.setProperty("webdriver.opera.driver", "C:\\Users\\User\\Downloads\\operadriver_win64\\operadriver_win64\\operadriver.exe");34 driver = new OperaDriver();35 }36 else if(browser.equals("safari")) {37 driver = new SafariDriver();38 }39 System.out.println(driver.getTitle());40 driver.quit();41 }42}43package com.automation;44import org.openqa.selenium.MutableCapabilities;45import org.openqa.selenium.WebDriver;46import org.openqa.selenium.chrome.ChromeDriver;47import org.openqa.selenium.edge.EdgeDriver;48import org.openqa.selenium.firefox.FirefoxDriver;49import org.openqa.selenium.ie.InternetExplorerDriver;50import
setCapability
Using AI Code Generation
1import org.openqa.selenium.MutableCapabilities;2import org.openqa.selenium.chrome.ChromeOptions;3import org.openqa.selenium.remote.RemoteWebDriver;4import java.net.URL;5import java.net.MalformedURLException;6public class MutableCapabilitiesExample {7 public static void main(String[] args) {8 MutableCapabilities caps = new MutableCapabilities();9 caps.setCapability("browserName", "chrome");10 ChromeOptions options = new ChromeOptions();11 options.setCapability("goog:chromeOptions", caps);12 driver.quit();13 }14}15import org.openqa.selenium.remote.DesiredCapabilities;16import org.openqa.selenium.chrome.ChromeOptions;17import org.openqa.selenium.remote.RemoteWebDriver;18import java.net.URL;19import java.net.MalformedURLException;20public class DesiredCapabilitiesExample {21 public static void main(String[] args) {22 DesiredCapabilities caps = new DesiredCapabilities();23 caps.setCapability("browserName", "chrome");24 ChromeOptions options = new ChromeOptions();25 options.setCapability("goog:chromeOptions", caps);26 driver.quit();27 }28}29import org.openqa.selenium.remote.CapabilityType;30import org.openqa.selenium.chrome.ChromeOptions;31import org.openqa.selenium.remote.RemoteWebDriver;32import java.net.URL;33import java.net.MalformedURLException;34public class CapabilityTypeExample {35 public static void main(String[] args) {36 ChromeOptions options = new ChromeOptions();37 options.setCapability(C
setCapability
Using AI Code Generation
1package com.selenium4beginners.java.webdriver.basics;2import org.openqa.selenium.MutableCapabilities;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.chrome.ChromeOptions;6import org.openqa.selenium.edge.EdgeDriver;7import org.openqa.selenium.edge.EdgeOptions;8import org.openqa.selenium.firefox.FirefoxDriver;9import org.openqa.selenium.firefox.FirefoxOptions;10import org.openqa.selenium.opera.OperaDriver;11import org.openqa.selenium.opera.OperaOptions;12import org.openqa.selenium.safari.SafariDriver;13import org.openqa.selenium.safari.SafariOptions;14public class SetCapability {15 public static void main(String[] args) {16 setCapability();17 }18 public static void setCapability() {19 ChromeOptions chromeOptions = new ChromeOptions();20 MutableCapabilities mutableCapabilities = new MutableCapabilities();21 mutableCapabilities.setCapability("profile.default_content_settings.popups", 0);22 mutableCapabilities.setCapability("download.default_directory", "C:\\Users\\Selenium\\Downloads");23 chromeOptions.merge(mutableCapabilities);24 WebDriver driver = new ChromeDriver(chromeOptions);25 FirefoxOptions firefoxOptions = new FirefoxOptions();26 MutableCapabilities mutableCapabilities1 = new MutableCapabilities();27 mutableCapabilities1.setCapability("profile.default_content_settings.popups", 0);28 mutableCapabilities1.setCapability("download.default_directory", "C:\\Users\\Selenium\\Downloads");29 firefoxOptions.merge(mutableCapabilities1);30 WebDriver driver1 = new FirefoxDriver(firefoxOptions);
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!