4

In Ranorex, I found how to wait for the existence of an element, but I don't find how to wait the element is visible.

enter image description here

I want to do the same thing, but I want to wait until the element is visible. Unfortunately, I just see Exist and Not Exist as possible values for the WaitFor statement. And in my case, the test is unreliable, cause the click is sometimes launched in spite of the existence of the element, but is still not displayed

Do you know how to do this please?

Sae1962
  • 1,122
  • 15
  • 31

3 Answers3

2

You can add this element in your repo path [@visible='true'] and use a WaitForExists:

repo.elementInfo.WaitForExists(SearchTimeOut)
Sae1962
  • 1,122
  • 15
  • 31
David McDuff
  • 235
  • 1
  • 5
  • 14
1

You can use the Visible property to wait for the element:

DateTime start = DateTime.Now;

while (Repository.Element.Visible)
{               
    Delay.Seconds(1);

    if(System.DateTime.Now.Subtract(start).TotalSeconds > MaxWaitTime)
    {                   
        Validate.Fail("");
    }
}
Sae1962
  • 1,122
  • 15
  • 31
Manik Jadhav
  • 387
  • 4
  • 9
0

For Web elements, it is difficult to know whether an item exists, is visible, etc.

I achieved reliability using the DOM page location and width by comparing them to, in my example, a sliding menu position and width, as follows:

    Public Function IsMenuVisible() As Boolean
        Dim menuVisible As Boolean

        'Get page position and width
        Dim pageXPosition As Integer = repo.WebPage.Self.Element.ScreenLocation.X
        Dim pageWidth As Integer = repo.WebPage.Self.Element.ScreenRectangle.Width
        'Get configuration menu position and width
        Dim menuXPosition As Integer = repo.WebPage.Menu.Self.Element.ScreenLocation.X
        Dim menuWidth As Integer = repo.WebPage.Menu.Self.Element.ScreenRectangle.Width

        'If menu top right location is >= web page size (out of screen)
        If menuXPosition + menuWidth > pageXPosition + pageWidth Then
            Report.Info(String.Format("Configuration menu is hidden (menuXPositon = {0}, pageXPosition = {1}, pageWidth = {2}, menuWidth = {3}).", menuXPosition, pageXPosition, pageWidth, menuWidth))
            menuVisible = False
        Else
            Report.Info("Configuration menu is currently visible.")
            menuVisible = True
        End If

        Return menuVisible
    End Function

In my example, the menu is located on the right of the page. Please modify it according to your needs.

Then, do simple user code to loop a number of times to wait for the menu to appear as follows:

        Public Sub WaitForMenuToAppear()
        Dim retries As Integer = WaitForMenuToAppearRetries
        While Not IsMenuVisible() AndAlso retries > 0
            retries -= 1
            Report.Info(String.Format("Waiting for configuration menu to be visible ({0}).", retries))
            Delay.Duration(1000)
        End While

        If Not IsMenuVisible() Then
            Throw New RanorexException(String.Format("Menu did not appear within '{0}' seconds.", WaitForMenuToAppearRetries))
        Else
            Report.Info("Menu is visible.")
        End If
    End Sub

I had to do this since the sliding is always visible. Using the highlight function of Ranorex Spy, the red rectangle is drawn outside the viewing area of the Web page.

I leave to you to complete the example.

Hope this helps…

Sup3rHugh
  • 677
  • 7
  • 15
  • It seems there has been so much people asking for this that Ranorex is finally in the process of integrating it directly, as you can read in this [Ranorex user voice article](http://uservoice.ranorex.com/forums/150109-ideas-to-improve-ranorex/suggestions/17162687-create-function-for-wait-for-visible-and-wait-for) – Sup3rHugh May 03 '17 at 16:00