Guest User

Untitled

a guest
Jul 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. /**
  2. * This a method to get text from a native iOS/Android app component.
  3. * It can be used when you are using webdriver.io
  4. *
  5. * In the app I need to automate I found out that:
  6. * - iOS will have the complete text (of all the childeren) in the parent component.
  7. * - on Android a component can have mutiple childrens, that's why you get an array of text back
  8. *
  9. * Feel free to use it for all kinds of purposes, a star is much appreciated ;-)
  10. *
  11. * Grtz,
  12. * Wim | wswebreation
  13. */
  14.  
  15. /**
  16. * Get the text of a native Android / iOS element
  17. *
  18. * @param {element} element
  19. *
  20. * @return {string}
  21. */
  22. export function getTextOfElement(element) {
  23. let visualText;
  24. try {
  25. visualText = element.getText(browser.isAndroid ? ANDROID_SELECTORS.TEXT : IOS_SELECTORS.GENERIC_TEXT);
  26. } catch (e) {
  27. visualText = element.getText();
  28. }
  29.  
  30. if (typeof visualText === 'string') {
  31. return visualText;
  32. }
  33.  
  34. return Array.isArray(visualText) ? visualText.join(' ') : '';
  35. }
  36.  
  37. /**
  38. * Cross-platform selectors
  39. */
  40. const ANDROID_SELECTORS = {
  41. TEXT: '*//android.widget.TextView',
  42. };
  43. const IOS_SELECTORS = {
  44. GENERIC_TEXT: null,
  45. };
Add Comment
Please, Sign In to add comment