Esempio n. 1
0
    def webdriver_setFirefoxProfile(browserProxy, randomProxy=None):
        profile = webdriver.FirefoxProfile()
        if browserProxy:
            profile.set_proxy(browserProxy.selenium_proxy())

        if randomProxy:
            """
            from selenium import webdriver
            return webdriver.Proxy({
                "httpProxy": self.proxy,
                "sslProxy": self.proxy,
            })
            """
            # We shall use a random Proxy from the list:
            PROXY = f"{randomProxy['ip']}:{randomProxy['port']}"

            logger.info(f"Using Proxy-Server: {PROXY}")

            ffProxy = webdriver.Proxy({
                "httpProxy": PROXY,
                "ftpProxy": PROXY,
                "sslProxy": PROXY,
                "noProxy": None,
                "proxy_type": "MANUAL",
                "proxyType": "MANUAL",
                "class": "org.openqa.selenium.Proxy",
                "autodetect": False
            })

            profile.set_proxy(ffProxy)

        profile.set_preference("browser.download.folderList", 2)
        profile.set_preference("browser.helperApps.alwaysAsk.force", False)
        profile.set_preference("browser.download.manager.showWhenStarting",
                               False)
        profile.set_preference("browser.download.manager.showAlertOnComplete",
                               False)
        profile.set_preference(
            'browser.helperApps.neverAsk.saveToDisk',
            'application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf,application/zip,application/octet-stream,application/x-zip-compressed,multipart/x-zip,application/x-rar-compressed, application/octet-stream,application/msword,application/vnd.ms-word.document.macroEnabled.12,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/rtf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,application/vnd.ms-word.document.macroEnabled.12,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/xls,application/msword,text/csv,application/vnd.ms-excel.sheet.binary.macroEnabled.12,text/plain,text/csv/xls/xlsb,application/csv,application/download,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/octet-stream'
        )
        profile.set_preference(
            'browser.helperApps.neverAsk.openFile',
            'application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf,application/zip,application/octet-stream,application/x-zip-compressed,multipart/x-zip,application/x-rar-compressed, application/octet-stream,application/msword,application/vnd.ms-word.document.macroEnabled.12,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/rtf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,application/vnd.ms-word.document.macroEnabled.12,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/xls,application/msword,text/csv,application/vnd.ms-excel.sheet.binary.macroEnabled.12,text/plain,text/csv/xls/xlsb,application/csv,application/download,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/octet-stream'
        )
        profile.set_preference(
            "browser.download.dir",
            helper.browserHelper_setBrowserDownloadDirRandom())
        profile.set_preference("browser.download.manager.useWindow", False)
        profile.set_preference("browser.download.manager.focusWhenStarting",
                               False)
        profile.set_preference("browser.download.manager.showAlertOnComplete",
                               False)
        profile.set_preference("browser.download.manager.closeWhenDone", True)
        profile.set_preference("pdfjs.enabledCache.state", False)
        profile.set_preference(
            "pdfjs.disabled",
            True)  # This is nowhere on the Internet! But that did the trick!

        # Set volume to 0
        profile.set_preference("media.volume_scale", "0.0")
        return profile
Esempio n. 2
0
    def webdriver_createBrowserOptions(browserName,
                                       desiredCapabilities,
                                       browserMobProxy=None,
                                       randomProxy=None):
        """
        Translates desired capabilities from the Testrun (or globals) into specific BrowserOptions for the
        currently active browser

        @param browserName: any of the GC.BROWSER*
        @param desiredCapabilities: Settings from TestRun or globals
        @param browserMobProxy: Proxy-Server IP+Port of internal BrowserMobProxy.
        @param randomProxy: Proxy-Server IP+Port of random external Proxy
        @return: the proper BrowserOptions for the currently active browser.
        """

        # Default Download Directory for Attachment downloads
        if browserName == GC.BROWSER_CHROME:
            lOptions = ChromeOptions()
            prefs = {
                "plugins.plugins_disabled": ["Chrome PDF Viewer"],
                "plugins.always_open_pdf_externally":
                True,
                "profile.default_content_settings.popups":
                0,
                "download.default_directory":
                helper.browserHelper_setBrowserDownloadDirRandom(
                ),  # IMPORTANT - ENDING SLASH V IMPORTANT
                "directory_upgrade":
                True
            }
            lOptions.add_experimental_option("prefs", prefs)
            # Set Proxy for Chrome. First RandomProxy (External), if set. If not, then internal Browsermob
            if randomProxy:
                lOptions.add_argument(
                    f"--proxy-server={randomProxy['ip']}:{randomProxy['port']}"
                )
            elif browserMobProxy:
                lOptions.add_argument('--proxy-server={0}'.format(
                    browserMobProxy.proxy))
        elif browserName == GC.BROWSER_FIREFOX:
            lOptions = ffOptions()
        else:
            lOptions = None

        if desiredCapabilities and lOptions:
            # sometimes instead of DICT comes a string with DICT-Format
            if isinstance(
                    desiredCapabilities, str
            ) and "{" in desiredCapabilities and "}" in desiredCapabilities:
                desiredCapabilities = json.loads(
                    desiredCapabilities.replace("'", '"'))

            if isinstance(desiredCapabilities, dict):
                if desiredCapabilities.get(GC.BROWSER_MODE_HEADLESS):
                    logger.debug("Starting in Headless mode")
                    lOptions.headless = True
                else:
                    # statement not matched
                    pass
            else:
                # statement not matched
                pass
        else:
            # statement not matched
            pass

        return lOptions