Best Python code snippet using ATX
MyChromedriver.py
Source:MyChromedriver.py
...18class ChromeDriver(object):19 def __init__(self, d, port=9515):20 self._d = d21 self._port = port22 def _launch_webdriver(self):23 print("start chromedriver instance")24 p = subprocess.Popen(['chromedriver', '--port=' + str(self._port)])25 try:26 p.wait(timeout=2.0)27 return False28 except subprocess.TimeoutExpired:29 return True30 def driver(self, device_ip=None, package=None, attach=True, activity=None, process=None):31 """32 Args:33 - package(string): default current running app34 - attach(bool): default true, Attach to an already-running app instead of launching the app with a clear data directory35 - activity(string): Name of the Activity hosting the WebView.36 - process(string): Process name of the Activity hosting the WebView (as given by ps).37 If not given, the process name is assumed to be the same as androidPackage.38 Returns:39 selenium driver40 """41 # if device_ip:42 # subprocess.call(['adb', 'tcpip', '5555'])43 # subprocess.call(['adb', 'connect', str(device_ip)])44 app = self._d.app_current()45 appname = self._d.info46 # é
置微信H547 if package == 'com.tencent.mm' or appname['currentPackageName'] == 'com.tencent.mm':48 process = 'com.tencent.mm:tools'49 capabilities = {50 'chromeOptions': {51 'androidDeviceSerial': device_ip or self._d.serial,52 'androidPackage': package or app['package'],53 'androidUseRunningApp': attach,54 'androidProcess': process or app['package'],55 'androidActivity': activity or app['activity'],56 }57 }58 try:59 dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)60 except URLError:61 self._launch_webdriver()62 dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)63 except MaxRetryError:64 self._launch_webdriver()65 dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)66 except WebDriverException:67 self.windows_kill()68 self._launch_webdriver()69 dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)70 # always quit driver when done71 atexit.register(dr.quit)72 return dr73 def windows_kill(self):74 subprocess.call(['taskkill', '/F', '/IM', 'chromedriver.exe', '/T'])75if __name__ == '__main__':76 import atx77 d = atx.connect()78 driver = ChromeDriver(d).driver()79 elem = driver.find_element_by_link_text(u"ç»å½")80 elem.click()...
chromedriver.py
Source:chromedriver.py
...18class ChromeDriver(object):19 def __init__(self, d, port=9515):20 self._d = d21 self._port = port22 def _launch_webdriver(self):23 print("start chromedriver instance")24 p = subprocess.Popen(['chromedriver', '--port='+str(self._port)])25 try:26 p.wait(timeout=2.0)27 return False28 except subprocess.TimeoutExpired:29 return True30 def driver(self, device_ip=None, package=None, attach=True, activity=None, process=None):31 """32 Args:33 - package(string): default current running app34 - attach(bool): default true, Attach to an already-running app instead of launching the app with a clear data directory35 - activity(string): Name of the Activity hosting the WebView.36 - process(string): Process name of the Activity hosting the WebView (as given by ps).37 If not given, the process name is assumed to be the same as androidPackage.38 Returns:39 selenium driver40 """41 if device_ip != None:42 subprocess.call(['adb','tcpip','5555'])43 subprocess.Popen(['adb','connect',str(device_ip)])44 app = self._d.app_current45 capabilities = {46 'chromeOptions': {47 'androidDeviceSerial': device_ip or self._d.serial,48 'androidPackage': package or self._d.info['currentPackageName'],49 'androidUseRunningApp': attach,50 'androidProcess': process or package or self._d.info['currentPackageName'],51 # 'androidActivity':'com.bfsggame.AppActivity'52 'androidActivity':activity or app['activity'],53 }54 }55 try:56 # dr = webdriver.Remote('http://localhost:%d' %57 # self._port, capabilities)58 dr = webdriver.Remote('http://localhost:%d' %59 self._port, capabilities)60 except URLError:61 self._launch_webdriver()62 dr = webdriver.Remote('http://localhost:%d' %63 self._port, capabilities)64 except MaxRetryError:65 self._launch_webdriver()66 dr = webdriver.Remote('http://localhost:%d' %67 self._port, capabilities)68 # always quit driver when done69 atexit.register(dr.quit)70 return dr71 def windows_kill(self):72 subprocess.call(['taskkill', '/F', '/IM', 'chromedriver.exe', '/T'])73if __name__ == '__main__':74 import atx75 d = atx.connect()76 driver = ChromeDriver(d).driver()77 elem = driver.find_element_by_link_text(u"ç»å½")78 elem.click()79 driver.quit()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!