diff --git a/src/main/java/ru/alfabank/tests/core/drivers/CustomDriverProvider.java b/src/main/java/ru/alfabank/tests/core/drivers/CustomDriverProvider.java index cf657443..084b4045 100644 --- a/src/main/java/ru/alfabank/tests/core/drivers/CustomDriverProvider.java +++ b/src/main/java/ru/alfabank/tests/core/drivers/CustomDriverProvider.java @@ -21,11 +21,14 @@ import net.lightbody.bmp.BrowserMobProxyServer; import net.lightbody.bmp.proxy.BlacklistEntry; import org.openqa.selenium.Dimension; +import org.openqa.selenium.MutableCapabilities; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.opera.OperaDriver; +import org.openqa.selenium.opera.OperaOptions; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; @@ -34,7 +37,6 @@ import java.net.MalformedURLException; import java.net.URI; import java.util.HashMap; -import java.util.Hashtable; import java.util.List; import java.util.Map; @@ -47,9 +49,10 @@ * * Например, можно указать браузер, версию браузера, remote Url(где будут запущены тесты), ширину и высоту окна браузера: * -Dbrowser=chrome -DbrowserVersion=63.0 -DremoteUrl=http://some/url -Dwidth=1000 -Dheight=500 - * Если параметр remoteUrl не указан - тесты будут запущены локально в заданном браузере последней версии + * Если параметр remoteUrl не указан - тесты будут запущены локально в заданном браузере последней версии. + * Все необходимые опции можно прописывать в переменную options, разделяя их пробелом. * Если указан параметр remoteUrl и browser, но версия браузера не указана, - * по умолчанию для chrome будет установлена версия 60.0 и для firefox версия 57.0 + * по умолчанию будет установлена версия latest * Если браузер не указан - по умолчанию будет запущен chrome * По умолчанию размер окна браузера при remote запуске равен 1920x1080 * Предусмотрена возможность запуска в режиме мобильного браузера (-Dbrowser=mobile) @@ -59,44 +62,37 @@ public class CustomDriverProvider implements WebDriverProvider { public final static String MOBILE_DRIVER = "mobile"; public final static String BROWSER = "browser"; - public final static String BROWSER_VERSION = "browserVersion"; public final static String REMOTE_URL = "remoteUrl"; public final static String WINDOW_WIDTH = "width"; public final static String WINDOW_HEIGHT = "height"; - public final static String RESOLUTION = "resolution"; + public final static String VERSION_LATEST = "latest"; + public final static String LOCAL = "local"; public final static int DEFAULT_WIDTH = 1920; public final static int DEFAULT_HEIGHT = 1080; private BrowserMobProxy proxy = new BrowserMobProxyServer(); + private String[] options = loadSystemPropertyOrDefault("options", "").split(" "); @Override public WebDriver createDriver(DesiredCapabilities capabilities) { String expectedBrowser = loadSystemPropertyOrDefault(BROWSER, CHROME); - String remoteUrl = loadSystemPropertyOrDefault(REMOTE_URL, "local"); + String remoteUrl = loadSystemPropertyOrDefault(REMOTE_URL, LOCAL); BlackList blackList = new BlackList(); if (FIREFOX.equalsIgnoreCase(expectedBrowser)) { - capabilities = getFirefoxDriverCapabilities(); - return "local".equalsIgnoreCase(remoteUrl) ? new FirefoxDriver() : getRemoteDriver(capabilities, remoteUrl, blackList.getBlacklistEntries()); + return LOCAL.equalsIgnoreCase(remoteUrl) ? createFirefoxDriver() : getRemoteDriver(getFirefoxDriverOptions(), remoteUrl, blackList.getBlacklistEntries()); } if (MOBILE_DRIVER.equalsIgnoreCase(expectedBrowser)) { - capabilities.setCapability(ChromeOptions.CAPABILITY, getMobileChromeOptions()); - return "local".equalsIgnoreCase(remoteUrl) ? new ChromeDriver(getMobileChromeOptions()) : getRemoteDriver(capabilities, remoteUrl, blackList.getBlacklistEntries()); + return LOCAL.equalsIgnoreCase(remoteUrl) ? new ChromeDriver(getMobileChromeOptions()) : getRemoteDriver(getMobileChromeOptions(), remoteUrl, blackList.getBlacklistEntries()); } if (OPERA.equalsIgnoreCase(expectedBrowser)) { - capabilities = getOperaDriverCapabilities(); - return "local".equalsIgnoreCase(remoteUrl) ? new OperaDriver() : getRemoteDriver(capabilities, remoteUrl, blackList.getBlacklistEntries()); + return LOCAL.equalsIgnoreCase(remoteUrl) ? createOperaDriver() : getRemoteDriver(getOperaDriverOptions(), remoteUrl, blackList.getBlacklistEntries()); } - ChromeOptions chromeOptions = new ChromeOptions(); - chromeOptions.addArguments("--window-size=1300,1000"); - DesiredCapabilities cap = DesiredCapabilities.chrome(); - cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions); - - log.info("remoteUrl=" + remoteUrl + " expectedBrowser= " + expectedBrowser + " BROWSER_VERSION=" + System.getProperty(BROWSER_VERSION)); - capabilities = getChromeDriverCapabilities(); - return "local".equalsIgnoreCase(remoteUrl) ? new ChromeDriver(chromeOptions) : getRemoteDriver(capabilities, remoteUrl, blackList.getBlacklistEntries()); + + log.info("remoteUrl=" + remoteUrl + " expectedBrowser= " + expectedBrowser + " BROWSER_VERSION=" + System.getProperty(CapabilityType.BROWSER_VERSION)); + return LOCAL.equalsIgnoreCase(remoteUrl) ? createChromeDriver() : getRemoteDriver(getChromeDriverOptions(), remoteUrl, blackList.getBlacklistEntries()); } /** @@ -106,18 +102,16 @@ public WebDriver createDriver(DesiredCapabilities capabilities) { * @param remoteUrl - url для запуска тестов, например http://remoteIP:4444/wd/hub * @return */ - private WebDriver getRemoteDriver(DesiredCapabilities capabilities, String remoteUrl) { + private WebDriver getRemoteDriver(MutableCapabilities capabilities, String remoteUrl) { log.info("---------------run Selenoid Remote Driver---------------------"); - Integer browserWidth = loadSystemPropertyOrDefault(WINDOW_WIDTH, DEFAULT_WIDTH); - Integer browserHeight = loadSystemPropertyOrDefault(WINDOW_HEIGHT, DEFAULT_HEIGHT); capabilities.setCapability("enableVNC", true); + capabilities.setCapability("screenResolution", String.format("%sx%s", loadSystemPropertyOrDefault(WINDOW_WIDTH, DEFAULT_WIDTH), + loadSystemPropertyOrDefault(WINDOW_HEIGHT, DEFAULT_HEIGHT))); try { - RemoteWebDriver driver = new RemoteWebDriver( + return new RemoteWebDriver( URI.create(remoteUrl).toURL(), capabilities ); - driver.manage().window().setSize(new Dimension(browserWidth, browserHeight)); - return driver; } catch (MalformedURLException e) { throw new RuntimeException(e); } @@ -131,7 +125,7 @@ private WebDriver getRemoteDriver(DesiredCapabilities capabilities, String remot * @param blacklistEntries - список url для добавления в Blacklist * @return */ - private WebDriver getRemoteDriver(DesiredCapabilities capabilities, String remoteUrl, List blacklistEntries) { + private WebDriver getRemoteDriver(MutableCapabilities capabilities, String remoteUrl, List blacklistEntries) { proxy.setBlacklist(blacklistEntries); return getRemoteDriver(capabilities, remoteUrl); } @@ -145,15 +139,12 @@ private WebDriver getRemoteDriver(DesiredCapabilities capabilities, String remot private ChromeOptions getMobileChromeOptions() { log.info("---------------run CustomMobileDriver---------------------"); String mobileDeviceName = loadSystemPropertyOrDefault("device", "Nexus 5"); - ChromeOptions chromeOptions = new ChromeOptions(); - - chromeOptions.addArguments("disable-extensions", + ChromeOptions chromeOptions = new ChromeOptions().addArguments("disable-extensions", "test-type", "no-default-browser-check", "ignore-certificate-errors"); Map mobileEmulation = new HashMap<>(); mobileEmulation.put("deviceName", mobileDeviceName); chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation); - return chromeOptions; } @@ -162,14 +153,11 @@ private ChromeOptions getMobileChromeOptions() { * * @return */ - private DesiredCapabilities getChromeDriverCapabilities() { + private ChromeOptions getChromeDriverOptions() { log.info("---------------Chrome Driver---------------------"); - DesiredCapabilities capabilities = DesiredCapabilities.chrome(); - capabilities.setBrowserName(CHROME); - capabilities.setVersion(loadSystemPropertyOrDefault(BROWSER_VERSION, "latest")); - capabilities.setCapability(RESOLUTION, String.format("%sx%s", - loadSystemPropertyOrDefault(WINDOW_WIDTH,DEFAULT_WIDTH), loadSystemPropertyOrDefault(WINDOW_HEIGHT,DEFAULT_HEIGHT))); - return capabilities; + ChromeOptions chromeOptions = !options[0].equals("") ? new ChromeOptions().addArguments(options) : new ChromeOptions(); + chromeOptions.setCapability(CapabilityType.BROWSER_VERSION, VERSION_LATEST); + return chromeOptions; } /** @@ -177,14 +165,11 @@ private DesiredCapabilities getChromeDriverCapabilities() { * * @return */ - private DesiredCapabilities getFirefoxDriverCapabilities() { + private FirefoxOptions getFirefoxDriverOptions() { log.info("---------------Firefox Driver---------------------"); - DesiredCapabilities capabilities = DesiredCapabilities.firefox(); - capabilities.setBrowserName(FIREFOX); - capabilities.setVersion(loadSystemPropertyOrDefault(BROWSER_VERSION, "latest")); - capabilities.setCapability(RESOLUTION, String.format("%sx%s", - loadSystemPropertyOrDefault(WINDOW_WIDTH,DEFAULT_WIDTH), loadSystemPropertyOrDefault(WINDOW_HEIGHT,DEFAULT_HEIGHT))); - return capabilities; + FirefoxOptions firefoxOptions = !options[0].equals("") ? new FirefoxOptions().addArguments(options) : new FirefoxOptions(); + firefoxOptions.setCapability(CapabilityType.BROWSER_VERSION, VERSION_LATEST); + return firefoxOptions; } /** @@ -192,45 +177,41 @@ private DesiredCapabilities getFirefoxDriverCapabilities() { * * @return */ - private DesiredCapabilities getOperaDriverCapabilities() { + private OperaOptions getOperaDriverOptions() { log.info("---------------Opera Driver---------------------"); - DesiredCapabilities capabilities = DesiredCapabilities.operaBlink(); - capabilities.setBrowserName(OPERA); - capabilities.setVersion(loadSystemPropertyOrDefault(BROWSER_VERSION, "latest")); - capabilities.setCapability(RESOLUTION, String.format("%sx%s", - loadSystemPropertyOrDefault(WINDOW_WIDTH,DEFAULT_WIDTH), loadSystemPropertyOrDefault(WINDOW_HEIGHT,DEFAULT_HEIGHT))); - return capabilities; + OperaOptions operaOptions = !options[0].equals("") ? new OperaOptions().addArguments(options) : new OperaOptions(); + operaOptions.setCapability(CapabilityType.BROWSER_VERSION, VERSION_LATEST); + return operaOptions; + } + + /** + * Создает WebDriver + * + * @return + */ + private WebDriver createChromeDriver(){ + ChromeDriver chromeDriver = new ChromeDriver(getChromeDriverOptions()); + chromeDriver.manage().window().setSize(setDimension()); + return chromeDriver; + } + + private WebDriver createFirefoxDriver(){ + FirefoxDriver firefoxDriver = new FirefoxDriver(getFirefoxDriverOptions()); + firefoxDriver.manage().window().setSize(setDimension()); + return firefoxDriver; + } + + private WebDriver createOperaDriver(){ + OperaDriver operaDriver = new OperaDriver(getOperaDriverOptions()); + operaDriver.manage().window().setSize(setDimension()); + return operaDriver; } /** - * Используется для создания хром-браузера с пользовательскими настройками - * ("profile.default_content_settings.popups", 0) - блокирует всплывающие окна - * ("download.prompt_for_download", "false") - выключает подтверждение (и выбор пути) для загрузки файла - * ("download.default_directory", ...) - устанавливает стандартную папку загрузки файлов - * "plugins.plugins_disabled", new String[]{ - * "Adobe Flash Player", "Chrome PDF Viewer" - выключает плагины - *

- * Чтобы задать папку для загрузки файлов пропишите абсолютный путь - * в fileDownloadPath в application.properties - *

- * Полный список параметров, которые можно установить таким способом: - * https://sites.google.com/a/chromium.org/chromedriver/capabilities + * Задает настройки разрешения */ - private DesiredCapabilities getCapabilitiesWithCustomFileDownloadFolder(DesiredCapabilities capabilities) { - Map preferences = new Hashtable<>(); - preferences.put("profile.default_content_settings.popups", 0); - preferences.put("download.prompt_for_download", "false"); - String downloadsPath = System.getProperty("user.home") + "/Downloads"; - preferences.put("download.default_directory", loadSystemPropertyOrDefault("fileDownloadPath", downloadsPath)); - preferences.put("plugins.plugins_disabled", new String[]{ - "Adobe Flash Player", "Chrome PDF Viewer"}); - capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); - - ChromeOptions options = new ChromeOptions(); - options.setExperimentalOption("prefs", preferences); - - capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); - capabilities.setCapability(ChromeOptions.CAPABILITY, options); - return capabilities; + private Dimension setDimension(){ + return new Dimension(loadSystemPropertyOrDefault(WINDOW_WIDTH, DEFAULT_WIDTH), + loadSystemPropertyOrDefault(WINDOW_HEIGHT, DEFAULT_HEIGHT)); } } diff --git a/src/main/java/ru/alfabank/tests/core/drivers/MobileChrome.java b/src/main/java/ru/alfabank/tests/core/drivers/MobileChrome.java deleted file mode 100644 index c8813b50..00000000 --- a/src/main/java/ru/alfabank/tests/core/drivers/MobileChrome.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright 2017 Alfa Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package ru.alfabank.tests.core.drivers; - -import com.codeborne.selenide.WebDriverProvider; -import lombok.extern.slf4j.Slf4j; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.chrome.ChromeOptions; -import org.openqa.selenium.remote.DesiredCapabilities; - -import java.util.HashMap; -import java.util.Map; - -import static ru.alfabank.tests.core.helpers.PropertyLoader.loadSystemPropertyOrDefault; - - -/** - * Эмуляция мобильной версии браузера Google Chrome - */ - - -@Slf4j -@Deprecated -public class MobileChrome implements WebDriverProvider { - - /** - * Создание instance google chrome эмулирующего работу на мобильном устройстве (по умолчанию nexus 5) - * Мобильное устройство может быть задано через системные переменные - * - * @param capabilities настройки Chrome браузера - * @return возвращает новый instance Chrome драйера - */ - - @Override - public WebDriver createDriver(DesiredCapabilities capabilities) { - log.info("---------------run CustomMobileDriver---------------------"); - String mobileDeviceName = loadSystemPropertyOrDefault("device", "Nexus 5"); - Map mobileEmulation = new HashMap<>(); - mobileEmulation.put("deviceName", mobileDeviceName); - - Map chromeOptions = new HashMap<>(); - chromeOptions.put("mobileEmulation", mobileEmulation); - - DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome(); - desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); - desiredCapabilities.setBrowserName(desiredCapabilities.chrome().getBrowserName()); - return new ChromeDriver(desiredCapabilities); - } - - -}