Xcodes UI Testing - Tapping an x y location - ios

I am trying to tap an x, y location, while using Xcode and UI Testing. Here is my code:
XCUIApplication().coordinateWithNormalizedOffset(CGVector(dx: 50, dy: 50)).tap()
The middle of the button is definitely at that location and cannot be identified as an individual object (hence using the x, y coordinates).
However, this is not pressing the button. Anyone know if there is a correct way of tapping an x y coordinate?

The dx and dy are not pixel offsets but normalized vectors. For example, 0.5 is actually the middle of the element. Your code is trying to tap an element outside of the screens bounds (by ~50x!).
I suggest trying to solve the problem with a different approach. First off, why can't the button be identified? What have you tried? Are you using a custom UIButton subclass?
I ask because you can (usually) expose any control or UI element to UI testing with the right accessibility attributes. For example, you can set accessibilityLabel and accessibilityIdentifier to the button's text. Then you can use that value to access the button under test.
// Production Code
let button = UIButton()
button.setTitle("Save", forState: .Normal)
button.accessibilityIdentifier = "Save"
// UI Test Code
```
let app = XCUIApplication()
app.buttons["Save"].tap()

You can use dx: 1.5 to click 50% to the right of an element. I used this on an app that has a a webView that I don't control.

Related

How to deselect text in a `ICoreWebView2` control?

Assume that we have 2 ICoreWebView2 controls in a panel, and they are X and Y. When there is any text selected in X, if we then select text in Y, we can observe that the text we selected in X is still selected, even though X has lost keyboard focus, which is now in Y.
How do we "deselect all" in X, when X loses keyboard focus?
By the way, this (deselecting all in X) is the default behavior on WebKit2, but ICoreWebView2 has different behavior. To make our application cross-platform, we need them to have the same behavior so that other codes won't be confused by multiple selected texts. Thanks.
Problem solved! Just call this JavaScript function on X, whenever Y gets the keyboard focus.

xcuitest- Trying to tap on a link within a linklabel

I am trying to tap on a link within a link label. Using the recorder I have this code for the tap event
let link = XCUIApplication().scrollViews.otherElements.links["link here"]
link.tap() does not tap on the link correctly. The link is at the end of the text, how would I be able to tap that part of the label? It seems like XCUITest taps the start of the text which doesnt have the link
Seems like it may be an accessibility container issue. However one work around is to compute the coordinate relative to the UI element you can access with XCUIApplication.
let coordinate = CGVector(dx: 0.1, dy: 0.1) //Enter your values for x / y
someElement.coordinate(withNormalizedOffset: coordinate).tap()
I usually just use trial and error to compute the coordinates. However, you can use the accessibility inspector and debug prints if you need an extra precise press.

Scroll bar in LibreOffice dialog

I am trying to make an image picker component in LibreOffice.
I have a dialog that is dynamically filled with images. When the user clicks on one images, it should be selected and the dialog should be closed.
The problem is that the number of images is variable. So I need to enable scrolling in the dialog (so that the user can navigate through all images).
There seems to be some properties on the dialog object (Scrollbars, Scroll width, Scroll height, etc)
However, I cannot find a way to use them anywhere.
Any ideas?
The scrollbar is one of the Controls available through the dialog box editor. That is the easier way to put a ScrollBar on a dialog box. Just insert it like any other control. There is a harder way via DialogModel.addControl but that seems non-essential to answering this question.
If you add a scrollbar to the dialog box and run the dialog box, you will find it does nothing by default. The functionality (apparently) must be written into a macro. The appropriate triggering event is the While Adjusting event on the ScrollBar object, although it does not trigger the macro simply with the "Test Mode" function in the dialog editor. Running the dialog box through a macro triggers the While Adjusting event when the scroll arrows are triggered, when the slider area is clicked to move the slider, and when the slider itself is dragged. The Object variable returned by the scrollbar event contains a property .Value which is an absolute value between 0 and the EventObject.Model.ScrollValueMax, which allows you to manipulate the other objects on the page manually based on the position of the slider.
Yes, that's right, manipulate objects manually. The sole example I found, from the LibreOffice 4.5 SDK, does precisely this. Of course, it is not as bad as it sounds, because one can iterate through all of the objects on the page by reading the array Dialog.getControls(). In any event, the secret sauce of the example provided in the SDK is to define Static variables to save the initial positions of all of the objects you manipulate with the scrollbar and then simply index those initial positions based on a ratio derived from the scrollbar Value divided by the ScrollValueMax.
Here is a very simple working example of how to scroll. This requires a saved Dialog1 in the Standard library of your document, which contains an object ScrollBar1 (a vertical scrollbar) and Label1 anywhere in the dialog. The ScrollBar1 must be configured to execute the macro ScrBar subroutine (below) on the While Adjusting event. Open the dialog by executing the OpenDialog macro and the scrollbar will move the Label1 control up and down in proportion to the page.
Sub OpenDialog
DialogLibraries.LoadLibrary("Standard")
oVariable = DialogLibraries.Standard.Dialog1
oDialog1 = CreateUnoDialog( oVariable )
oDialog1.Execute()
End Sub
Sub ScrBar (oEventObj As Object)
Static bInit As Boolean
Static PositionLbl1Y0 As Long
oSrc = oEventObj.Source
oSrcModel = oSrc.Model
scrollRatio = oEventObj.Value / oSrcModel.ScrollValueMax
oContx = oSrc.Context
oContxModl = oContx.Model
oLbl1 = oContx.getControl("Label1")
oLbl1Model = oLbl1.Model
REM on initialization remember the position of the label
If bInit = False Then
bInit = True
PositionLbl1Y0 = oLbl1Model.PositionY
End If
oLbl1Model.PositionY = PositionLbl1Y0 - (scrollRatio * oContx.Size.Height)
End Sub
The example provided by the SDK does not run on my setup, but the principles are sound.
There appears to be a second improvised method closer to the functionality one might expect. This method uses the DialogModel.scrollTop property. The property appears to iterate the entire box up or down as a scroll based on the user input. There are two problems using this methodology, however. First, unless you put the scrollbar somewhere else, the scroll bar will scroll away along with the rest of the page. You will need to adjust the location of the scrollbar precisely to compensate for/negate the scrolling of the entire page. In the example below I tried but did not perfect this. Second, the property seems to miss inputs with frequency and easily goes out of alignment/ enters a maladjusted state. Perhaps you can overcome these limitations. Here is the example, relying on the same setup described above.
Sub ScrBar (oEventObj As Object)
Static scrollPos
oSrc = oEventObj.Source
oSrcModel = oSrc.Model
scrollRatio = oEventObj.Value / oSrcModel.ScrollValueMax
If IsEmpty(scrollPos) = False Then
scrollDiff = oEventObj.Value - scrollPos
Else
scrollDiff = oEventObj.Value
End If
scrollPos = oEventObj.Value
oContx = oSrc.Context
oContxModl = oContx.Model
oContxModl.scrollTop = scrollDiff * -1
oSrcModel.PositionY=(scrollRatio * oContx.Size.Height/5) * -1
End Sub
This (sort of) will scroll the contents of the entire dialog box, within limits and with the caveats noted above.

How can I set up a DialogBox in Swift?

I would like to have a box appear in the screen (some kind of textfield) and want to make it look like the static character is talking to the user (like the link below). The text needs to be slowly shown to the user (not all at once). I'm not sure the best way to implement this. Also, I'm not sure how to wrap the text within the box.
http://lightsendgame.com/images/screenshots/CrystalsRoom.JPG
You can create a UIView with a UILabel inside and set the alpha to 0. Whenever you want the box to appear (on button press for example) you can execute a UIAnimation block to set the alpha to 1. This will give the fade in effect you want.
How you display the text is up to you, use the styling in xCode for this, most of us aren’t the best of designers ;)
About the text which slowly comes in to view, you can iterate through a string and keep on adding the next letting in the string to another string. After you’ve added one letter to the second string, output this second string to the user. This will go pretty quick though, if you want to slow this down you can make the thread sleep for an x amount between the iterations.
import UIKit
import XCPlayground
var text:String = “Slowly old-school displaying text is awesome!"
var scrollingText:String = “"
var label:UILabel = UILabel(frame: CGRectMake(0, 0, 400, 100))
for char in text{
scrollingText = "\(scrollingText)\(char)"
label.text = scrollingText
}

Check out whether or not Focus Rect is required (Delphi)

Imagine that we have a form with two buttons on it. I run the application and I click on the first button. nothing happens and no focus rectangle is displayed. But when I press a key, it shows a focus rect on the button and even if I click on the second one, it moves the focus rect to it. So it doesn't display the focus rect unless I press a key. I'm creating my own component and I need to know whether or not I should display the focus rect to draw it.
How do I know it?
I think it's not meant to display the focus rectangle by default, that's until a keyboard accelerator is used. Read UI State on MSDN, that suggests WM_QUERYUISTATE should be used to determine if keyboard accelerators or focus indicators should be drawn or not.

Resources