def start_local(): try: # creates an instance of Local BS_LOCAL = Local() # replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY". bs_local_args = {"key": BROWSERSTACK_ACCESS_KEY} # starts the Local instance with the required arguments BS_LOCAL.start(**bs_local_args) # check if BrowserStack local instance is running print("bs_local running:", BS_LOCAL.isRunning()) except Exception as e: print('Exception occured while instantiating web driver locally' + str(e))
def connect_bs_to_local(key): """ Create a connection between Browserstack and localhost """ from browserstack.local import Local bs_local = Local() bs_local_args = { "key": key, "forcelocal": "true", } bs_local.start(**bs_local_args) logger.info('Browerstack local connection:', bs_local.isRunning()) # The default Browserstack local.stop() isn't working for some reason. # But actually there is no need to close the connection as it is running inside a Docker container that gets killed after the test is done. # def stop_bs_local(): # bs_local.stop()
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
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())
from common import start_time from common import datetime from common import test_runner from browserstack.local import Local #creates an instance of Local bs_local = Local() #replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY". bs_local_args = {"key": "", "forcelocal": "true"} #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()) big_dict = gsheet.big_dict gc = gsheet.gc wks = gc.open("BoomAudit").worksheet('PWA Logs') #print(big_dict) desired_cap_list = [] browser_list_array = big_dict.get('browsers') for browser_list in browser_list_array: desired_cap_list.append({ 'browser': browser_list[0], 'browser_version': browser_list[1], 'os': browser_list[2], 'os_version': browser_list[3],
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()