def browserstack_local(access_key): """Start browserstack local tool as a background process""" # TODO This can be deprecated once # https://github.com/browserstack/browserstack-local-python/pull/28 is # merged (it may not be merged because it's a relatively inactive repo) local = Local(key=access_key) try: local.start() yield local finally: local.stop()
class TestLocal(unittest.TestCase): def setUp(self): self.local = Local('sKUQixMHzMLvVtAqysUN') def tearDown(self): self.local.stop() def test_start_local(self): self.local.start() self.assertNotEqual(self.local.proc.pid, 0) def test_verbose(self): self.local.verbose(True) self.assertTrue('-v' in self.local._generate_args()) def test_local_folder(self): self.local.local_folder('hello') self.assertTrue('-f' in self.local._generate_args()) self.assertTrue('hello' in self.local._generate_args()) def test_force_kill(self): self.local.force_kill(True) self.assertTrue('-force' in self.local._generate_args()) def test_only_automate(self): self.local.only_automate(True) self.assertTrue('-onlyAutomate' in self.local._generate_args()) def test_force_local(self): self.local.force_local(True) self.assertTrue('-forcelocal' in self.local._generate_args()) def test_proxy(self): self.local.proxy('localhost', 2000, 'hello', 'test123') self.assertTrue( "-proxyHost localhost -proxyPort 2000 -proxyUser hello -proxyPass test123" in self.local._generate_args()) def test_local_identifier(self): self.local.local_identifier('mytunnel') self.assertTrue( '-localIdentifier mytunnel' in self.local._generate_args())
def run_suite(self, suite, **kwargs): """Override run_suite with a for loop function.""" suite._cleanup = False bs_local = Local() bs_local_args = {"forcelocal": "true"} bs_local.start(**bs_local_args) print( # noqa: T001 "Browser stack is %srunning..." % ("" if bs_local.isRunning() else "not ")) result = {} for cap in getattr(settings, "BROWSERSTACK_CAPS", {}): settings.WEBDRIVER = cap kwargs = self.get_test_runner_kwargs() runner = self.test_runner(**kwargs) result = runner.run(suite) # return last result.. not sure how to merge them bs_local.stop() return result
desired_capabilities=desired_cap) print("Local Test Started") driver.get("localhost:8000") # Local Environment time.sleep(10) if "Local Server" in driver.title: requests.put('https://' + BROWSERSTACK_USERNAME + ':' + BROWSERSTACK_ACCESS_KEY + '@api.browserstack.com/automate/sessions/' + driver.session_id + '.json', data={ "status": "passed", "reason": "Local Server title matched" }) print("Marked Test Pass using REST API") # Rest Api For Pass! else: requests.put('https://' + BROWSERSTACK_USERNAME + ':' + BROWSERSTACK_ACCESS_KEY + '@api.browserstack.com/automate/sessions/' + driver.session_id + '.json', data={ "status": "failed", "reason": "Local Server title not found" }) print("Marked Test Fail using REST API") # Rest Api for Fail ! finally: bs_local.stop() driver.quit()
class TestBrowserStackSearch(unittest.TestCase): _multiprocess_shared_ = True driver = None bs_local = None @classmethod def create_driver(self): desired_capabilities = TestBrowserStackSearch.get_caps() return webdriver.Remote( desired_capabilities=desired_capabilities, command_executor="http://%s:%[email protected]/wd/hub" % (USERNAME, BROWSERSTACK_ACCESS_KEY)) @classmethod def get_caps(self): desired_capabilities = {} desired_capabilities['os'] = 'OS X' desired_capabilities['os_version'] = 'El Capitan' desired_capabilities['browser'] = 'firefox' desired_capabilities['browser_version'] = '46' desired_capabilities['build'] = 'Sample python tests using unittest' desired_capabilities['name'] = 'Sample python unittest' if 'local' in test_type.lower(): desired_capabilities['browserstack.local'] = True return desired_capabilities @classmethod def start_local(self): self.bs_local = Local() bs_local_args = {"key": "sUiJatw6NhJZpsttcY35", "forcelocal": "true"} self.bs_local.start(**bs_local_args) @classmethod def stop_local(self): if self.bs_local is not None: self.bs_local.stop() @classmethod def setUpClass(self): if 'local' in test_type.lower(): self.start_local() if 'parallel' not in test_type.lower(): self.driver = TestBrowserStackSearch.create_driver() @classmethod def tearDownClass(self): self.stop_local() if 'parallel' not in test_type.lower(): self.driver.quit() def setUp(self): if 'parallel' in test_type.lower(): self.driver = TestBrowserStackSearch.create_driver() def tearDown(self): if 'parallel' in test_type.lower(): self.driver.quit() def test_google_search(self): self.driver.get("http://www.google.com") if not "Google" in self.driver.title: raise Exception("Unable to load google page!") elem = self.driver.find_element_by_name("q") elem.send_keys("BrowserStack") elem.submit() self.assertTrue("browserstack" in self.driver.title.lower()) def test_google_search_clone(self): self.driver.get("http://www.google.com") if not "Google" in self.driver.title: raise Exception("Unable to load google page!") elem = self.driver.find_element_by_name("q") elem.send_keys("BrowserStack") elem.submit() self.assertTrue("browserstack" in self.driver.title.lower())
class TestBrowserStackSearch(unittest.TestCase): _multiprocess_shared_ = True driver = None bs_local = None @classmethod def create_driver(self): desired_capabilities = TestBrowserStackSearch.get_caps() return webdriver.Remote( desired_capabilities=desired_capabilities, command_executor="http://%s:%[email protected]/wd/hub" % ( USERNAME, BROWSERSTACK_ACCESS_KEY ) ) @classmethod def get_caps(self): desired_capabilities = {} desired_capabilities['os'] = 'OS X' desired_capabilities['os_version'] = 'El Capitan' desired_capabilities['browser'] = 'firefox' desired_capabilities['browser_version'] = '46' desired_capabilities['build'] = 'Sample python tests using unittest' desired_capabilities['name'] = 'Sample python unittest' if 'local' in test_type.lower(): desired_capabilities['browserstack.local'] = True return desired_capabilities @classmethod def start_local(self): self.bs_local = Local() bs_local_args = { "key": "sUiJatw6NhJZpsttcY35", "forcelocal": "true" } self.bs_local.start(**bs_local_args) @classmethod def stop_local(self): if self.bs_local is not None: self.bs_local.stop() @classmethod def setUpClass(self): if 'local' in test_type.lower(): self.start_local() if 'parallel' not in test_type.lower(): self.driver = TestBrowserStackSearch.create_driver() @classmethod def tearDownClass(self): self.stop_local() if 'parallel' not in test_type.lower(): self.driver.quit() def setUp(self): if 'parallel' in test_type.lower(): self.driver = TestBrowserStackSearch.create_driver() def tearDown(self): if 'parallel' in test_type.lower(): self.driver.quit() def test_google_search(self): self.driver.get("http://www.google.com") if not "Google" in self.driver.title: raise Exception("Unable to load google page!") elem = self.driver.find_element_by_name("q") elem.send_keys("BrowserStack") elem.submit() self.driver.find_element_by_name("btnG").click() self.assertTrue("browserstack" in self.driver.title.lower()) def test_google_search_clone(self): self.driver.get("http://www.google.com") if not "Google" in self.driver.title: raise Exception("Unable to load google page!") elem = self.driver.find_element_by_name("q") elem.send_keys("BrowserStack") elem.submit() self.driver.find_element_by_name("btnG").click() self.assertTrue("browserstack" in self.driver.title.lower())
class TestLocal(unittest.TestCase): def setUp(self): self.local = Local(os.environ['BROWSERSTACK_ACCESS_KEY']) def tearDown(self): self.local.stop() def test_start_local(self): self.local.start() self.assertNotEqual(self.local.proc.pid, 0) def test_running(self): self.assertFalse(self.local.isRunning()) self.local.start() self.assertTrue(self.local.isRunning()) def test_multiple(self): self.assertFalse(self.local.isRunning()) self.local.start() self.assertTrue(self.local.isRunning()) try: self.local2 = Local(os.environ['BROWSERSTACK_ACCESS_KEY']) self.local2.start() except BrowserStackLocalError as e: self.assertEqual(str(e), "Either another browserstack local client is running on your machine or some server is listening on port 45691") def test_verbose(self): self.local.start(v=True, onlyCommand=True) self.assertIn('-v', self.local._generate_cmd()) def test_local_folder(self): self.local.start(f='hello', onlyCommand=True) self.assertIn('-f', self.local._generate_cmd()) self.assertIn('hello', self.local._generate_cmd()) def test_force_kill(self): self.local.start(force=True, onlyCommand=True) self.assertIn('-force', self.local._generate_cmd()) def test_only_automate(self): self.local.start(onlyAutomate=True, onlyCommand=True) self.assertIn('-onlyAutomate', self.local._generate_cmd()) def test_force_local(self): self.local.start(forcelocal=True, onlyCommand=True) self.assertIn('-forcelocal', self.local._generate_cmd()) def test_custom_boolean_argument(self): self.local.start(boolArg1=True, boolArg2=True, onlyCommand=True) self.assertIn('-boolArg1', self.local._generate_cmd()) self.assertIn('-boolArg2', self.local._generate_cmd()) def test_custom_keyval(self): self.local.start(customKey1="custom value1", customKey2="custom value2", onlyCommand=True) self.assertIn('-customKey1', self.local._generate_cmd()) self.assertIn('custom value1', self.local._generate_cmd()) self.assertIn('-customKey2', self.local._generate_cmd()) self.assertIn('custom value2', self.local._generate_cmd()) def test_proxy(self): self.local.start(proxyHost='localhost', proxyPort=2000, proxyUser='******', proxyPass='******', onlyCommand=True) self.assertIn('-proxyHost', self.local._generate_cmd()) self.assertIn('localhost', self.local._generate_cmd()) self.assertIn('-proxyPort', self.local._generate_cmd()) self.assertIn(2000, self.local._generate_cmd()) self.assertIn('-proxyUser', self.local._generate_cmd()) self.assertIn('hello', self.local._generate_cmd()) self.assertIn('-proxyPass', self.local._generate_cmd()) self.assertIn('test123', self.local._generate_cmd()) def test_force_proxy(self): self.local.start(forceproxy=True, onlyCommand=True) self.assertIn('-forceproxy', self.local._generate_cmd()) def test_local_identifier(self): self.local.start(localIdentifier='mytunnel', onlyCommand=True) self.assertIn('-localIdentifier', self.local._generate_cmd()) self.assertIn('mytunnel', self.local._generate_cmd())
def Download_file_to_machine(self): desired_cap = { "browser": "chrome", "browser_version": "latest", "os": "windows", "os_version": "10", 'build': 'Teladoc', 'name': 'Teladoc Test', 'browserstack.local': 'true' } bs_local = Local() # You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY". bs_local_args = { "key": "yourKey", "f": "/Users/yourFolder/Documents/Local_Testing/Local" } # Starts the Local instance with the required arguments bs_local.start(**bs_local_args) # Check if BrowserStack local instance is running print(bs_local.isRunning()) driver = webdriver.Remote( desired_capabilities=desired_cap, command_executor= 'https://*****:*****@hub-cloud.browserstack.com/wd/hub') # Navigate to the link driver.get("http://username.browserstack.com") # Accept the cookie popup time.sleep(2) url = 'download/11_01_2020 01_26 AM PDT — 11_10_2020 05_26 AM PST.xml' # Find element by class name and store in variable "element" element = driver.find_element_by_xpath('//a[@href="' + url + '"]') time.sleep(2) # Click on the element to download the file element.click() time.sleep(2) # Check if file exists file_exists = driver.execute_script( 'browserstack_executor: {"action": "fileExists"}') print(file_exists) # Check file properties file_properties = driver.execute_script( 'browserstack_executor: {"action": "getFileProperties"}') print(file_properties) get_file_content = driver.execute_script( 'browserstack_executor: {"action": "getFileContent"}') time.sleep(2) # Decode the content to Base64 and write to a file data = base64.b64decode(get_file_content) f = open("11_01_2020 01_26 AM PDT — 11_10_2020 05_26 AM PST.xml", "wb") f.write(data) # Stop the Local instance bs_local.stop() driver.quit()