How can I determine what part of text in a scroll view is visible on screen from an Xcode UI test?












1















I'm new to the Xcode User Interface testing framework. I can successfully manipulate the screen elements, but cannot work out how to produce a meaningful assertion about what text is visible in a scrolling view.



The test I would like to write would go as follows: launch the app, type lots of text into a text view (enough that the first line scrolls out of view), assert that the first line of text is not visible, scroll the view back up to the top, then assert that the first line is now visible. Note that the purpose of this test is to ensure my app has wired things up correctly, not to test Apple's code.



XCUIApplication allows me to type into my NSTextView instance, and also allows me to scroll the associated NSScrollView. But how do I assert whether the first line of text is currently visible? The value attribute on XCUIElement provides the entire text content of the view, whether or not it is currently displayed.



The accessibilityRange(forLine:) and accessibilityString(for:) methods on NSTextView would be ideal, but I can't see how to access them as the UI test only has access to an XCUIElement, not the underlying NSTextView.



Have I missed something, or is there a better way to approach this?










share|improve this question





























    1















    I'm new to the Xcode User Interface testing framework. I can successfully manipulate the screen elements, but cannot work out how to produce a meaningful assertion about what text is visible in a scrolling view.



    The test I would like to write would go as follows: launch the app, type lots of text into a text view (enough that the first line scrolls out of view), assert that the first line of text is not visible, scroll the view back up to the top, then assert that the first line is now visible. Note that the purpose of this test is to ensure my app has wired things up correctly, not to test Apple's code.



    XCUIApplication allows me to type into my NSTextView instance, and also allows me to scroll the associated NSScrollView. But how do I assert whether the first line of text is currently visible? The value attribute on XCUIElement provides the entire text content of the view, whether or not it is currently displayed.



    The accessibilityRange(forLine:) and accessibilityString(for:) methods on NSTextView would be ideal, but I can't see how to access them as the UI test only has access to an XCUIElement, not the underlying NSTextView.



    Have I missed something, or is there a better way to approach this?










    share|improve this question



























      1












      1








      1








      I'm new to the Xcode User Interface testing framework. I can successfully manipulate the screen elements, but cannot work out how to produce a meaningful assertion about what text is visible in a scrolling view.



      The test I would like to write would go as follows: launch the app, type lots of text into a text view (enough that the first line scrolls out of view), assert that the first line of text is not visible, scroll the view back up to the top, then assert that the first line is now visible. Note that the purpose of this test is to ensure my app has wired things up correctly, not to test Apple's code.



      XCUIApplication allows me to type into my NSTextView instance, and also allows me to scroll the associated NSScrollView. But how do I assert whether the first line of text is currently visible? The value attribute on XCUIElement provides the entire text content of the view, whether or not it is currently displayed.



      The accessibilityRange(forLine:) and accessibilityString(for:) methods on NSTextView would be ideal, but I can't see how to access them as the UI test only has access to an XCUIElement, not the underlying NSTextView.



      Have I missed something, or is there a better way to approach this?










      share|improve this question
















      I'm new to the Xcode User Interface testing framework. I can successfully manipulate the screen elements, but cannot work out how to produce a meaningful assertion about what text is visible in a scrolling view.



      The test I would like to write would go as follows: launch the app, type lots of text into a text view (enough that the first line scrolls out of view), assert that the first line of text is not visible, scroll the view back up to the top, then assert that the first line is now visible. Note that the purpose of this test is to ensure my app has wired things up correctly, not to test Apple's code.



      XCUIApplication allows me to type into my NSTextView instance, and also allows me to scroll the associated NSScrollView. But how do I assert whether the first line of text is currently visible? The value attribute on XCUIElement provides the entire text content of the view, whether or not it is currently displayed.



      The accessibilityRange(forLine:) and accessibilityString(for:) methods on NSTextView would be ideal, but I can't see how to access them as the UI test only has access to an XCUIElement, not the underlying NSTextView.



      Have I missed something, or is there a better way to approach this?







      macos cocoa xcode-ui-testing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 11:12







      Tristram

















      asked Nov 19 '18 at 18:49









      TristramTristram

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          1














          If you set the accessibility identifier in the storyboard or in code for the text view you can get the text view via (assuming you gave it the id "textview1" and the window it's in has the default accessibility identifier of "Window"):



          let textview1TextView = app.windows["Window"].textViews["textview1"]


          but that won't actually get you what you need.



          Instead, set the accessibility identifier of the scrollview and get that:



          let scrollview = app.windows["Window"].scrollViews["scrollview1"]


          Then use that to get the scrollbars (you should only have one in this case; you can use scrollbars.count to check.



          let scrollbars = scrollview.scrollBars
          print("scrollbars count: (scrollbars.count)")


          Then you can use the value attribute of the scrollbar to get it's value:
          (you're converting a XCUIElemenTypeQueryProvider into an XCUIElement so you can get it's value):



          let val = scrollbars.element.value


          it will be 0 at the top and a floating point value when scrolled (one line of text in my test code showed a value of {{0.02409638554216868}}.



          Documentation that will help you explore further:



          XCUIElementTypeQueryProvider



          XCUIElementAttributes



          Note that you can put a breakpoint in the middle of your test, run it and then use the debugger console to examine things:



          (lldb) po scrollbars.element.value
          t = 749.66s Find the ScrollBar ▿ Optional<Any>
          - some : 0

          (lldb) po scrollbars.element.value
          t = 758.17s Find the ScrollBar ▿ Optional<Any>
          - some : 0.05421686746987952


          and while in the debugger you can even interact with your app's window to scroll it manually (which is what I did between typing in those two po calls), or perhaps add text and so on.





          OK OP now noted that they're interested in the specific text showing or not rather than the first line in view or not (which is what I previously answered above).



          Here's a bit of a hack, but I think it'll work:



          Use XCUICoordinate's click(forDuration:, thenDragTo:) method to select the first line of text (use the view frame to calculate coordinates) and then use the typeKey( modifierFlags:) to invoke the edit menu "copy" command. Then use NSPasteboard methods to get the pasteboard contents and check the text.



          Here's a quick test I did to validate the approach (selecting the first line of text using XCUICoordinate as noted above is left as an exercise for the reader):



          NSPasteboard.general.clearContents()

          // stopped at break point on next line and I manually selected the text of the first line of text in my test app and then hit continue in the debugger

          textview1TextView.typeKey("c", modifierFlags:.command)
          print( NSPasteboard.general.pasteboardItems?.first?.string(forType: NSPasteboard.PasteboardType.string) ?? "none" );


          -> "the text of the first line" was printed to the console.



          Note that you can scroll the selection off screen so you have to not scroll after doing the select or you won't be getting the answer you want.






          share|improve this answer


























          • Thank you for the debugging tips. I'm afraid my question must have been unclear. I can scroll the view without a problem. What I want to do — but don't know how — is to determine what text is currently visible on screen.

            – Tristram
            Nov 20 '18 at 11:01











          • Yes, scrolling is easy. Question asks how to write a test to test if first line is scrolled out of view or scrolled into view. Above shows how to do this if your test enters the same text every time. First line is visible when scrollbars.element.value < 0.0001 (effectively 0). Set the breakpoint like I suggest and while stopped in the breakpoint, after getting scrollbars, scroll manually until first line of text is not visible and then check the value of the scrollbar using po. Write a test to assert that the scrollbar value being > then that value and you have the not visible test.

            – Dad
            Nov 20 '18 at 16:40











          • The Note at the end is me doing exactly this. visible and scrolled to the top scrollbars.element.value is 0. Scrolled manually to make the first line not visible and then did the second po command and the value was now 0.54215867… So you assert value is greater than 0.07 say and if true the first line is scrolled out of the view. This assumes your view is the same width and the font is the same every time you run the test, of course.

            – Dad
            Nov 20 '18 at 16:42











          • I'm using scrollview.scroll(byDeltaX: 0.0, deltaY: 50.0) in my test to scroll programmatically in my test fwiw.

            – Dad
            Nov 20 '18 at 16:45











          • @Tristram added an approach to try to the bottom of my answer in case you really want to get the text of the first visible line. But I think my comments above are much easier and seem to allow you to write the test you said you wanted to write in your question… ¯_(ツ)_/¯ up to you! :)

            – Dad
            Nov 20 '18 at 17:46











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380916%2fhow-can-i-determine-what-part-of-text-in-a-scroll-view-is-visible-on-screen-from%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          If you set the accessibility identifier in the storyboard or in code for the text view you can get the text view via (assuming you gave it the id "textview1" and the window it's in has the default accessibility identifier of "Window"):



          let textview1TextView = app.windows["Window"].textViews["textview1"]


          but that won't actually get you what you need.



          Instead, set the accessibility identifier of the scrollview and get that:



          let scrollview = app.windows["Window"].scrollViews["scrollview1"]


          Then use that to get the scrollbars (you should only have one in this case; you can use scrollbars.count to check.



          let scrollbars = scrollview.scrollBars
          print("scrollbars count: (scrollbars.count)")


          Then you can use the value attribute of the scrollbar to get it's value:
          (you're converting a XCUIElemenTypeQueryProvider into an XCUIElement so you can get it's value):



          let val = scrollbars.element.value


          it will be 0 at the top and a floating point value when scrolled (one line of text in my test code showed a value of {{0.02409638554216868}}.



          Documentation that will help you explore further:



          XCUIElementTypeQueryProvider



          XCUIElementAttributes



          Note that you can put a breakpoint in the middle of your test, run it and then use the debugger console to examine things:



          (lldb) po scrollbars.element.value
          t = 749.66s Find the ScrollBar ▿ Optional<Any>
          - some : 0

          (lldb) po scrollbars.element.value
          t = 758.17s Find the ScrollBar ▿ Optional<Any>
          - some : 0.05421686746987952


          and while in the debugger you can even interact with your app's window to scroll it manually (which is what I did between typing in those two po calls), or perhaps add text and so on.





          OK OP now noted that they're interested in the specific text showing or not rather than the first line in view or not (which is what I previously answered above).



          Here's a bit of a hack, but I think it'll work:



          Use XCUICoordinate's click(forDuration:, thenDragTo:) method to select the first line of text (use the view frame to calculate coordinates) and then use the typeKey( modifierFlags:) to invoke the edit menu "copy" command. Then use NSPasteboard methods to get the pasteboard contents and check the text.



          Here's a quick test I did to validate the approach (selecting the first line of text using XCUICoordinate as noted above is left as an exercise for the reader):



          NSPasteboard.general.clearContents()

          // stopped at break point on next line and I manually selected the text of the first line of text in my test app and then hit continue in the debugger

          textview1TextView.typeKey("c", modifierFlags:.command)
          print( NSPasteboard.general.pasteboardItems?.first?.string(forType: NSPasteboard.PasteboardType.string) ?? "none" );


          -> "the text of the first line" was printed to the console.



          Note that you can scroll the selection off screen so you have to not scroll after doing the select or you won't be getting the answer you want.






          share|improve this answer


























          • Thank you for the debugging tips. I'm afraid my question must have been unclear. I can scroll the view without a problem. What I want to do — but don't know how — is to determine what text is currently visible on screen.

            – Tristram
            Nov 20 '18 at 11:01











          • Yes, scrolling is easy. Question asks how to write a test to test if first line is scrolled out of view or scrolled into view. Above shows how to do this if your test enters the same text every time. First line is visible when scrollbars.element.value < 0.0001 (effectively 0). Set the breakpoint like I suggest and while stopped in the breakpoint, after getting scrollbars, scroll manually until first line of text is not visible and then check the value of the scrollbar using po. Write a test to assert that the scrollbar value being > then that value and you have the not visible test.

            – Dad
            Nov 20 '18 at 16:40











          • The Note at the end is me doing exactly this. visible and scrolled to the top scrollbars.element.value is 0. Scrolled manually to make the first line not visible and then did the second po command and the value was now 0.54215867… So you assert value is greater than 0.07 say and if true the first line is scrolled out of the view. This assumes your view is the same width and the font is the same every time you run the test, of course.

            – Dad
            Nov 20 '18 at 16:42











          • I'm using scrollview.scroll(byDeltaX: 0.0, deltaY: 50.0) in my test to scroll programmatically in my test fwiw.

            – Dad
            Nov 20 '18 at 16:45











          • @Tristram added an approach to try to the bottom of my answer in case you really want to get the text of the first visible line. But I think my comments above are much easier and seem to allow you to write the test you said you wanted to write in your question… ¯_(ツ)_/¯ up to you! :)

            – Dad
            Nov 20 '18 at 17:46
















          1














          If you set the accessibility identifier in the storyboard or in code for the text view you can get the text view via (assuming you gave it the id "textview1" and the window it's in has the default accessibility identifier of "Window"):



          let textview1TextView = app.windows["Window"].textViews["textview1"]


          but that won't actually get you what you need.



          Instead, set the accessibility identifier of the scrollview and get that:



          let scrollview = app.windows["Window"].scrollViews["scrollview1"]


          Then use that to get the scrollbars (you should only have one in this case; you can use scrollbars.count to check.



          let scrollbars = scrollview.scrollBars
          print("scrollbars count: (scrollbars.count)")


          Then you can use the value attribute of the scrollbar to get it's value:
          (you're converting a XCUIElemenTypeQueryProvider into an XCUIElement so you can get it's value):



          let val = scrollbars.element.value


          it will be 0 at the top and a floating point value when scrolled (one line of text in my test code showed a value of {{0.02409638554216868}}.



          Documentation that will help you explore further:



          XCUIElementTypeQueryProvider



          XCUIElementAttributes



          Note that you can put a breakpoint in the middle of your test, run it and then use the debugger console to examine things:



          (lldb) po scrollbars.element.value
          t = 749.66s Find the ScrollBar ▿ Optional<Any>
          - some : 0

          (lldb) po scrollbars.element.value
          t = 758.17s Find the ScrollBar ▿ Optional<Any>
          - some : 0.05421686746987952


          and while in the debugger you can even interact with your app's window to scroll it manually (which is what I did between typing in those two po calls), or perhaps add text and so on.





          OK OP now noted that they're interested in the specific text showing or not rather than the first line in view or not (which is what I previously answered above).



          Here's a bit of a hack, but I think it'll work:



          Use XCUICoordinate's click(forDuration:, thenDragTo:) method to select the first line of text (use the view frame to calculate coordinates) and then use the typeKey( modifierFlags:) to invoke the edit menu "copy" command. Then use NSPasteboard methods to get the pasteboard contents and check the text.



          Here's a quick test I did to validate the approach (selecting the first line of text using XCUICoordinate as noted above is left as an exercise for the reader):



          NSPasteboard.general.clearContents()

          // stopped at break point on next line and I manually selected the text of the first line of text in my test app and then hit continue in the debugger

          textview1TextView.typeKey("c", modifierFlags:.command)
          print( NSPasteboard.general.pasteboardItems?.first?.string(forType: NSPasteboard.PasteboardType.string) ?? "none" );


          -> "the text of the first line" was printed to the console.



          Note that you can scroll the selection off screen so you have to not scroll after doing the select or you won't be getting the answer you want.






          share|improve this answer


























          • Thank you for the debugging tips. I'm afraid my question must have been unclear. I can scroll the view without a problem. What I want to do — but don't know how — is to determine what text is currently visible on screen.

            – Tristram
            Nov 20 '18 at 11:01











          • Yes, scrolling is easy. Question asks how to write a test to test if first line is scrolled out of view or scrolled into view. Above shows how to do this if your test enters the same text every time. First line is visible when scrollbars.element.value < 0.0001 (effectively 0). Set the breakpoint like I suggest and while stopped in the breakpoint, after getting scrollbars, scroll manually until first line of text is not visible and then check the value of the scrollbar using po. Write a test to assert that the scrollbar value being > then that value and you have the not visible test.

            – Dad
            Nov 20 '18 at 16:40











          • The Note at the end is me doing exactly this. visible and scrolled to the top scrollbars.element.value is 0. Scrolled manually to make the first line not visible and then did the second po command and the value was now 0.54215867… So you assert value is greater than 0.07 say and if true the first line is scrolled out of the view. This assumes your view is the same width and the font is the same every time you run the test, of course.

            – Dad
            Nov 20 '18 at 16:42











          • I'm using scrollview.scroll(byDeltaX: 0.0, deltaY: 50.0) in my test to scroll programmatically in my test fwiw.

            – Dad
            Nov 20 '18 at 16:45











          • @Tristram added an approach to try to the bottom of my answer in case you really want to get the text of the first visible line. But I think my comments above are much easier and seem to allow you to write the test you said you wanted to write in your question… ¯_(ツ)_/¯ up to you! :)

            – Dad
            Nov 20 '18 at 17:46














          1












          1








          1







          If you set the accessibility identifier in the storyboard or in code for the text view you can get the text view via (assuming you gave it the id "textview1" and the window it's in has the default accessibility identifier of "Window"):



          let textview1TextView = app.windows["Window"].textViews["textview1"]


          but that won't actually get you what you need.



          Instead, set the accessibility identifier of the scrollview and get that:



          let scrollview = app.windows["Window"].scrollViews["scrollview1"]


          Then use that to get the scrollbars (you should only have one in this case; you can use scrollbars.count to check.



          let scrollbars = scrollview.scrollBars
          print("scrollbars count: (scrollbars.count)")


          Then you can use the value attribute of the scrollbar to get it's value:
          (you're converting a XCUIElemenTypeQueryProvider into an XCUIElement so you can get it's value):



          let val = scrollbars.element.value


          it will be 0 at the top and a floating point value when scrolled (one line of text in my test code showed a value of {{0.02409638554216868}}.



          Documentation that will help you explore further:



          XCUIElementTypeQueryProvider



          XCUIElementAttributes



          Note that you can put a breakpoint in the middle of your test, run it and then use the debugger console to examine things:



          (lldb) po scrollbars.element.value
          t = 749.66s Find the ScrollBar ▿ Optional<Any>
          - some : 0

          (lldb) po scrollbars.element.value
          t = 758.17s Find the ScrollBar ▿ Optional<Any>
          - some : 0.05421686746987952


          and while in the debugger you can even interact with your app's window to scroll it manually (which is what I did between typing in those two po calls), or perhaps add text and so on.





          OK OP now noted that they're interested in the specific text showing or not rather than the first line in view or not (which is what I previously answered above).



          Here's a bit of a hack, but I think it'll work:



          Use XCUICoordinate's click(forDuration:, thenDragTo:) method to select the first line of text (use the view frame to calculate coordinates) and then use the typeKey( modifierFlags:) to invoke the edit menu "copy" command. Then use NSPasteboard methods to get the pasteboard contents and check the text.



          Here's a quick test I did to validate the approach (selecting the first line of text using XCUICoordinate as noted above is left as an exercise for the reader):



          NSPasteboard.general.clearContents()

          // stopped at break point on next line and I manually selected the text of the first line of text in my test app and then hit continue in the debugger

          textview1TextView.typeKey("c", modifierFlags:.command)
          print( NSPasteboard.general.pasteboardItems?.first?.string(forType: NSPasteboard.PasteboardType.string) ?? "none" );


          -> "the text of the first line" was printed to the console.



          Note that you can scroll the selection off screen so you have to not scroll after doing the select or you won't be getting the answer you want.






          share|improve this answer















          If you set the accessibility identifier in the storyboard or in code for the text view you can get the text view via (assuming you gave it the id "textview1" and the window it's in has the default accessibility identifier of "Window"):



          let textview1TextView = app.windows["Window"].textViews["textview1"]


          but that won't actually get you what you need.



          Instead, set the accessibility identifier of the scrollview and get that:



          let scrollview = app.windows["Window"].scrollViews["scrollview1"]


          Then use that to get the scrollbars (you should only have one in this case; you can use scrollbars.count to check.



          let scrollbars = scrollview.scrollBars
          print("scrollbars count: (scrollbars.count)")


          Then you can use the value attribute of the scrollbar to get it's value:
          (you're converting a XCUIElemenTypeQueryProvider into an XCUIElement so you can get it's value):



          let val = scrollbars.element.value


          it will be 0 at the top and a floating point value when scrolled (one line of text in my test code showed a value of {{0.02409638554216868}}.



          Documentation that will help you explore further:



          XCUIElementTypeQueryProvider



          XCUIElementAttributes



          Note that you can put a breakpoint in the middle of your test, run it and then use the debugger console to examine things:



          (lldb) po scrollbars.element.value
          t = 749.66s Find the ScrollBar ▿ Optional<Any>
          - some : 0

          (lldb) po scrollbars.element.value
          t = 758.17s Find the ScrollBar ▿ Optional<Any>
          - some : 0.05421686746987952


          and while in the debugger you can even interact with your app's window to scroll it manually (which is what I did between typing in those two po calls), or perhaps add text and so on.





          OK OP now noted that they're interested in the specific text showing or not rather than the first line in view or not (which is what I previously answered above).



          Here's a bit of a hack, but I think it'll work:



          Use XCUICoordinate's click(forDuration:, thenDragTo:) method to select the first line of text (use the view frame to calculate coordinates) and then use the typeKey( modifierFlags:) to invoke the edit menu "copy" command. Then use NSPasteboard methods to get the pasteboard contents and check the text.



          Here's a quick test I did to validate the approach (selecting the first line of text using XCUICoordinate as noted above is left as an exercise for the reader):



          NSPasteboard.general.clearContents()

          // stopped at break point on next line and I manually selected the text of the first line of text in my test app and then hit continue in the debugger

          textview1TextView.typeKey("c", modifierFlags:.command)
          print( NSPasteboard.general.pasteboardItems?.first?.string(forType: NSPasteboard.PasteboardType.string) ?? "none" );


          -> "the text of the first line" was printed to the console.



          Note that you can scroll the selection off screen so you have to not scroll after doing the select or you won't be getting the answer you want.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 17:44

























          answered Nov 20 '18 at 2:33









          DadDad

          4,44812028




          4,44812028













          • Thank you for the debugging tips. I'm afraid my question must have been unclear. I can scroll the view without a problem. What I want to do — but don't know how — is to determine what text is currently visible on screen.

            – Tristram
            Nov 20 '18 at 11:01











          • Yes, scrolling is easy. Question asks how to write a test to test if first line is scrolled out of view or scrolled into view. Above shows how to do this if your test enters the same text every time. First line is visible when scrollbars.element.value < 0.0001 (effectively 0). Set the breakpoint like I suggest and while stopped in the breakpoint, after getting scrollbars, scroll manually until first line of text is not visible and then check the value of the scrollbar using po. Write a test to assert that the scrollbar value being > then that value and you have the not visible test.

            – Dad
            Nov 20 '18 at 16:40











          • The Note at the end is me doing exactly this. visible and scrolled to the top scrollbars.element.value is 0. Scrolled manually to make the first line not visible and then did the second po command and the value was now 0.54215867… So you assert value is greater than 0.07 say and if true the first line is scrolled out of the view. This assumes your view is the same width and the font is the same every time you run the test, of course.

            – Dad
            Nov 20 '18 at 16:42











          • I'm using scrollview.scroll(byDeltaX: 0.0, deltaY: 50.0) in my test to scroll programmatically in my test fwiw.

            – Dad
            Nov 20 '18 at 16:45











          • @Tristram added an approach to try to the bottom of my answer in case you really want to get the text of the first visible line. But I think my comments above are much easier and seem to allow you to write the test you said you wanted to write in your question… ¯_(ツ)_/¯ up to you! :)

            – Dad
            Nov 20 '18 at 17:46



















          • Thank you for the debugging tips. I'm afraid my question must have been unclear. I can scroll the view without a problem. What I want to do — but don't know how — is to determine what text is currently visible on screen.

            – Tristram
            Nov 20 '18 at 11:01











          • Yes, scrolling is easy. Question asks how to write a test to test if first line is scrolled out of view or scrolled into view. Above shows how to do this if your test enters the same text every time. First line is visible when scrollbars.element.value < 0.0001 (effectively 0). Set the breakpoint like I suggest and while stopped in the breakpoint, after getting scrollbars, scroll manually until first line of text is not visible and then check the value of the scrollbar using po. Write a test to assert that the scrollbar value being > then that value and you have the not visible test.

            – Dad
            Nov 20 '18 at 16:40











          • The Note at the end is me doing exactly this. visible and scrolled to the top scrollbars.element.value is 0. Scrolled manually to make the first line not visible and then did the second po command and the value was now 0.54215867… So you assert value is greater than 0.07 say and if true the first line is scrolled out of the view. This assumes your view is the same width and the font is the same every time you run the test, of course.

            – Dad
            Nov 20 '18 at 16:42











          • I'm using scrollview.scroll(byDeltaX: 0.0, deltaY: 50.0) in my test to scroll programmatically in my test fwiw.

            – Dad
            Nov 20 '18 at 16:45











          • @Tristram added an approach to try to the bottom of my answer in case you really want to get the text of the first visible line. But I think my comments above are much easier and seem to allow you to write the test you said you wanted to write in your question… ¯_(ツ)_/¯ up to you! :)

            – Dad
            Nov 20 '18 at 17:46

















          Thank you for the debugging tips. I'm afraid my question must have been unclear. I can scroll the view without a problem. What I want to do — but don't know how — is to determine what text is currently visible on screen.

          – Tristram
          Nov 20 '18 at 11:01





          Thank you for the debugging tips. I'm afraid my question must have been unclear. I can scroll the view without a problem. What I want to do — but don't know how — is to determine what text is currently visible on screen.

          – Tristram
          Nov 20 '18 at 11:01













          Yes, scrolling is easy. Question asks how to write a test to test if first line is scrolled out of view or scrolled into view. Above shows how to do this if your test enters the same text every time. First line is visible when scrollbars.element.value < 0.0001 (effectively 0). Set the breakpoint like I suggest and while stopped in the breakpoint, after getting scrollbars, scroll manually until first line of text is not visible and then check the value of the scrollbar using po. Write a test to assert that the scrollbar value being > then that value and you have the not visible test.

          – Dad
          Nov 20 '18 at 16:40





          Yes, scrolling is easy. Question asks how to write a test to test if first line is scrolled out of view or scrolled into view. Above shows how to do this if your test enters the same text every time. First line is visible when scrollbars.element.value < 0.0001 (effectively 0). Set the breakpoint like I suggest and while stopped in the breakpoint, after getting scrollbars, scroll manually until first line of text is not visible and then check the value of the scrollbar using po. Write a test to assert that the scrollbar value being > then that value and you have the not visible test.

          – Dad
          Nov 20 '18 at 16:40













          The Note at the end is me doing exactly this. visible and scrolled to the top scrollbars.element.value is 0. Scrolled manually to make the first line not visible and then did the second po command and the value was now 0.54215867… So you assert value is greater than 0.07 say and if true the first line is scrolled out of the view. This assumes your view is the same width and the font is the same every time you run the test, of course.

          – Dad
          Nov 20 '18 at 16:42





          The Note at the end is me doing exactly this. visible and scrolled to the top scrollbars.element.value is 0. Scrolled manually to make the first line not visible and then did the second po command and the value was now 0.54215867… So you assert value is greater than 0.07 say and if true the first line is scrolled out of the view. This assumes your view is the same width and the font is the same every time you run the test, of course.

          – Dad
          Nov 20 '18 at 16:42













          I'm using scrollview.scroll(byDeltaX: 0.0, deltaY: 50.0) in my test to scroll programmatically in my test fwiw.

          – Dad
          Nov 20 '18 at 16:45





          I'm using scrollview.scroll(byDeltaX: 0.0, deltaY: 50.0) in my test to scroll programmatically in my test fwiw.

          – Dad
          Nov 20 '18 at 16:45













          @Tristram added an approach to try to the bottom of my answer in case you really want to get the text of the first visible line. But I think my comments above are much easier and seem to allow you to write the test you said you wanted to write in your question… ¯_(ツ)_/¯ up to you! :)

          – Dad
          Nov 20 '18 at 17:46





          @Tristram added an approach to try to the bottom of my answer in case you really want to get the text of the first visible line. But I think my comments above are much easier and seem to allow you to write the test you said you wanted to write in your question… ¯_(ツ)_/¯ up to you! :)

          – Dad
          Nov 20 '18 at 17:46




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380916%2fhow-can-i-determine-what-part-of-text-in-a-scroll-view-is-visible-on-screen-from%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()