robotframework-appiumwindows 0.1.0__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- from .elementfinder import ElementFinder
4
-
5
- __all__ = [
6
- "ElementFinder",
7
- ]
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from .elementfinder import ElementFinder
4
+
5
+ __all__ = [
6
+ "ElementFinder",
7
+ ]
@@ -1,264 +1,264 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- from AppiumLibrary import utils
4
- from appium.webdriver.common.appiumby import AppiumBy
5
- from robot.api import logger
6
-
7
-
8
- class ElementFinder(object):
9
-
10
- def __init__(self):
11
- self._strategies = {
12
- 'identifier': self._find_by_identifier,
13
- 'id': self._find_by_id,
14
- 'name': self._find_by_name,
15
- 'xpath': self._find_by_xpath,
16
- 'class': self._find_by_class_name,
17
- 'accessibility_id': self._find_element_by_accessibility_id,
18
- 'android': self._find_by_android,
19
- 'viewtag': self._find_by_android_viewtag,
20
- 'data_matcher': self._find_by_android_data_matcher,
21
- 'view_matcher': self._find_by_android_view_matcher,
22
- 'ios': self._find_by_ios,
23
- 'css': self._find_by_css_selector,
24
- 'jquery': self._find_by_sizzle_selector,
25
- 'predicate': self._find_by_ios_predicate,
26
- 'chain': self._find_by_chain,
27
- 'default': self._find_by_default
28
- }
29
-
30
- def find(self, application, locator, tag=None):
31
- assert application is not None
32
- assert locator is not None and len(locator) > 0
33
-
34
- (prefix, criteria) = self._parse_locator(locator)
35
- prefix = 'default' if prefix is None else prefix
36
- strategy = self._strategies.get(prefix)
37
- if strategy is None:
38
- raise ValueError("Element locator with prefix '" + prefix + "' is not supported")
39
- (tag, constraints) = self._get_tag_and_constraints(tag)
40
- return strategy(application, criteria, tag, constraints)
41
-
42
- # Strategy routines, private
43
-
44
- def _find_by_identifier(self, application, criteria, tag, constraints):
45
- elements = self._normalize_result(application.find_elements(by=AppiumBy.ID, value=criteria))
46
- elements.extend(self._normalize_result(application.find_elements(by=AppiumBy.NAME, value=criteria)))
47
- return self._filter_elements(elements, tag, constraints)
48
-
49
- def _find_by_id(self, application, criteria, tag, constraints):
50
- # print(f"criteria is {criteria}")
51
- return self._filter_elements(
52
- application.find_elements(by=AppiumBy.ID, value=criteria),
53
- tag, constraints)
54
-
55
- def _find_by_name(self, application, criteria, tag, constraints):
56
- return self._filter_elements(
57
- application.find_elements(by=AppiumBy.NAME, value=criteria),
58
- tag, constraints)
59
-
60
- def _find_by_xpath(self, application, criteria, tag, constraints):
61
- return self._filter_elements(
62
- application.find_elements(by=AppiumBy.XPATH, value=criteria),
63
- tag, constraints)
64
-
65
- def _find_by_dom(self, application, criteria, tag, constraints):
66
- result = application.execute_script("return %s;" % criteria)
67
- if result is None:
68
- return []
69
- if not isinstance(result, list):
70
- result = [result]
71
- return self._filter_elements(result, tag, constraints)
72
-
73
- def _find_by_sizzle_selector(self, application, criteria, tag, constraints):
74
- js = "return jQuery('%s').get();" % criteria.replace("'", "\\'")
75
- return self._filter_elements(
76
- application.execute_script(js),
77
- tag, constraints)
78
-
79
- def _find_by_link_text(self, application, criteria, tag, constraints):
80
- return self._filter_elements(
81
- application.find_elements(by=AppiumBy.LINK_TEXT, value=criteria),
82
- tag, constraints)
83
-
84
- def _find_by_css_selector(self, application, criteria, tag, constraints):
85
- return self._filter_elements(
86
- application.find_elements(by=AppiumBy.CSS_SELECTOR, value=criteria),
87
- tag, constraints)
88
-
89
- def _find_by_tag_name(self, application, criteria, tag, constraints):
90
- return self._filter_elements(
91
- application.find_elements(by=AppiumBy.TAG_NAME, value=criteria),
92
- tag, constraints)
93
-
94
- def _find_by_class_name(self, application, criteria, tag, constraints):
95
- return self._filter_elements(
96
- application.find_elements(by=AppiumBy.CLASS_NAME, value=criteria),
97
- tag, constraints)
98
-
99
- def _find_element_by_accessibility_id(self, application, criteria, tag, constraints):
100
- return self._filter_elements(
101
- application.find_elements(by=AppiumBy.ACCESSIBILITY_ID, value=criteria),
102
- tag, constraints)
103
-
104
- def _find_by_android(self, application, criteria, tag, constraints):
105
- """Find element matches by UI Automator."""
106
- return self._filter_elements(
107
- application.find_elements(by=AppiumBy.ANDROID_UIAUTOMATOR, value=criteria),
108
- tag, constraints)
109
-
110
- def _find_by_android_viewtag(self, application, criteria, tag, constraints):
111
- """Find element matches by its view tag
112
- Espresso only
113
- """
114
- return self._filter_elements(
115
- application.find_elements(by=AppiumBy.ANDROID_VIEWTAG, value=criteria),
116
- tag, constraints)
117
-
118
- def _find_by_android_data_matcher(self, application, criteria, tag, constraints):
119
- """Find element matches by Android Data Matcher
120
- Espresso only
121
- """
122
- return self._filter_elements(
123
- application.find_elements(by=AppiumBy.ANDROID_DATA_MATCHER, value=criteria),
124
- tag, constraints)
125
-
126
- def _find_by_android_view_matcher(self, application, criteria, tag, constraints):
127
- """Find element matches by Android View Matcher
128
- Espresso only
129
- """
130
- return self._filter_elements(
131
- application.find_elements(by=AppiumBy.ANDROID_VIEW_MATCHER, value=criteria),
132
- tag, constraints)
133
-
134
- def _find_by_ios(self, application, criteria, tag, constraints):
135
- """Find element matches by UI Automation."""
136
- return self._filter_elements(
137
- application.find_elements(by=AppiumBy.IOS_UIAUTOMATION, value=criteria),
138
- tag, constraints)
139
-
140
- def _find_by_ios_predicate(self, application, criteria, tag, constraints):
141
- """Find element matches by iOSNsPredicateString."""
142
- return self._filter_elements(
143
- application.find_elements(by=AppiumBy.IOS_PREDICATE, value=criteria),
144
- tag, constraints)
145
-
146
- def _find_by_chain(self, application, criteria, tag, constraints):
147
- """Find element matches by iOSChainString."""
148
- return self._filter_elements(
149
- application.find_elements(by=AppiumBy.IOS_CLASS_CHAIN, value=criteria),
150
- tag, constraints)
151
-
152
- def _find_by_default(self, application, criteria, tag, constraints):
153
- if self._is_xpath(criteria):
154
- return self._find_by_xpath(application, criteria, tag, constraints)
155
- # Used `id` instead of _find_by_key_attrs since iOS and Android internal `id` alternatives are
156
- # different and inside appium python client. Need to expose these and improve in order to make
157
- # _find_by_key_attrs useful.
158
- return self._find_by_id(application, criteria, tag, constraints)
159
-
160
- # TODO: Not in use after conversion from Selenium2Library need to make more use of multiple auto selector strategy
161
- def _find_by_key_attrs(self, application, criteria, tag, constraints):
162
- key_attrs = self._key_attrs.get(None)
163
- if tag is not None:
164
- key_attrs = self._key_attrs.get(tag, key_attrs)
165
-
166
- xpath_criteria = utils.escape_xpath_value(criteria)
167
- xpath_tag = tag if tag is not None else '*'
168
- xpath_constraints = ["@%s='%s'" % (name, constraints[name]) for name in constraints]
169
- xpath_searchers = ["%s=%s" % (attr, xpath_criteria) for attr in key_attrs]
170
- xpath_searchers.extend(
171
- self._get_attrs_with_url(key_attrs, criteria, application))
172
- xpath = "//%s[%s(%s)]" % (
173
- xpath_tag,
174
- ' and '.join(xpath_constraints) + ' and ' if len(xpath_constraints) > 0 else '',
175
- ' or '.join(xpath_searchers))
176
- return self._normalize_result(application.find_elements(by=AppiumBy.XPATH, value=xpath))
177
-
178
- # Private
179
- _key_attrs = {
180
- None: ['@id', '@name'],
181
- 'a': ['@id', '@name', '@href', 'normalize-space(descendant-or-self::text())'],
182
- 'img': ['@id', '@name', '@src', '@alt'],
183
- 'input': ['@id', '@name', '@value', '@src'],
184
- 'button': ['@id', '@name', '@value', 'normalize-space(descendant-or-self::text())']
185
- }
186
-
187
- def _get_tag_and_constraints(self, tag):
188
- if tag is None:
189
- return None, {}
190
-
191
- tag = tag.lower()
192
- constraints = {}
193
- if tag == 'link':
194
- tag = 'a'
195
- elif tag == 'image':
196
- tag = 'img'
197
- elif tag == 'list':
198
- tag = 'select'
199
- elif tag == 'radio button':
200
- tag = 'input'
201
- constraints['type'] = 'radio'
202
- elif tag == 'checkbox':
203
- tag = 'input'
204
- constraints['type'] = 'checkbox'
205
- elif tag == 'text field':
206
- tag = 'input'
207
- constraints['type'] = 'text'
208
- elif tag == 'file upload':
209
- tag = 'input'
210
- constraints['type'] = 'file'
211
- return tag, constraints
212
-
213
- def _element_matches(self, element, tag, constraints):
214
- if not element.tag_name.lower() == tag:
215
- return False
216
- for name in constraints:
217
- if not element.get_attribute(name) == constraints[name]:
218
- return False
219
- return True
220
-
221
- def _filter_elements(self, elements, tag, constraints):
222
- elements = self._normalize_result(elements)
223
- if tag is None:
224
- return elements
225
- return filter(
226
- lambda element: self._element_matches(element, tag, constraints),
227
- elements)
228
-
229
- def _get_attrs_with_url(self, key_attrs, criteria, browser):
230
- attrs = []
231
- url = None
232
- xpath_url = None
233
- for attr in ['@src', '@href']:
234
- if attr in key_attrs:
235
- if url is None or xpath_url is None:
236
- url = self._get_base_url(browser) + "/" + criteria
237
- xpath_url = utils.escape_xpath_value(url)
238
- attrs.append("%s=%s" % (attr, xpath_url))
239
- return attrs
240
-
241
- def _get_base_url(self, browser):
242
- url = browser.get_current_url()
243
- if '/' in url:
244
- url = '/'.join(url.split('/')[:-1])
245
- return url
246
-
247
- def _parse_locator(self, locator):
248
- prefix = None
249
- criteria = locator
250
- if not self._is_xpath(locator):
251
- locator_parts = locator.partition('=')
252
- if len(locator_parts[1]) > 0:
253
- prefix = locator_parts[0].strip().lower()
254
- criteria = locator_parts[2].strip()
255
- return (prefix, criteria)
256
-
257
- def _is_xpath(self, locator):
258
- return locator and locator.startswith('/')
259
-
260
- def _normalize_result(self, elements):
261
- if not isinstance(elements, list):
262
- logger.debug("WebDriver find returned %s" % elements)
263
- return []
264
- return elements
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from AppiumLibrary import utils
4
+ from appium.webdriver.common.appiumby import AppiumBy
5
+ from robot.api import logger
6
+
7
+
8
+ class ElementFinder(object):
9
+
10
+ def __init__(self):
11
+ self._strategies = {
12
+ 'identifier': self._find_by_identifier,
13
+ 'id': self._find_by_id,
14
+ 'name': self._find_by_name,
15
+ 'xpath': self._find_by_xpath,
16
+ 'class': self._find_by_class_name,
17
+ 'accessibility_id': self._find_element_by_accessibility_id,
18
+ 'android': self._find_by_android,
19
+ 'viewtag': self._find_by_android_viewtag,
20
+ 'data_matcher': self._find_by_android_data_matcher,
21
+ 'view_matcher': self._find_by_android_view_matcher,
22
+ 'ios': self._find_by_ios,
23
+ 'css': self._find_by_css_selector,
24
+ 'jquery': self._find_by_sizzle_selector,
25
+ 'predicate': self._find_by_ios_predicate,
26
+ 'chain': self._find_by_chain,
27
+ 'default': self._find_by_default
28
+ }
29
+
30
+ def find(self, application, locator, tag=None):
31
+ assert application is not None
32
+ assert locator is not None and len(locator) > 0
33
+
34
+ (prefix, criteria) = self._parse_locator(locator)
35
+ prefix = 'default' if prefix is None else prefix
36
+ strategy = self._strategies.get(prefix)
37
+ if strategy is None:
38
+ raise ValueError("Element locator with prefix '" + prefix + "' is not supported")
39
+ (tag, constraints) = self._get_tag_and_constraints(tag)
40
+ return strategy(application, criteria, tag, constraints)
41
+
42
+ # Strategy routines, private
43
+
44
+ def _find_by_identifier(self, application, criteria, tag, constraints):
45
+ elements = self._normalize_result(application.find_elements(by=AppiumBy.ID, value=criteria))
46
+ elements.extend(self._normalize_result(application.find_elements(by=AppiumBy.NAME, value=criteria)))
47
+ return self._filter_elements(elements, tag, constraints)
48
+
49
+ def _find_by_id(self, application, criteria, tag, constraints):
50
+ # print(f"criteria is {criteria}")
51
+ return self._filter_elements(
52
+ application.find_elements(by=AppiumBy.ID, value=criteria),
53
+ tag, constraints)
54
+
55
+ def _find_by_name(self, application, criteria, tag, constraints):
56
+ return self._filter_elements(
57
+ application.find_elements(by=AppiumBy.NAME, value=criteria),
58
+ tag, constraints)
59
+
60
+ def _find_by_xpath(self, application, criteria, tag, constraints):
61
+ return self._filter_elements(
62
+ application.find_elements(by=AppiumBy.XPATH, value=criteria),
63
+ tag, constraints)
64
+
65
+ def _find_by_dom(self, application, criteria, tag, constraints):
66
+ result = application.execute_script("return %s;" % criteria)
67
+ if result is None:
68
+ return []
69
+ if not isinstance(result, list):
70
+ result = [result]
71
+ return self._filter_elements(result, tag, constraints)
72
+
73
+ def _find_by_sizzle_selector(self, application, criteria, tag, constraints):
74
+ js = "return jQuery('%s').get();" % criteria.replace("'", "\\'")
75
+ return self._filter_elements(
76
+ application.execute_script(js),
77
+ tag, constraints)
78
+
79
+ def _find_by_link_text(self, application, criteria, tag, constraints):
80
+ return self._filter_elements(
81
+ application.find_elements(by=AppiumBy.LINK_TEXT, value=criteria),
82
+ tag, constraints)
83
+
84
+ def _find_by_css_selector(self, application, criteria, tag, constraints):
85
+ return self._filter_elements(
86
+ application.find_elements(by=AppiumBy.CSS_SELECTOR, value=criteria),
87
+ tag, constraints)
88
+
89
+ def _find_by_tag_name(self, application, criteria, tag, constraints):
90
+ return self._filter_elements(
91
+ application.find_elements(by=AppiumBy.TAG_NAME, value=criteria),
92
+ tag, constraints)
93
+
94
+ def _find_by_class_name(self, application, criteria, tag, constraints):
95
+ return self._filter_elements(
96
+ application.find_elements(by=AppiumBy.CLASS_NAME, value=criteria),
97
+ tag, constraints)
98
+
99
+ def _find_element_by_accessibility_id(self, application, criteria, tag, constraints):
100
+ return self._filter_elements(
101
+ application.find_elements(by=AppiumBy.ACCESSIBILITY_ID, value=criteria),
102
+ tag, constraints)
103
+
104
+ def _find_by_android(self, application, criteria, tag, constraints):
105
+ """Find element matches by UI Automator."""
106
+ return self._filter_elements(
107
+ application.find_elements(by=AppiumBy.ANDROID_UIAUTOMATOR, value=criteria),
108
+ tag, constraints)
109
+
110
+ def _find_by_android_viewtag(self, application, criteria, tag, constraints):
111
+ """Find element matches by its view tag
112
+ Espresso only
113
+ """
114
+ return self._filter_elements(
115
+ application.find_elements(by=AppiumBy.ANDROID_VIEWTAG, value=criteria),
116
+ tag, constraints)
117
+
118
+ def _find_by_android_data_matcher(self, application, criteria, tag, constraints):
119
+ """Find element matches by Android Data Matcher
120
+ Espresso only
121
+ """
122
+ return self._filter_elements(
123
+ application.find_elements(by=AppiumBy.ANDROID_DATA_MATCHER, value=criteria),
124
+ tag, constraints)
125
+
126
+ def _find_by_android_view_matcher(self, application, criteria, tag, constraints):
127
+ """Find element matches by Android View Matcher
128
+ Espresso only
129
+ """
130
+ return self._filter_elements(
131
+ application.find_elements(by=AppiumBy.ANDROID_VIEW_MATCHER, value=criteria),
132
+ tag, constraints)
133
+
134
+ def _find_by_ios(self, application, criteria, tag, constraints):
135
+ """Find element matches by UI Automation."""
136
+ return self._filter_elements(
137
+ application.find_elements(by=AppiumBy.IOS_UIAUTOMATION, value=criteria),
138
+ tag, constraints)
139
+
140
+ def _find_by_ios_predicate(self, application, criteria, tag, constraints):
141
+ """Find element matches by iOSNsPredicateString."""
142
+ return self._filter_elements(
143
+ application.find_elements(by=AppiumBy.IOS_PREDICATE, value=criteria),
144
+ tag, constraints)
145
+
146
+ def _find_by_chain(self, application, criteria, tag, constraints):
147
+ """Find element matches by iOSChainString."""
148
+ return self._filter_elements(
149
+ application.find_elements(by=AppiumBy.IOS_CLASS_CHAIN, value=criteria),
150
+ tag, constraints)
151
+
152
+ def _find_by_default(self, application, criteria, tag, constraints):
153
+ if self._is_xpath(criteria):
154
+ return self._find_by_xpath(application, criteria, tag, constraints)
155
+ # Used `id` instead of _find_by_key_attrs since iOS and Android internal `id` alternatives are
156
+ # different and inside appium python client. Need to expose these and improve in order to make
157
+ # _find_by_key_attrs useful.
158
+ return self._find_by_id(application, criteria, tag, constraints)
159
+
160
+ # TODO: Not in use after conversion from Selenium2Library need to make more use of multiple auto selector strategy
161
+ def _find_by_key_attrs(self, application, criteria, tag, constraints):
162
+ key_attrs = self._key_attrs.get(None)
163
+ if tag is not None:
164
+ key_attrs = self._key_attrs.get(tag, key_attrs)
165
+
166
+ xpath_criteria = utils.escape_xpath_value(criteria)
167
+ xpath_tag = tag if tag is not None else '*'
168
+ xpath_constraints = ["@%s='%s'" % (name, constraints[name]) for name in constraints]
169
+ xpath_searchers = ["%s=%s" % (attr, xpath_criteria) for attr in key_attrs]
170
+ xpath_searchers.extend(
171
+ self._get_attrs_with_url(key_attrs, criteria, application))
172
+ xpath = "//%s[%s(%s)]" % (
173
+ xpath_tag,
174
+ ' and '.join(xpath_constraints) + ' and ' if len(xpath_constraints) > 0 else '',
175
+ ' or '.join(xpath_searchers))
176
+ return self._normalize_result(application.find_elements(by=AppiumBy.XPATH, value=xpath))
177
+
178
+ # Private
179
+ _key_attrs = {
180
+ None: ['@id', '@name'],
181
+ 'a': ['@id', '@name', '@href', 'normalize-space(descendant-or-self::text())'],
182
+ 'img': ['@id', '@name', '@src', '@alt'],
183
+ 'input': ['@id', '@name', '@value', '@src'],
184
+ 'button': ['@id', '@name', '@value', 'normalize-space(descendant-or-self::text())']
185
+ }
186
+
187
+ def _get_tag_and_constraints(self, tag):
188
+ if tag is None:
189
+ return None, {}
190
+
191
+ tag = tag.lower()
192
+ constraints = {}
193
+ if tag == 'link':
194
+ tag = 'a'
195
+ elif tag == 'image':
196
+ tag = 'img'
197
+ elif tag == 'list':
198
+ tag = 'select'
199
+ elif tag == 'radio button':
200
+ tag = 'input'
201
+ constraints['type'] = 'radio'
202
+ elif tag == 'checkbox':
203
+ tag = 'input'
204
+ constraints['type'] = 'checkbox'
205
+ elif tag == 'text field':
206
+ tag = 'input'
207
+ constraints['type'] = 'text'
208
+ elif tag == 'file upload':
209
+ tag = 'input'
210
+ constraints['type'] = 'file'
211
+ return tag, constraints
212
+
213
+ def _element_matches(self, element, tag, constraints):
214
+ if not element.tag_name.lower() == tag:
215
+ return False
216
+ for name in constraints:
217
+ if not element.get_attribute(name) == constraints[name]:
218
+ return False
219
+ return True
220
+
221
+ def _filter_elements(self, elements, tag, constraints):
222
+ elements = self._normalize_result(elements)
223
+ if tag is None:
224
+ return elements
225
+ return filter(
226
+ lambda element: self._element_matches(element, tag, constraints),
227
+ elements)
228
+
229
+ def _get_attrs_with_url(self, key_attrs, criteria, browser):
230
+ attrs = []
231
+ url = None
232
+ xpath_url = None
233
+ for attr in ['@src', '@href']:
234
+ if attr in key_attrs:
235
+ if url is None or xpath_url is None:
236
+ url = self._get_base_url(browser) + "/" + criteria
237
+ xpath_url = utils.escape_xpath_value(url)
238
+ attrs.append("%s=%s" % (attr, xpath_url))
239
+ return attrs
240
+
241
+ def _get_base_url(self, browser):
242
+ url = browser.get_current_url()
243
+ if '/' in url:
244
+ url = '/'.join(url.split('/')[:-1])
245
+ return url
246
+
247
+ def _parse_locator(self, locator):
248
+ prefix = None
249
+ criteria = locator
250
+ if not self._is_xpath(locator):
251
+ locator_parts = locator.partition('=')
252
+ if len(locator_parts[1]) > 0:
253
+ prefix = locator_parts[0].strip().lower()
254
+ criteria = locator_parts[2].strip()
255
+ return (prefix, criteria)
256
+
257
+ def _is_xpath(self, locator):
258
+ return locator and locator.startswith('/')
259
+
260
+ def _normalize_result(self, elements):
261
+ if not isinstance(elements, list):
262
+ logger.debug("WebDriver find returned %s" % elements)
263
+ return []
264
+ return elements
@@ -1,50 +1,50 @@
1
- import os
2
- from pathlib import Path
3
-
4
- from robot.utils import abspath
5
-
6
- from .applicationcache import ApplicationCache
7
-
8
-
9
- def escape_xpath_value(value):
10
- value = str(value)
11
- if '"' in value and '\'' in value:
12
- parts_wo_apos = value.split('\'')
13
- return "concat('%s')" % "', \"'\", '".join(parts_wo_apos)
14
- if '\'' in value:
15
- return "\"%s\"" % value
16
- return "'%s'" % value
17
-
18
-
19
- def read_file(file_path):
20
- with open(_absnorm(file_path), encoding='UTF-8', errors='strict', newline="") as f:
21
- file_content = f.read().replace("\r\n", "\n")
22
- return file_content
23
-
24
-
25
- def _absnorm(path):
26
- return abspath(_normalize_path(path))
27
-
28
-
29
- def _normalize_path(path, case_normalize=False):
30
- """Normalizes the given path.
31
-
32
- - Collapses redundant separators and up-level references.
33
- - Converts ``/`` to ``\\`` on Windows.
34
- - Replaces initial ``~`` or ``~user`` by that user's home directory.
35
- - Converts ``pathlib.Path`` instances to ``str``.
36
- On Windows result would use ``\\`` instead of ``/`` and home directory
37
- would be different.
38
- """
39
- if isinstance(path, Path):
40
- path = str(path)
41
- else:
42
- path = path.replace("/", os.sep)
43
- path = os.path.normpath(os.path.expanduser(path))
44
- # os.path.normcase doesn't normalize on OSX which also, by default,
45
- # has case-insensitive file system. Our robot.utils.normpath would
46
- # do that, but it's not certain would that, or other things that the
47
- # utility do, desirable.
48
- if case_normalize:
49
- path = os.path.normcase(path)
50
- return path or "."
1
+ import os
2
+ from pathlib import Path
3
+
4
+ from robot.utils import abspath
5
+
6
+ from .applicationcache import ApplicationCache
7
+
8
+
9
+ def escape_xpath_value(value):
10
+ value = str(value)
11
+ if '"' in value and '\'' in value:
12
+ parts_wo_apos = value.split('\'')
13
+ return "concat('%s')" % "', \"'\", '".join(parts_wo_apos)
14
+ if '\'' in value:
15
+ return "\"%s\"" % value
16
+ return "'%s'" % value
17
+
18
+
19
+ def read_file(file_path):
20
+ with open(_absnorm(file_path), encoding='UTF-8', errors='strict', newline="") as f:
21
+ file_content = f.read().replace("\r\n", "\n")
22
+ return file_content
23
+
24
+
25
+ def _absnorm(path):
26
+ return abspath(_normalize_path(path))
27
+
28
+
29
+ def _normalize_path(path, case_normalize=False):
30
+ """Normalizes the given path.
31
+
32
+ - Collapses redundant separators and up-level references.
33
+ - Converts ``/`` to ``\\`` on Windows.
34
+ - Replaces initial ``~`` or ``~user`` by that user's home directory.
35
+ - Converts ``pathlib.Path`` instances to ``str``.
36
+ On Windows result would use ``\\`` instead of ``/`` and home directory
37
+ would be different.
38
+ """
39
+ if isinstance(path, Path):
40
+ path = str(path)
41
+ else:
42
+ path = path.replace("/", os.sep)
43
+ path = os.path.normpath(os.path.expanduser(path))
44
+ # os.path.normcase doesn't normalize on OSX which also, by default,
45
+ # has case-insensitive file system. Our robot.utils.normpath would
46
+ # do that, but it's not certain would that, or other things that the
47
+ # utility do, desirable.
48
+ if case_normalize:
49
+ path = os.path.normcase(path)
50
+ return path or "."