Example #1
0
    def get_browser_instance(self):
        try:
            if self.browser.lower() == "firefox":
                options = FirefoxOptions()
                if self.headless:
                    options.add_argument("--headless")
                    options.add_argument("-width=1920")
                    options.add_argument("-height=1080")
                # options.add_argument("--disable-gpu")
                profile = webdriver.FirefoxProfile()
                # options.add_argument("--private")
                profile.accept_untrusted_certs = True
                service = Service(executable_path=GeckoDriverManager(
                    cache_valid_range=10).install())
                driver = webdriver.Firefox(service=service,
                                           firefox_profile=profile,
                                           options=options)

            elif self.browser.lower() == "chrome":
                options = ChromeOptions()
                if self.headless:
                    options.add_argument('headless')
                    options.add_argument('window-size=1920x1080')
                if self.mobile:
                    mobile_emulation = {"deviceName": self.device}
                    options.add_experimental_option("mobileEmulation",
                                                    mobile_emulation)
                options.add_argument('ignore-certificate-errors')
                options.add_argument('--incognito')
                # options.add_argument('--start-maximized')
                options.add_experimental_option('useAutomationExtension',
                                                False)
                options.add_experimental_option("excludeSwitches",
                                                ["enable-automation"])
                # options.add_experimental_option("excludeSwitches", ["enable-logging"])
                options.add_argument('--log-level=3')
                service = Service(
                    ChromeDriverManager(cache_valid_range=10).install())
                driver = webdriver.Chrome(service=service, options=options)

            elif self.browser.lower() == "ie":
                driver = webdriver.Ie(IEDriverManager().install())

            elif self.browser.lower() == "edge":
                options = EdgeOptions()
                if self.headless:
                    options.add_argument('headless')
                    options.add_argument('window-size=1920x1080')
                options.use_chromium = True
                options.add_argument('ignore-certificate-errors')
                options.add_experimental_option('useAutomationExtension',
                                                False)
                options.add_argument('--inprivate')
                options.add_argument('--log-level=3')
                options.add_experimental_option("excludeSwitches",
                                                ["enable-automation"])
                service = Service(
                    EdgeChromiumDriverManager(cache_valid_range=10,
                                              log_level=1).install())
                driver = webdriver.Chrome(service=service, options=options)

            elif self.browser.lower() == 'browserstack':
                bs_local = Local()
                bs_local_args = {
                    "key": key,
                    "localIdentifier": localIdentifier
                }
                bs_local.start(**bs_local_args)
                driver = webdriver.Remote(command_executor=bb_url,
                                          desired_capabilities=browser_config)

            # elif self.browser.lower() =="docker":
            #     options = FirefoxOptions()
            #     options.add_argument("--headless")
            #     options.add_argument("-width=1920")
            #     options.add_argument("-height=1080")
            #     profile = webdriver.FirefoxProfile()
            #     # options.add_argument("--private")
            #     profile.accept_untrusted_certs = True
            #     driver = webdriver.Remote(command_executor="http://localhost:4444", options=options, browser_profile=profile)

            else:
                raise ValueError

            if self.headless:
                self.cl.info("Starting " + str(self.browser).upper() +
                             " browser in headless mode")
            else:
                self.cl.info("Starting " + str(self.browser).upper() +
                             " browser ")
                if self.browser.lower(
                ) == "browserstack" and 'browserName' in browser_config.keys():
                    pass
                else:
                    driver.maximize_window()

            driver.delete_all_cookies()
            # driver.set_page_load_timeout(30)

            if self.baseUrl:
                driver.get(self.baseUrl)
                self.cl.info("Opening the URL :: " + str(self.baseUrl))

            # driver.implicitly_wait(10)
            return driver

        except ValueError as val:
            self.cl.error(
                "Browser not supported :: " + str(self.browser) +
                ". Supported browser types are Chrome, Firefox, Edge. Exception occurred. :: "
                + str(val.__class__.__name__) + ' ' + str(val))
            raise val

        except Exception as e:
            self.cl.error("Exception occurred. :: " +
                          str(e.__class__.__name__) + ' ' + str(e))
            raise e
Example #2
0
def initialize():
    b = args.browser.lower()
    d = args.debug
    global browser

    print("\n\aConnecting to Saavn...\n")
    driver_dir = os.path.dirname(os.path.realpath(__file__))
    if b.startswith('firefox'):
        opt = FireOptions()
        opt.headless = True

        if sys.platform.startswith('linux'):
            path = os.path.join(driver_dir,'drivers','linux','geckodriver')
        elif sys.platform == 'darwin':
            path = os.path.join(driver_dir, 'drivers', 'mac', 'geckodriver')
        else:
            path = os.path.join(driver_dir,'drivers','windows','geckodriver.exe')

        try:
            if d == 'on':
                print("Debugging mode turned ON...")
                browser = webdriver.Firefox(executable_path=path,service_log_path=os.path.devnull)
            else:
                raise IndexError
        except IndexError:
            browser = webdriver.Firefox(executable_path=path,options=opt,service_log_path=os.path.devnull)
    elif b.startswith('chrome'):
        opt = ChromeOptions()
        opt.add_argument("--log-level=3")
        opt.add_argument("--window-size=1366,768")

        if d == 'on':
            print("Debugging mode turned ON...")
        else:
            opt.add_argument("--headless")

        if sys.platform == 'linux':
            path = os.path.join(driver_dir,'drivers','linux','chromedriver')
        elif sys.platform == 'darwin':
            path = os.path.join(driver_dir, 'drivers', 'mac', 'chromedriver')
        else:
            path = os.path.join(driver_dir,'drivers','windows','chromedriver.exe')
    
        browser = webdriver.Chrome(executable_path=path,options=opt,service_log_path=os.path.devnull)
    elif b.startswith('microsoft'):
        opt = EdgeOptions()
        opt.use_chromium = True

        opt.add_argument("--log-level=3")
        opt.add_argument("--window-size=1366,768")
        del opt.capabilities['platform']

        if d == 'on':
            print("Debugging mode turned ON...")
        else:
            opt.add_argument('--headless')

        if sys.platform == 'linux':
            path = os.path.join(driver_dir,'drivers','linux','msedgedriver')
        elif sys.platform == 'darwin':
            path = os.path.join(driver_dir, 'drivers', 'mac', 'msedgedriver')
        else:
            path = os.path.join(driver_dir,'drivers','windows','msedgedriver.exe')
        
        browser = Edge(executable_path=path, options=opt, service_log_path=os.path.devnull)
        
    return True
Example #3
0
    def get_browser_instance(self):
        try:
            if self.browser.lower() == "firefox":
                options = FirefoxOptions()
                if self.headless:
                    options.add_argument("--headless")
                #options.add_argument("--disable-gpu")
                profile = webdriver.FirefoxProfile()
                #options.add_argument("--private")
                # options.add_argument("-width=1920")
                # options.add_argument("-height=1080")
                profile.accept_untrusted_certs = True
                driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_profile=profile,
                                           options=options)

            elif self.browser.lower() == "chrome":
                chrome_options = Options()
                if self.headless:
                    chrome_options.add_argument('headless')
                #chrome_options.add_argument('window-size=1920x1080')
                chrome_options.add_argument('ignore-certificate-errors')
                chrome_options.add_argument('--incognito')
                chrome_options.add_argument('--start-maximized')
                # chrome_options.add_experimental_option('prefs', {'geolocation': True})
                chrome_options.add_experimental_option('useAutomationExtension', False)
                chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
                chrome_options.add_argument('--log-level=3')
                # driver = webdriver.Chrome(options=chrome_options, executable_path='drivers//chromedriver.exe')
                driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)

            elif self.browser.lower() == "ie":
                driver = webdriver.Ie(IEDriverManager().install())

            elif self.browser.lower() == "edge":
                options = EdgeOptions()
                if self.headless:
                    options.add_argument('headless')
                options.use_chromium = True
                #options.add_argument('window-size=1920x1080')
                options.add_argument('ignore-certificate-errors')
                options.add_experimental_option('useAutomationExtension', False)
                options.add_argument('--inprivate')
                options.add_argument('--log-level=3')
                options.add_experimental_option("excludeSwitches", ["enable-automation"])
                driver = webdriver.Chrome(EdgeChromiumDriverManager().install(), options=options)

            elif self.browser.lower() == 'browserstack':
                # bs_local = Local()
                # bs_local_args = {"key": key,"localIdentifier": localIdentifier}
                # bs_local.start(**bs_local_args)
                driver = webdriver.Remote(command_executor=bb_url, desired_capabilities=browser_config)

            else:
                raise ValueError

            if self.headless:
                self.cl.info("Starting " + str(self.browser).upper() + " browser in headless mode")
            else:
                self.cl.info("Starting " + str(self.browser).upper() + " browser ")
            driver.maximize_window()
            # if self.baseUrl:
            #     driver.get(self.baseUrl)
            #     self.cl.info("Opening the URL :: " + str(self.baseUrl))

            driver.implicitly_wait(5)
            driver.delete_all_cookies()
            driver.set_page_load_timeout(20)
            return driver


        except ValueError as e:
            self.cl.error("Browser not supported :: " + str(
                self.browser) + ". Supported browser types are Chrome, Firefox, Edge. Exception occurred. :: " + str(
                e.__class__.__name__) + ' ' + str(e))
            raise e

        except Exception as e:
            self.cl.error("Exception occurred. :: " + str(
                e.__class__.__name__) + ' ' + str(e))
            raise e