Can not access the programatically added UIView in XCTest - ios

Below is my recorded XCTest
let app = XCUIApplication()
let tablesQuery = app.tables
tablesQuery.staticTexts["Video"].tap()
tablesQuery.staticTexts["\tWindowed"].tap()
app.buttons["Launch"].tap()
app.buttons["Popout Video"].tap()
app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap()
When I am trying to run the test the last part that is:
app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap()
is not accessible. It does not throw any error but the last line of code is not executed.
I have tried solving the issue by referring to the following stackoverflow question :
Xcode UI Tests can't find views that are added programatically
Also , I have referred to the following Apple Documentations: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW2
https://developer.apple.com/reference/uikit/uiview
But all these doesn't seem to solve the issue. Any help will be greatly appreciated.

The line in question is very brittle. It's possible that your app's views are not always available in the same order every time the app launches, depending on race conditions, so what was recorded in your recording session does not necessarily work for all launches of the app. You'll probably find that the code is actually running, but isn't tapping the element you expected.
To make your code less brittle, add an accessibility identifier to the UIView in your app code, and use the otherElements query to find the UIView.
// app code
let view: UIView!
view.accessibilityIdentifier = "myAccessibilityIdentifier"
// UI test code
app.otherElements["myAccessibilityIdentifier"].tap()

Related

Accessing buttons on a UIActivityViewController in an iOS 13 UI test causes a crash

Is anyone else having problems running Xcode UI tests with Xcode 11 targeting an iOS 13 simulator or device where looking for the buttons on an UIActivityViewController causes a crash?
I have multiple UI tests that verify the buttons that appear in a UIActivityViewController. They do the expected setup work and then look for the button with something like:
XCTAssertTrue(app.buttons["Copy"].exists)
The tests have run fine iOS 10, 11, and 12. If I try to run the same test on an iOS 13 simulator or device, the moment the code attempts to access app.buttons, execution stops and I'll get a Thread 1: signal SIGABRT followed by Failed to get matching snapshots: Lost connection to the application (pid 33047). at the line where I try to access app.buttons.
Adding a wait or even an old-school sleep does nothing. I've tried to dig around some of the other queries hanging off of XCUIElementTypeQueryProvider to find the elements with no luck.
If I debug the test and put a breakpoint before the test accesses app.buttons, and I try to print out what it contains, I get a different error message.
po app.buttons
t = 49.37s Requesting snapshot of accessibility hierarchy for app with pid 37576
expression produced error: error: /var/folders/f2/zhwz28mn1hd815pc78kg02q80000gp/T/expr5-3b2971..swift:1:72: error: 'XCUIElementQuery' is not a member type of 'XCTest'
Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer<XCTest.XCUIElementQuery>(bitPattern: 0x10c73f4d0)!.pointee)
This sure feels like an Xcode bug. Has anyone else run into this?
Here's a bit of code if anyone else wants to try it out.
From a view controller:
#IBAction func showPressed(_ sender: Any) {
let text = "I have something to share."
let vc = UIActivityViewController(activityItems: [text], applicationActivities: nil)
vc.popoverPresentationController?.sourceView = self.view
self.present(vc, animated: true, completion: nil)
}
The UI test:
func testActivityViewController() {
let app = XCUIApplication()
app.launch()
app.buttons["Show AVC"].tap()
let buttons = app.buttons
let copy = buttons["Copy"]
sleep(2) // Just keeping things simple for the example.
XCTAssertTrue(copy.exists)
}
I'm not sure exactly in which version this was fixed, but the UI elements are available in a slightly different configuration as of Xcode version 11.2.1 (11B500). Here's how you might access the Copy button from UIActivityViewController now:
XCUIApplication().otherElements["ActivityListView"].cells.containing(.label(equals: "Copy")).firstMatch
HT to https://stackoverflow.com/a/48450562/19626 for the otherElements selector.
The close/cancel button moved, too. Here's where I found it:
XCUIApplication().otherElements["ActivityListView"].buttons["Close"].tap()
I'm seeing this behavior with Xcode 11 too, and I think your assumption that it's an Xcode bug is correct.
The crash appears to be fixed with Xcode Version 11.2 beta 2 (11B44), but querying for buttons in a UIActivityViewController is still broken. (I never see the element resolve.) Hopefully Apple will fix element lookup in the near future.

Network extension view print statements

I am implementing a NEPacketTunnelProvider and loading it from my view controller using:
var vpnManager: NETunnelProviderManager = NETunnelProviderManager()
...
let providerProtocol = NETunnelProviderProtocol()
providerProtocol.providerBundleIdentifier = "AA.BB.CC"
providerProtocol.serverAddress = "<something>"
...
self.vpnManager.localizedDescription = "My app"
self.vpnManager.protocolConfiguration = providerProtocol
self.vpnManager.isEnabled = true
self.vpnManager.connection.startVPNTunnel()
Parts marked with "..." seemed irrelevant.
My understanding (although it's really not clear in the documentation) is that when I do this, and I have a target that was created as type "NetworkExtension" with BundleId AA.BB.CC, that the extension would be loaded and executed properly. So, my understanding is that startTunnel (from NEPacketTunnelProvider) will implicitly be called from the code block above.
I put a NSLog("STARTING TUNNEL!!!!!") right at the top of the startTunnel method, but am not sure where to see that. So far, I have viewed the logs in:
Console
Window > Devices and simulators > View device logs
None of these appear to show the logs from within the extension. The problem is that the extension seems to crash before I can attach to the running process, so I have the feeling I'm just "missing" that log because I can't attach quickly enough.
Short question
How can I attach to a running network extension quickly enough so that I don't miss an NSLog that is run immediately?
The logs from Network extensions do not go to Xcode console. to see the logs from Network extension you have to follow the bellow steps.
Open the console application.
Select your iOS device running the network extension you will see all the logs from your device.
Filter the logs by Network extension target name now you will see the logs only from your network extension.
How can I attach to a running network extension quickly enough?
What I have been doing to solve this problem was, put the thread on sleep right before log statement.
sleep(60)
It will give you enough time to attach the Network Extension process for debug.

why my print result of from one swift file does not appear in xcode debug area

xcode debug area
hi,
i am a new beginner in iOS development. i am trying to learn someone code to build an app.
here is the screenshot of my problem : https://i.stack.imgur.com/4QX5P.png
i am trying to print("helloo") and print("your requestID is : ") but it doesnt appear in my debug area, that code is written in my ViewController.swift file
the debug area just print out the networking result from VenuesTableViewController.swift file (from another swift file)
what went wrong in here ? thanks in advance :)
Based on your code, it's very strange that your printing "helloo" doesn't appear on your debug area since it is declared on the viewDidLoad of your ViewController. Did you really use this ViewController ?
The second print "Your request id..." can or not be print based on your previous guard statements : guard let dictionnarayJSON...guard let meta...guard let requestID. A guard statement allows you to control the flow of your code. If what you test is correct it can continue to proceed your code. If not you put return so the flow of your code is stopped.
So, if one of your 3 guard statements fails, you can't reach your printing statement print(Your requet id...)
I recommend you to see Apple documentation : Control Flow

UITest in Xcode 7: How to test keyboard layout (keyboardType)

I am currently exploring the new UITest library in Xcode, and I want to test if the keyboard that pops up upon clicking inside a UITextView has the proper type (in this case it should be .PhonePad).
I don't think this is feasible with the default XCUIElement and XCUIElementAttributes (which are still a bit blurry to me concerning their actual meaning), and I don't really understand how and what I am supposed to extend in order to be able to test this.
Any help would be greatly appreciated ! :)
Below code I am using for testing the Phone number and password validation check.
let app = XCUIApplication()
let tablesQuery = app.tables
tablesQuery.cells.containingType(.StaticText, identifier:"Login Using").buttons["Icon mail"].tap()
tablesQuery.textFields["Phone Number"].tap()
tablesQuery.cells.containingType(.SecureTextField, identifier:"Password").childrenMatchingType(.TextField).element.typeText("ooo")
tablesQuery.staticTexts["Login Using"].swipeUp()
tablesQuery.secureTextFields["Password"].tap()
tablesQuery.cells.containingType(.Button, identifier:"Login").childrenMatchingType(.SecureTextField).element.typeText("eeee")
tablesQuery.buttons["Login"].tap()
app.alerts["Error"].collectionViews.buttons["OK"].pressForDuration(1.1);
I hope this will be useful for you.

UIAutomation with Instruments - How to tap copy/paste buttons?

I'm using Instruments for iOS automation and I can't seem to figure out how to tap options on the copy/paste menu. When I do a logElementTree(),I see that we are returning a UIEditingMenu and then three elements (which correspond to options of that menu, such as copy/paste, etc..). I am attempting to place this into a variable, and then trying to "tap" that variable but I cannot get that to work. Here is a sample of my code:
var target = UIATarget.localTarget();
var app = target.frontMostApp();
var window = app.mainWindow();
//This generates the highlighted text
app.dragInsideWithOptions({startOffset:{x:0.45, y:0.6}, endOffset:{x:0.45, y:0.6}, duration:1.5});
var copy = app.editingMenu.elements.withName("copyButton");
copy.tap();
Instruments returns, "0) UIAElementNil". In addition to the above, I've also tried:
app.elements.withName("copyButton")
window.elements.withName("copyButton")
So, I can get the editingMenu to produce the available options, but I cannot figure out a way to tap or select one of those options. I'm not quite sure I know how to reference those options to begin with.
Does anyone have any ideas?
Thanks!
You should try app.editingMenu().elements()[index].tap() where index is the index of the option you want to tap from the array of elements returned. I got my one working this way.
Hey.
First of all, I was always using .elements() not .elements... but it is JS, so it may be invoking function that is assigned to object property..?
Anyway, maybe this edit menu is not internal window of the app, but it is system level menu, that is invoked, when you do the drag? If that is true, try:
UIATarget.localTarget().frontMostApp().elements().withName("copyButton").tap();
But as I see in apple reference your version with calling app.editingMenu() should be fine...
Maybe try calling buttons by position, and you will see which respond:
UIATarget.localTarget().frontMostApp().editingMenu().elements()[0].tap;
UIATarget.localTarget().frontMostApp().editingMenu().elements()[1].tap;
UIATarget.localTarget().frontMostApp().editingMenu().elements()[2].tap;
You should find position of correct one this way. When you have it's position you can check its properties by button.logElement();. With this inf you you should be able to switch back to .withName method instead hardcoded position.
I did this similar to yoosiba but with editingMenu element names.
Using Xcode 4.5.1 and device running iOS 6.
Using Alex Vollmer's excellent tuneup_js for target, app and vtap().
Otherwise you can use UIATarget.localTarget().frontMostApp() and tap().
NOTE: vtap() will delay and retry tapping. Without this you may need to add your own delays.
// tap in textFieldA to see editingMenu.
app.mainWindow().textFields()["textFieldA"].vtap();
app.editingMenu().elements()["Select All"].vtap();
app.editingMenu().elements()["Copy"].vtap();
// must delay before attempting next tap
target.delay(2);
// ... navigate to different section of the app
// tap in textFieldB to see editingMenu.
app.mainWindow().textFields()["textFieldB"].vtap();
// paste clipboard contents copied from textFieldA into textFieldB
app.editingMenu().elements()["Paste"].vtap();
target.delay(2);

Resources