Best Python code snippet using webdriver_manager
driver.py
Source:driver.py
...21 self._os_type = os_type22 self._latest_release_url = latest_release_url23 def get_name(self):24 return self._name25 def get_os_type(self):26 return self._os_type27 def get_url(self):28 return f"{self._url}/{self.get_version()}/{self.get_name()}_{self.get_os_type()}.zip"29 def get_version(self):30 driver_version = self._version31 if driver_version == "latest":32 return self.get_latest_release_version()33 return self._version34 def get_latest_release_version(self):35 # type: () -> str36 raise NotImplementedError("Please implement this method")37class ChromeDriver(Driver):38 def __init__(self, name, version, os_type, url, latest_release_url,39 chrome_type=ChromeType.GOOGLE):40 super(ChromeDriver, self).__init__(name, version, os_type, url,41 latest_release_url)42 self.chrome_type = chrome_type43 self.browser_version = chrome_version(chrome_type)44 def get_os_type(self):45 if "win" in super().get_os_type():46 return "win32"47 return super().get_os_type()48 def get_latest_release_version(self):49 log(f"Get LATEST driver version for {self.browser_version}")50 resp = requests.get(f"{self._latest_release_url}_{self.browser_version}")51 validate_response(resp)52 return resp.text.rstrip()53class GeckoDriver(Driver):54 def __init__(self, name,55 version,56 os_type,57 url,58 latest_release_url,59 mozila_release_tag):60 super(GeckoDriver, self).__init__(name, version, os_type, url,61 latest_release_url)62 self._mozila_release_tag = mozila_release_tag63 self._os_token = os.getenv("GH_TOKEN", None)64 self.auth_header = None65 self.browser_version = ""66 if self._os_token:67 log("GH_TOKEN will be used to perform requests", first_line=True)68 self.auth_header = {'Authorization': f'token {self._os_token}'}69 def get_latest_release_version(self):70 # type: () -> str71 resp = requests.get(url=self.latest_release_url,72 headers=self.auth_header)73 validate_response(resp)74 return resp.json()["tag_name"]75 def get_url(self):76 # https://github.com/mozilla/geckodriver/releases/download/v0.11.1/geckodriver-v0.11.1-linux64.tar.gz77 log(f"Getting latest mozilla release info for {self.get_version()}")78 resp = requests.get(url=self.tagged_release_url(self.get_version()),79 headers=self.auth_header)80 validate_response(resp)81 assets = resp.json()["assets"]82 name = f"{self.get_name()}-{self.get_version()}-{self.get_os_type()}"83 output_dict = [asset for asset in assets if84 asset['name'].startswith(name)]85 return output_dict[0]['browser_download_url']86 def get_os_type(self):87 if super().get_os_type().startswith("mac"):88 return "macos"89 return super().get_os_type()90 @property91 def latest_release_url(self):92 return self._latest_release_url93 def tagged_release_url(self, version):94 return self._mozila_release_tag.format(version)95class IEDriver(Driver):96 def __init__(self, name, version,97 os_type,98 url,99 latest_release_url):100 if os_type == "win64":101 os_type = "x64"102 else:103 os_type = "Win32"104 super(IEDriver, self).__init__(version=version,105 os_type=os_type,106 url=url,107 latest_release_url=latest_release_url,108 name=name)109 self.browser_version = ""110 def sortchildrenby(self, container):111 data = []112 for elem in container.iter("Contents"):113 key = elem114 data.append((key, elem))115 data.sort()116 def get_latest_release_version(self):117 resp = requests.get(self._url)118 root = ElementTree.fromstring(resp.text)119 values = {}120 xmlns = '{http://doc.s3.amazonaws.com/2006-03-01}'121 for child in root.findall(xmlns + 'Contents'):122 key = child.find(xmlns + 'Key').text123 if self.get_name() in key and self._os_type in key:124 last_modified = child.find(xmlns + 'LastModified').text125 values[last_modified] = key126 latest_key = values[max(values)]127 # 2.39/IEDriverServer_Win32_2.39.0.zip128 m = re.match(r".*_{os}_(.*)\.zip".format(os=self.get_os_type()),129 latest_key)130 if m:131 return m.group(1)132 else:133 raise ValueError("Can't parse latest version {key} | {os}".format(134 key=latest_key, os=self.get_os_type()))135 def get_url(self):136 major, minor, patch = self.__get_divided_version(self.get_version())137 return ("{url}/{major}.{minor}/"138 "{name}_{os}_{major}.{minor}.{patch}.zip").format(139 url=self._url, name=self.get_name(), os=self.get_os_type(),140 major=major, minor=minor, patch=patch)141 def __get_divided_version(self, version):142 divided_version = version.split('.')143 if len(divided_version) == 2:144 return divided_version[0], divided_version[1], '0'145 elif len(divided_version) == 3:146 return divided_version147 else:148 raise ValueError(149 "Version must consist of major, minor and/or patch, "150 "but given was: {version}".format(version=self.get_version()))151class OperaDriver(Driver):152 def __init__(self, name,153 version,154 os_type,155 url,156 latest_release_url,157 opera_release_tag):158 super(OperaDriver, self).__init__(name, version, os_type, url,159 latest_release_url)160 self.opera_release_tag = opera_release_tag161 self._os_token = os.getenv("GH_TOKEN", None)162 self.auth_header = None163 self.browser_version = ""164 if self._os_token:165 log("GH_TOKEN will be used to perform requests")166 self.auth_header = {'Authorization': f'token {self._os_token}'}167 def get_latest_release_version(self):168 # type: () -> str169 resp = requests.get(self.latest_release_url, headers=self.auth_header)170 validate_response(resp)171 return resp.json()["tag_name"]172 def get_url(self):173 # type: () -> str174 # https://github.com/operasoftware/operachromiumdriver/releases/download/v.2.45/operadriver_linux64.zip175 version = self.get_version()176 log(f"Getting latest opera release info for {version}")177 resp = requests.get(url=self.tagged_release_url(version),178 headers=self.auth_header)179 validate_response(resp)180 assets = resp.json()["assets"]181 name = "{0}_{1}".format(self.get_name(), self.get_os_type())182 output_dict = [asset for asset in assets if183 asset['name'].startswith(name)]184 return output_dict[0]['browser_download_url']185 @property186 def latest_release_url(self):187 return self._latest_release_url188 def tagged_release_url(self, version):189 return self.opera_release_tag.format(version)190class EdgeChromiumDriver(Driver):191 def __init__(self, name, version, os_type, url, latest_release_url):192 super(EdgeChromiumDriver, self).__init__(name, version, os_type, url,193 latest_release_url)194 self.browser_version = ""195 def get_latest_release_version(self):...
android_dev_os_unittest.py
Source:android_dev_os_unittest.py
...22 @mock.patch('platform.system', return_value='Darwin')23 def test_get_os_info_mac(self, mock_system):24 """Test with running in Mac"""25 mock_system.return_value = 'Darwin'26 self.assertTrue(AndroidDevOS.MAC == AndroidDevOS.get_os_type())27 self.assertFalse(AndroidDevOS.LINUX == AndroidDevOS.get_os_type())28 self.assertFalse(AndroidDevOS.WINDOWS == AndroidDevOS.get_os_type())29 self.assertTrue(AndroidDevOS.get_os_name() == 'MAC')30 @mock.patch('platform.system', return_value='Linux')31 def test_get_os_info_linux(self, mock_system):32 """Test with running in Linux"""33 mock_system.return_value = 'Linux'34 self.assertFalse(AndroidDevOS.MAC == AndroidDevOS.get_os_type())35 self.assertTrue(AndroidDevOS.LINUX == AndroidDevOS.get_os_type())36 self.assertFalse(AndroidDevOS.WINDOWS == AndroidDevOS.get_os_type())37 self.assertTrue(AndroidDevOS.get_os_name() == 'LINUX')38 @mock.patch('platform.system', return_value='Windows')39 def test_get_os_info_windows(self, mock_system):40 """Test with running in Windows"""41 mock_system.return_value = 'Windows'42 self.assertFalse(AndroidDevOS.MAC == AndroidDevOS.get_os_type())43 self.assertFalse(AndroidDevOS.LINUX == AndroidDevOS.get_os_type())44 self.assertTrue(AndroidDevOS.WINDOWS == AndroidDevOS.get_os_type())45 self.assertTrue(AndroidDevOS.get_os_name() == 'WINDOWS')46 @mock.patch('platform.system', return_value='None')47 def test_get_os_info_default(self, mock_system):48 """Test with running in unknown"""49 mock_system.return_value = 'None'50 self.assertFalse(AndroidDevOS.MAC == AndroidDevOS.get_os_type())51 self.assertTrue(AndroidDevOS.LINUX == AndroidDevOS.get_os_type())52 self.assertFalse(AndroidDevOS.WINDOWS == AndroidDevOS.get_os_type())53 self.assertTrue(AndroidDevOS.get_os_name() == 'LINUX')54if __name__ == '__main__':...
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!!