| 1859 | } |
| 1860 | |
| 1861 | function parseLocator(locator) { |
| 1862 | if (!locator) return null |
| 1863 | |
| 1864 | if (typeof locator === 'object') { |
| 1865 | if (locator.web && this.isWeb) { |
| 1866 | return parseLocator.call(this, locator.web) |
| 1867 | } |
| 1868 | |
| 1869 | if (locator.android && this.platform === 'android') { |
| 1870 | if (typeof locator.android === 'string') { |
| 1871 | return parseLocator.call(this, locator.android) |
| 1872 | } |
| 1873 | // The locator is an Android DataMatcher or ViewMatcher locator so return as is |
| 1874 | return locator.android |
| 1875 | } |
| 1876 | |
| 1877 | if (locator.ios && this.platform === 'ios') { |
| 1878 | return parseLocator.call(this, locator.ios) |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | if (typeof locator === 'string') { |
| 1883 | if (locator[0] === '~') return locator |
| 1884 | if (locator.substr(0, 2) === '//') return locator |
| 1885 | if (locator[0] === '#' && !this.isWeb) { |
| 1886 | // hook before webdriverio supports native # locators |
| 1887 | return parseLocator.call(this, { id: locator.slice(1) }) |
| 1888 | } |
| 1889 | |
| 1890 | if (this.platform === 'android' && !this.isWeb) { |
| 1891 | const isNativeLocator = /^\-?android=?/.exec(locator) |
| 1892 | return isNativeLocator ? locator : `android=new UiSelector().text("${locator}")` |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | locator = new Locator(locator, 'xpath') |
| 1897 | if (locator.type === 'css' && !this.isWeb) throw new Error('Unable to use css locators in apps. Locator strategies for this request: xpath, id, class name or accessibility id') |
| 1898 | if (locator.type === 'name' && !this.isWeb) throw new Error("Can't locate element by name in Native context. Use either ID, class name or accessibility id") |
| 1899 | if (locator.type === 'id' && !this.isWeb && this.platform === 'android') return `//*[@resource-id='${locator.value}']` |
| 1900 | return locator.simplify() |
| 1901 | } |
| 1902 | |
| 1903 | // in the end of a file |
| 1904 | function onlyForApps(expectedPlatform) { |