def setup_method(self, method): """ Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup and teardown """ self.verificationErrors = [] self.cf = saunter.ConfigWrapper.ConfigWrapper().config self.config = self.cf if self.cf.getboolean("SauceLabs", "ondemand"): desired_capabilities = { "platform": self.cf.get("SauceLabs", "os"), "browserName": self.cf.get("SauceLabs", "browser"), "version": self.cf.get("SauceLabs", "browser_version"), "name": self._testMethodName } if desired_capabilities["browserName"][0] == "*": desired_capabilities["browserName"] = desired_capabilities[ "browserName"][1:] if desired_capabilities["platform"] in os_map: desired_capabilities["platform"] = os_map[ desired_capabilities["platform"]] command_executor = "http://%s:%[email protected]:80/wd/hub" % ( self.cf.get("SauceLabs", "username"), self.cf.get("SauceLabs", "key")) else: browser = self.cf.get("Selenium", "browser") if browser[0] == "*": browser = browser[1:] if browser == "chrome": os.environ["webdriver.chrome.driver"] = self.cf.get( "Selenium", "chromedriver_path") desired_capabilities = capabilities_map[browser] if self.cf.has_section("Proxy") \ and self.cf.has_option("Proxy", "proxy_url") \ and (self.cf.has_option("Proxy", "browsermob") and self.cf.getboolean("Proxy", "browsermob")): from browsermobproxy import Client self.client = Client(self.cf.get("Proxy", "proxy_url")) self.client.add_to_webdriver_capabilities(desired_capabilities) if self.cf.has_section("Grid"): if self.cf.getboolean("Grid", "use_grid") and self.cf.get( "Grid", "type") == "selenium": if self.cf.has_option("Grid", "platform"): desired_capabilities["platform"] = self.cf.get( "Grid", "platform").upper() if self.cf.has_option("Grid", "version"): desired_capabilities["version"] = str( self.cf.get("Grid", "browser_version")) command_executor = "http://%s:%s/wd/hub" % (self.cf.get( "Selenium", "server_host"), self.cf.get("Selenium", "server_port")) self.driver = WebDriver(desired_capabilities=desired_capabilities, command_executor=command_executor) if self.cf.getboolean("Saunter", "use_implicit_wait"): self.driver.implicitly_wait( self.cf.getint("Saunter", "implicit_wait")) if self.cf.getboolean("SauceLabs", "ondemand"): self.sauce_session = self.driver.session_id
def start_proxy(self): """ Assumes browsermob-proxy is running in a docker container and initiates a client :return: browsermob-proxy client instance """ proxy_ip = self._get_browsermobproxy_docker_ip() proxy_port = "9090" self._client = Client("{0}:{1}".format(proxy_ip, proxy_port))
def proxy(): """Фикстура для конфигурирования прокси-сервера""" server = Server( "03_PageObject/browsermob-proxy-2.1.4/bin/browsermob-proxy") server.start() client = Client("localhost:8080") # print(client.port) client.new_har() return server, client
def __init__(self, br_conf, all_conf): self.browser_config = br_conf self.c = all_conf profile = None caps = cap_map[br_conf["type"]] if br_conf["type"] == "firefox": s = sys.platform if s in br_conf["profiles"].keys() and br_conf["profiles"][s]: profile_path = os.path.join(self.c["sframe"]["base"], "support", "profiles", br_conf["profiles"][s]) elif br_conf["profiles"]["profile"]: profile_path = os.path.join(self.c["sframe"]["base"], "support", "profiles", br_conf["profiles"]["profile"]) else: profile_path = None if profile_path: if os.path.isdir(profile_path): profile = FirefoxProfile(profile_path) else: raise ProfileNotFound("Profile not found at %s" % profile_path) if "on_error" in all_conf["sframe"]["screenshots"]: if not all_conf["sframe"]["screenshots"]["on_error"]: caps["webdriver.remote.quietExceptions"] = True if "headless" in br_conf["profiles"].keys( ) and br_conf["profiles"]["headless"]: chrome_options = Options() chrome_options.add_argument("--headless") caps = chrome_options.to_capabilities() print("added headless to chrome") if "type" in all_conf["selenium"]["proxy"]\ and all_conf["selenium"]["proxy"]["type"] == "browsermob": self.proxy = Client(all_conf["selenium"]["proxy"]["url"]) self.proxy.add_to_capabilities(caps) if "grid" in all_conf["selenium"]["executor"]\ and all_conf["selenium"]["executor"]["is grid"]: if br_conf["grid filters"]["platform"]: caps["platform"] = br_conf["grid filters"]["platform"].upper() if br_conf["grid filters"]["version"]: caps["version"] = str(br_conf["grid filters"]["version"]) com_exe = "http://%s:%s/wd/hub" % ( self.c["selenium"]["executor"]["host"], self.c["selenium"]["executor"]["port"]) self.driver = FrameWebdriver(desired_capabilities=caps, command_executor=com_exe, browser_profile=profile)
def pytest_configure(config): cwd = os.getcwd() sys.path.append(os.path.join(cwd, "modules")) saunter_config = saunter.ConfigWrapper.ConfigWrapper() saunter_config["saunter"] = {} saunter_config["saunter"]["base"] = cwd saunter_config["saunter"]["log_dir"] = os.environ["SAUNTER_LOG_DIR"] saunter_config.configure() # create the proxy instances we'll need; one per instance saunter_config['saunter']['proxies'] = [] if saunter_config['selenium']['proxy']['url'] != '' and saunter_config['selenium']['proxy']['type'].lower() == 'browsermob': from browsermobproxy import Client for x in range(0, int(os.environ["SAUNTER_PARALLEL"])): saunter_config['saunter']['proxies'].append(Client(saunter_config['selenium']['proxy']['url']))
def __init__(self, browser_config, all_config): self.browser_config = browser_config self.config = all_config profile = None if browser_config["type"] == 'firefox': if browser_config["profiles"][sys.platform]: profile_path = os.path.join( self.config["saunter"]["base"], 'support', 'profiles', browser_config["profiles"][sys.platform]) elif browser_config["profiles"]["profile"]: profile_path = os.path.join( self.config["saunter"]["base"], 'support', 'profiles', browser_config["profiles"]["profile"]) else: profile_path = None if profile_path: if os.path.isdir(profile_path): profile = FirefoxProfile(profile_path) else: raise ProfileNotFound("Profile not found at %s" % profile_path) if browser_config["sauce labs"]["ondemand"]: desired_capabilities = { "platform": browser_config["sauce labs"]["os"], "browserName": browser_config["type"], "version": browser_config["sauce labs"]["version"], } if desired_capabilities["platform"] in os_map: desired_capabilities["platform"] = os_map[ desired_capabilities["platform"]] if browser_config['sauce labs']['selenium version']: desired_capabilities['selenium-version'] = browser[ 'sauce labs']['selenium version'] if "disable" in self.config["sauce labs"] and self.config[ "sauce labs"]["disable"] is not None: if "record video" in self.config["sauce labs"]["disable"]: if self.config["sauce labs"]["disable"][ "record video"] == True: desired_capabilities['record-video'] = False if "upload video on pass" in self.config["sauce labs"][ "disable"]: if self.config["sauce labs"]["disable"][ "upload video on pass"] == True: desired_capabilities['video-upload-on-pass'] = False if "step screenshots" in self.config["sauce labs"]["disable"]: if self.config["sauce labs"]["disable"][ "step screenshots"] == True: desired_capabilities['record-screenshots'] = False if "sauce advisor" in self.config["sauce labs"]["disable"]: if self.config["sauce labs"]["disable"][ "sauce advisor"] == True: desired_capabilities['sauce-advisor'] = False if "enable" in self.config["sauce labs"] and self.config[ "sauce labs"]["enable"] is not None: if "source capture" in self.config["sauce labs"]["enable"]: if self.config["sauce labs"]["enable"][ "source capture"] == True: desired_capabilities['source capture'] = True if "error screenshots" in self.config["sauce labs"]["enable"]: if self.config["sauce labs"]["enable"][ "error screenshots"] == True: desired_capabilities[ 'webdriver.remote.quietExceptions'] = True command_executor = "http://%s:%[email protected]:80/wd/hub" % ( self.config["sauce labs"]["username"], self.config["sauce labs"]["key"]) else: desired_capabilities = capabilities_map[browser_config["type"]] if browser_config["proxy"]["type"] and browser_config["proxy"][ "type"].lower() == "browsermob": from browsermobproxy import Client self.client = Client(self.config.get("Proxy", "proxy_url")) self.client.add_to_webdriver_capabilities(desired_capabilities) if "is grid" in self.config["selenium"] and self.config[ "selenium"]["executor"]["is grid"]: if browser_config["grid filters"]["platform"]: desired_capabilities["platform"] = browser_config[ "grid filters"]["platform"].upper() if browser_config["grid filters"]["version"]: desired_capabilities["platform"] = str( browser_config["grid filters"]["version"]) command_executor = "http://%s:%s/wd/hub" % ( self.config["selenium"]["executor"]["host"], self.config["selenium"]["executor"]["port"]) # print(desired_capabilities) self.driver = TailoredWebDriver( desired_capabilities=desired_capabilities, command_executor=command_executor, browser_profile=profile)
from browsermobproxy import Server, Client import pytest import urllib.parse from selenium import webdriver server = Server(r"C:\Tools\browsermob-proxy-2.1.4\bin\browsermob-proxy") server.start() client = Client("localhost:8080") client.port # proxy = server.create_proxy() client.new_har() @pytest.fixture def chrome_browser(request): chrome_options = webdriver.ChromeOptions() url = vurllib.parse.urlparse(client.proxy).path chrome_options.add_argument('--proxy-server=%s' % url) driver = webdriver.Chrome(chrome_options=chrome_options) request.addfinalizer(driver.quit) return driver def test_proxy(chrome_browser): # print(chrome_browser) # driver = chrome_browser['driver'] chrome_browser.get('https://otus.ru/') print(' ') print(client.har)
def create_proxy(self): options = {'trustAllServers': 'true'} if self.trust_all_servers else {} r = requests.post(self._at('proxy'), data=options) self.port = json.loads(r.text)['port'] self.proxy = Client(self.api_base_url.replace('http://', ''), options={'existing_proxy_port_to_use': self.port})
def setup_method(self, method): """ Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup and teardown """ self.cf = self.config = saunter.ConfigWrapper.ConfigWrapper() self.current_method_name = method.__name__ browser = self.cf["browsers"][self.cf["saunter"]["default_browser"]] if browser["type"][0] == "*": browser = browser["type"] = browser["type"][1:] profile = None if browser["type"] == 'firefox': if browser["profiles"][sys.platform]: profile_path = os.path.join(self.cf["saunter"]["base"], 'support', 'profiles', browser["profiles"][sys.platform]) elif browser["profiles"]["profile"]: profile_path = os.path.join(self.cf["saunter"]["base"], 'support', 'profiles', browser["profiles"]["profile"]) else: profile_path = None if profile_path: if os.path.isdir(profile_path): profile = FirefoxProfile(profile_path) else: raise ProfileNotFound("Profile not found at %s" % profile_path) if "saucelabs" in browser and browser["saucelabs"]["ondemand"]: desired_capabilities = { "platform": self.cf["sauceLabs"]["os"], "browserName": self.cf["sauceLabs"]["browser"], "version": self.cf.get("SauceLabs", "browser_version"), "name": method.__name__ } if desired_capabilities["browserName"][0] == "*": desired_capabilities["browserName"] = desired_capabilities[ "browserName"][1:] if desired_capabilities["platform"] in os_map: desired_capabilities["platform"] = os_map[ desired_capabilities["platform"]] if self.cf.has_option("SauceLabs", "selenium_version"): desired_capabilities['selenium-version'] = self.cf.get( 'SauceLabs', 'selenium_version') command_executor = "http://%s:%[email protected]:80/wd/hub" % ( self.cf.get("SauceLabs", "username"), self.cf.get("SauceLabs", "key")) else: desired_capabilities = capabilities_map[browser["type"]] if browser["proxy"]["type"] and browser["proxy"]["type"].lower( ) == "browsermob": from browsermobproxy import Client self.client = Client(self.cf.get("Proxy", "proxy_url")) self.client.add_to_webdriver_capabilities(desired_capabilities) if "is grid" in self.cf["selenium"] and self.cf["selenium"][ "executor"]["is grid"]: if browser["grid filters"]["platform"]: desired_capabilities["platform"] = browser["grid filters"][ "platform"].upper() if browser["grid filters"]["version"]: desired_capabilities["platform"] = str( browser["grid filters"]["version"]) command_executor = "http://%s:%s/wd/hub" % ( self.cf["selenium"]["executor"]["host"], self.cf["selenium"]["executor"]["port"]) self.driver = WebDriver(desired_capabilities=desired_capabilities, command_executor=command_executor, browser_profile=profile) self.verificationErrors = [] self.matchers = Matchers(self.driver, self.verificationErrors) if "saucelabs" in self.cf["browsers"][ self.cf["saunter"]["default_browser"]] and self.cf["browsers"][ self.cf["saunter"] ["default_browser"]]["saucelabs"]["ondemand"]: self.sauce_session = self.driver.session_id self._screenshot_number = 1
def get(self, URL): try: proxy = Client('im-expservices1.gksm.local:8999') chromedriver = './chromedriver' os.environ['webdriver.chrome.driver'] = chromedriver url = urlparse.urlparse(proxy.proxy).path chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--proxy-server={0}'.format(url)) driver = webdriver.Chrome(chromedriver, chrome_options=chrome_options) driver.set_window_size(1920, 1080) try: proxy.new_har(str(URL), options={ 'captureHeaders': True, 'captureContent': True, 'captureBinaryContent': True }) driver.get(URL) proxy.wait_for_traffic_to_stop(100, 20000) except Exception, err: return 'GET ERROR: ' + str(err) S = lambda X: \ driver.execute_script('return document.body.parentNode.scroll' + X) driver.set_window_size(S('Width'), S('Height')) # har = proxy.har['log']['entries'] # mimeType = [] bodySize = [] download_time = [] counter = 0 for entry in proxy.har['log']['entries']: counter += 1 # mimeType.append(entry['response']['content']['mimeType']) bodySize.append(int(entry['response']['bodySize'])) download_time.append(int(entry['time'])) # example = defaultdict(dict) # keys = defaultdict(dict) # for i in range(1, len(mimeType)): # for entries in range(1, len(har)): # if (mimeType[i] == har[entries]['response']['content']['mimeType']): # keys[mimeType[i]][entries] = {'bodySize': int(har[entries]['response']['bodySize']),'URL': str(har[entries]['request']['url'])} # example['result'] = keys driver.quit() startDownloadTime = \ datetime.datetime.strptime(str(proxy.har['log' ]['entries'][0]['startedDateTime']), '%Y-%m-%dT%H:%M:%S.%fZ') LastStartDownloadTime = \ datetime.datetime.strptime(str(proxy.har['log' ]['entries'][counter - 1]['startedDateTime']), '%Y-%m-%dT%H:%M:%S.%fZ') proxy.close() return { 'bodySize': str(sum(bodySize)), 'browser_download_time': str(sum(download_time)), 'LastStartDownloadTime': str(LastStartDownloadTime), 'startDownloadTime': str(startDownloadTime), 'total_download_time': str((LastStartDownloadTime - startDownloadTime).total_seconds()), }