Element not found in UI Test Case using XCTestCase Xcode 7.3 - ios

I am trying to create UI test Cases using Xcode 7 and I am facing an issue that Ui elements such as buttons, tables are detected randomly when I run the test cases and most of the times it gives an error saying that "Failed to find the element". It is not resolved even after adding delays to the same. Can anyone please help.
The code for the same is
XCUIDevice.sharedDevice().orientation = .Portrait
let app = XCUIApplication()
app.buttons["Login"].tap()
let app2 = app
self.waitForHittable(app.tables.cells.staticTexts["Login with Email"], waitSeconds: 30)
app2.tables.cells.staticTexts["Login with Email"].tap()
app.textFields["Email address"].tap()
app.textFields["Email address"].typeText("anil#gmail.com")
UIPasteboard.generalPasteboard().string = "anil1234"
app.secureTextFields["Password"].doubleTap()
app.menuItems["Paste"].tap()
app.buttons["Login with Email"].tap()
self.waitForHittable(app.navigationBars["HomeView"].buttons["ic menu"], waitSeconds: 60)
app.navigationBars["HomeView"].buttons["ic menu"].tap()
If I write the code app.buttons["Login"].tap()
twice, it works or else it wont be able to find the subsequest control elements.

Related

UIPasteboard.general.string takes too long

In my UI test, I have a test where I am using the UIPasteboard to throw the input around. Just before I use it, I store the current value of the pasteboard into a local temporary variable like so: let currentClipboard = UIPasteboard.general.string ?? "" and it will be restored again at the end of the test like so: UIPasteboard.general.string = currentClipboard. However, sometimes the let currentClipboard = UIPasteboard.general.string ?? "" line takes forever to complete, if at all. I've waited like 1-2 minutes and it still stuck there. I know this because I actually added a breakpoint right below that line and it was never called. Can anybody tell me what's wrong with my code? Thanks.
I've faced the same issue in the simulator only.
If you're testing in simulator then just reset the simulator and it'll start working fine. And I didn't find this type of issue in the real device.

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.

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

Can not access the programatically added UIView in XCTest

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()

iOS UI testing tap first cell of table and failed to quiesce within 60s

Hi I'm a complete newbie teaching myself to UI test. I'm working on an iOS note app and trying to test the deletion of draft notes by creating a draft note and then deleting it right away. There's a list of options you can choose when creating a note, but I just want to default select the first one and am struggling to do so. What I have so far is:
let app = XCUIApplication()
let tablesQuery = app.tables
_ = self.expectationForPredicate(
NSPredicate(format: "self.count = 1"),
evaluatedWithObject: tablesQuery,
handler: nil)
let cells = tablesQuery.cells
let firstCell = cells.elementBoundByIndex(0)
firstCell.tap() // my attempt
On that last line of code, I get this error:
UI Testing Failure - App failed to quiesce within 60s
And I couldn't find anything online to help me with this error.
I don't really know much about the syntax and was looking for some guidance. Thank you.

Resources