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 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
class Browser(object): 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 setup_method(self, method): self.client = Client("http://localhost:8080") profile = selenium.webdriver.FirefoxProfile() profile.set_proxy(self.client.webdriver_proxy()) self.driver = selenium.webdriver.Firefox(firefox_profile=profile)
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))
class BrowserMobProxy: def __init__(self, **kwargs): self.server = Server(BrowserMobProxyConfig.binary) self.server.start() self.host = 'localhost' self.trust_all_servers = kwargs.get('trust_all_servers') self.create_proxy() if kwargs.get('capture_content'): self.capture_content() self.blacklist_extras() @property def api_base_url(self): return "http://{}:{}".format(self.host, self.server.port) @property def proxy_server(self): return "{}:{}".format(self.host, self.port) def _at(self, path): return "{}/{}".format(self.api_base_url, path) 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 capture_content(self): requests.put(self._at('proxy/{}/har'.format(self.port)), data={'captureContent': 'true'}) def blacklist_extras(self): for site_name in extras: self.proxy.blacklist(get_generic_url_regex(site_name), 401) def close(self): self.proxy.close() self.server.stop()
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 = saunter.ConfigWrapper.ConfigWrapper().config self.config = self.cf self.current_method_name = method.__name__ 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": 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"]] 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) self.verificationErrors = [] self.matchers = Matchers(self.driver, self.verificationErrors) if self.cf.getboolean("SauceLabs", "ondemand"): self.sauce_session = self.driver.session_id self._screenshot_number = 1
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']))
class TestTwo(object): def setup_method(self, method): self.client = Client("http://localhost:8080") profile = selenium.webdriver.FirefoxProfile() profile.set_proxy(self.client.webdriver_proxy()) self.driver = selenium.webdriver.Firefox(firefox_profile=profile) def teardown_method(self, method): self.driver.quit() self.client.close() @pytest.mark.pycon @pytest.mark.deep @pytest.mark.blacklist def test_one(self): self.client.blacklist("http://www\\.google-analytics\\.com/.*", 309) self.client.new_har() home = HomePage(self.driver).open().wait_until_loaded() har = self.client.har print(har)
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
class SaunterTestCase(BaseTestCase): """ Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup and teardown """ 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 = saunter.ConfigWrapper.ConfigWrapper().config self.config = self.cf self.current_method_name = method.__name__ 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": 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"]] 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) self.verificationErrors = [] self.matchers = Matchers(self.driver, self.verificationErrors) if self.cf.getboolean("SauceLabs", "ondemand"): self.sauce_session = self.driver.session_id self._screenshot_number = 1 def teardown_method(self, method): """ Default teardown method for all scripts. If run through Sauce Labs OnDemand, the job name, status and tags are updated. Also the video and server log are downloaded if so configured. """ if hasattr(self, "config") and not self.config.getboolean("SauceLabs", "ondemand"): self.take_named_screenshot("final") if hasattr(self, "driver"): self.driver.quit() def take_numbered_screenshot(self): if self.config.has_option("Saunter", "take_screenshots"): if self.cf.getboolean("Saunter", "take_screenshots"): method_dir = self._screenshot_prep_dirs() self.driver.get_screenshot_as_file(os.path.join(method_dir, str(self._screenshot_number).zfill(3) + ".png")) self._screenshot_number = self._screenshot_number + 1 def take_named_screenshot(self, name): method_dir = self._screenshot_prep_dirs() self.driver.get_screenshot_as_file(os.path.join(method_dir, str(name) + ".png"))
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)
class SaunterTestCase(BaseTestCase): """ Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup and teardown """ 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 teardown_method(self, method): """ Default teardown method for all scripts. If run through Sauce Labs OnDemand, the job name, status and tags are updated. Also the video and server log are downloaded if so configured. """ if hasattr(self, "driver"): self.driver.quit() if hasattr(self, "cf") and self.cf.getboolean("SauceLabs", "ondemand"): # session couldn't be established for some reason if not hasattr(self, "sauce_session"): return sauce_session = self.sauce_session j = {} # name j["name"] = self._testMethodName # result if self._resultForDoCleanups._excinfo == None and not self.verificationErrors: # print("pass") j["passed"] = True else: # print("fail") j["passed"] = False # tags j["tags"] = [] for keyword in self._resultForDoCleanups.keywords: if isinstance(self._resultForDoCleanups.keywords[keyword], MarkInfo): j["tags"].append(keyword) # update which_url = "https://saucelabs.com/rest/v1/%s/jobs/%s" % (self.cf.get("SauceLabs", "username"), sauce_session) r = requests.put(which_url, data=json.dumps(j), headers={"Content-Type": "application/json"}, auth=(self.cf.get("SauceLabs", "username"), self.cf.get("SauceLabs", "key"))) r.raise_for_status() if self.cf.getboolean("SauceLabs", "get_video"): self.fetch_sauce_artifact("video.flv") if self.cf.getboolean("SauceLabs", "get_log"): self.fetch_sauce_artifact("selenium-server.log") # def fetch_artifact(session, which): # which_url = "https://saucelabs.com/rest/%s/jobs/%s/results/%s" % (self.cf.get("SauceLabs", "username"), session, which) # code = 404 # timeout = 0 # while code in [401, 404]: # r = requests.get(which_url, auth = (cf.get("SauceLabs", "username"), self.cf.get("SauceLabs", "key"))) # try: # code = r.status_code # r.raise_for_status() # except urllib2.HTTPError, e: # time.sleep(4) # # artifact = open(os.path.join(os.path.dirname(__file__), "logs", which), "wb") # artifact.write(r.content)
class SaunterTestCase(BaseTestCase): """ Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup and teardown """ 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 teardown_method(self, method): """ Default teardown method for all scripts. If run through Sauce Labs OnDemand, the job name, status and tags are updated. Also the video and server log are downloaded if so configured. """ if hasattr(self, "config"): if "saucelabs" in self.cf["browsers"][self.cf["saunter"]["default_browser"]] and not self.cf["browsers"][self.cf["saunter"]["default_browser"]]["saucelabs"]["ondemand"]: self.take_named_screenshot("final") if hasattr(self, "driver"): self.driver.quit() def take_numbered_screenshot(self): if self.config.has_option("Saunter", "take_screenshots"): if self.cf.getboolean("Saunter", "take_screenshots"): method_dir = self._screenshot_prep_dirs() self.driver.get_screenshot_as_file(os.path.join(method_dir, str(self._screenshot_number).zfill(3) + ".png")) self._screenshot_number = self._screenshot_number + 1 if self.config.has_option("Saunter", "jenkins"): if self.cf.getboolean("Saunter", "jenkins"): sys.stdout.write(os.linesep + "[[ATTACHMENT|%s]]" % image_path + os.linesep) def take_named_screenshot(self, name): method_dir = self._screenshot_prep_dirs() image_path = os.path.join(method_dir, str(name) + ".png") self.driver.get_screenshot_as_file(image_path) if "ci_type" in self.cf and self.cf["ci_type"].lower() == "jenkins": sys.stdout.write(os.linesep + "[[ATTACHMENT|%s]]" % image_path + os.linesep)
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 __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)
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
class SaunterTestCase(BaseTestCase): """ Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup and teardown """ 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 teardown_method(self, method): """ Default teardown method for all scripts. If run through Sauce Labs OnDemand, the job name, status and tags are updated. Also the video and server log are downloaded if so configured. """ if hasattr(self, "config"): if "saucelabs" in self.cf["browsers"][ self.cf["saunter"] ["default_browser"]] and not self.cf["browsers"][self.cf[ "saunter"]["default_browser"]]["saucelabs"]["ondemand"]: self.take_named_screenshot("final") if hasattr(self, "driver"): self.driver.quit() def take_numbered_screenshot(self): if self.config.has_option("Saunter", "take_screenshots"): if self.cf.getboolean("Saunter", "take_screenshots"): method_dir = self._screenshot_prep_dirs() self.driver.get_screenshot_as_file( os.path.join( method_dir, str(self._screenshot_number).zfill(3) + ".png")) self._screenshot_number = self._screenshot_number + 1 if self.config.has_option("Saunter", "jenkins"): if self.cf.getboolean("Saunter", "jenkins"): sys.stdout.write(os.linesep + "[[ATTACHMENT|%s]]" % image_path + os.linesep) def take_named_screenshot(self, name): method_dir = self._screenshot_prep_dirs() image_path = os.path.join(method_dir, str(name) + ".png") self.driver.get_screenshot_as_file(image_path) if "ci_type" in self.cf and self.cf["ci_type"].lower() == "jenkins": sys.stdout.write(os.linesep + "[[ATTACHMENT|%s]]" % image_path + os.linesep)
from browsermobproxy import Server, Client server = Server( r"C:\Program Files (x86)\browsermob-proxy-2.1.4\bin\browsermob-proxy", ) server.start() client = Client("localhost:8080") print(client.port) print(server.port) client.new_har('google') 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})
from browsermobproxy import Server, Client server = Server(r"d:\Python\browsermob-proxy-2.0-beta-9-bin\browsermob-proxy-2.0-beta-9\bin\browsermob-proxy",{"port":9090}) server.start() client = Client("localhost:9090") client.port client.new_har('google') client.har
class SaunterTestCase(BaseTestCase): """ Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup and teardown """ 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 teardown_method(self, method): """ Default teardown method for all scripts. If run through Sauce Labs OnDemand, the job name, status and tags are updated. Also the video and server log are downloaded if so configured. """ if hasattr(self, "driver"): self.driver.quit() if hasattr(self, "cf") and self.cf.getboolean("SauceLabs", "ondemand"): # session couldn't be established for some reason if not hasattr(self, "sauce_session"): return sauce_session = self.sauce_session j = {} # name j["name"] = self._testMethodName # result if self._resultForDoCleanups._excinfo == None and not self.verificationErrors: # print("pass") j["passed"] = True else: # print("fail") j["passed"] = False # tags j["tags"] = [] for keyword in self._resultForDoCleanups.keywords: if isinstance(self._resultForDoCleanups.keywords[keyword], MarkInfo): j["tags"].append(keyword) # update which_url = "https://saucelabs.com/rest/v1/%s/jobs/%s" % ( self.cf.get("SauceLabs", "username"), sauce_session) r = requests.put(which_url, data=json.dumps(j), headers={"Content-Type": "application/json"}, auth=(self.cf.get("SauceLabs", "username"), self.cf.get("SauceLabs", "key"))) r.raise_for_status() if self.cf.getboolean("SauceLabs", "get_video"): self.fetch_sauce_artifact("video.flv") if self.cf.getboolean("SauceLabs", "get_log"): self.fetch_sauce_artifact("selenium-server.log")
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()), }