Cannot see recording red button for XCUITest in Xcode 9.2 - ios

As I am new to XCUITest, I want to write a test case by recording actions in the simulator but can't see the recording button in XCTestCase class.
What is the process to record a test case?

This might seem trivial, but almost drove me mad when I started with XCUITesting:
Notice that your cursor must be inside a function starting with test:
This won't work, though (Record button is greyed out):

I think that if you are not seeing the record button is because you don't have a iOS UI Testing Bundle in your targets. To do this you need to go to your project file and click the "+" button down on targets and then you add an iOS UI Testing Bundle. Then you will see a new folder named as your new target. There you can add UI tests and record them.

Related

In Xcode 8.2.1 I am unable to link #IBOutlet to the UI

I am having a huge problem when I follow this very simple procedure:
Create a new project
Add a label to the UI
Add '#IBOutlet var testLabel:UILabel!' to ViewController.swift
Open side-by-side editor
Drag from #IBOutlet to the label in the storyboard
At this point you should see two options in the semi-transparent window, testLabel and view however all I see is 'view'.
I recently updated Xcode from the app store and this is when the behavior started. I tried on an older machine with Xcode 7 and the procedure is sound, Xcode 7 detects the Outlet.
Am I missing something here? I tried installing 8.2.1 on a third machine and I am having the same exact problem.
Whenever you get flaky Xcode behavior, it is prudent to empty the "derived data" folder:
locate the derived data folder (it can be found under "Preferences" on "Xcode" menu and then choose the "Locations" tab);
quit Xcode;
empty the derived data folder; and
restart Xcode.
If that doesn't do it, sometimes repeating the process but rebooting before restarting Xcode will do it too.
For what it's worth, while can't reproduce your specific problem, I do experience a related problem that if I manually write an #IBAction and then drag from an event for a button (e.g. "Touch up inside"), it won't always recognize my #IBAction. But if I drag to an empty portion of my code, and let IB create its own #IBAction and give it a name (which can be identical to my manually written #IBAction) and then delete it's templated #IBAction, it then recognizes mine, fine. There's definitely a little flakiness in this connection process.

Why in iOS Framework, xctest is always red colour?

I'm trying to make a new framework to use in my other project. I know if it is red colour means can not find to the path of the file. But this not a file I created!
What I don't understand is why my new framework with pretty much nothing inside is always red colour. I even run test cases (default). I appreciate if anyone can help me to fix this:
No, Scheme(not schema) is different from Target. Check out this thread for better understanding Xcode: What is a target and scheme in plain language?
To solve your issue, click on the WSBandKit framework icon on top left section(adjacent to iPad2 simulator icon); and select editScheme option from drop down. You will be presented with a new screen with some option. Go to Build option and tick the option of Run for your test target. Close the screen and Build the framework for Device.
Check the image for reference. Initially the Run option will be unchecked for test target that you have to check.

UI unit testing stuck on "Waiting for accessibility to load"

I'm getting into UI unit testing, and for a couple days now the UI unit testing refuses to start properly. I setup a simple test to click a button, and when I run it, it hangs starting the app before even starting the test.
Note, it always hangs exactly one minute and then proceeds with the test correctly.
If I delete the app from the Simulator device, or clear the entire Simulator's Content and Settings, then the test runs successfully and instantly on the first run. It hangs each time after that until I delete again. This is not great either, as I end up getting new Location approval prompts each time which might interfere with the app.
What's going on here?
t = 0.00s Start Test
t = 0.00s Set Up
t = 0.00s Launch com.domain.appName
2015-10-06 11:59:24.493 XCTRunner[66707:4085844] Continuing to run tests in the background with task ID 1
t = 0.92s Waiting for accessibility to load
t = 60.92s Wait for app to idle
... rest of test runs immediately
I am also facing this issue but occasionally. Re-attempt or reboot simulator fixes the issue, but temporarily.
The answer at https://forums.developer.apple.com/thread/15780 worked for me:
Pointing the launch screen at a storyboard that is also used for
code, and has connected outlets to a UIViewController subclass. These
outlets can't be resolved by springboard while it generates a launch
image, and it seems to fail over and over, before timing out after 60
seconds.
One solution is to clear out the launch screen setting.
Another solution is to create and add a launch screen to your project by following the instructions at https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/ConfiguringYourApp/ConfiguringYourApp.html#//apple_ref/doc/uid/TP40012582-CH28-SW4 reproduced below:
Choose File > New > File.
Under iOS, select User Interface.
Click Launch Screen and click Next.
Enter a filename in the Save As text field, and click Create
Configure your launch screen file using basic UIKit views, such as UIImageView and UILabel objects, and uses Auto Layout constraints.
To set the launch screen file
If necessary, open the “App Icons and Launch Images” section of the General pane.
From the Launch Screen File pop-up menu, choose a launch screen file.

Xcode UI Test example

I have just recently learned about Unit Testing in Xcode. Now I am trying out Xcode 7 and I see there is a new group for UI Tests when I create a new project.
I watched the WWDC 2015 video and it was pretty good, but do you have a super simple example that I could go through myself? The video examples were a little too complex for me.
Notes
The answer below is my attempt to figure this out, but I welcome any better answers.
I have read these SO questions about UI Testing in Xcode but they are different: docs, reloading, App vs UI, ViewController, multiple, values and properties, pre XCode 7 projects.
Use Unit Tests to test the validity of methods in your classes. You use them to test the code you have written. (See my other example for setting up a simple Unit Test in Xcode.)
Use UI Tests to check the validity of the User Interface. Think of it like having your own robot to go through and do all the normal interactions with your app that a normal user would. This saves you the time of doing it yourself.
At the time of this writing, it is difficult to access many of the properties of the UI components, but just having a test go through tapping them and swiping them confirms that they are there.
Example
This is about the simplest setup and UI test that I could think of: a button that when pressed changes the text of a label.
Set up
Create a new project in Xcode 7+ for iOS 9.0+.
Make sure that Include UI Tests is checked
If you are adding UI tests to a project created before Xcode 7, see this answer. (File > New > Target > Test > Cocoa Touch UI Testing Bundle)
Add a UILabel and a UIButton to the storyboard
Create an #IBOutlet and #IBAction in the ViewController and make the label text change when the button is pressed.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBAction func button(sender: AnyObject) {
label.text = "Hello"
}
}
Do the test
Open the YourProjectUITests file.
Put your curser in the testExample() method. (You can delete the comments)
Press the red Record button
In the app, (1) tap the label, (2) tap the button, and then (3) tap the label again. (4) Press the Record button again to stop recording. The following code should have been automatically generated for you:
func testExample() {
let app = XCUIApplication()
app.staticTexts["Label"].tap()
app.buttons["Button"].tap()
app.staticTexts["Hello"].tap()
}
Use the staticText lines as a starting point for making an XCTAssert. Now you should have:
func testExample() {
let app = XCUIApplication()
XCTAssert(app.staticTexts["Label"].exists)
app.buttons["Button"].tap()
XCTAssert(app.staticTexts["Hello"].exists)
}
Press the diamond on the left to run the UI Test. It should turn green when it passes.
That's it! This showed that the UIButton and UILabel exist and that the text of the label changed. If you want to see it fail (a good idea), you can change "Hello" to something else.
Further study
UI Testing in Xcode
Exploring the New UI Testing Features of Xcode 7
Xcode 7 UI testing, a first look
UI Testing in Xcode 7
#Suragch +1 for the answer. One thing I observed and want to share that every function inside the UI Test case must start with "test". You can append extra name after that. Only this way the button(for clicking to start the test) appears.

Dismiss alert on initial launch on iOS simulator

I'm working on the automated UI tests for my app and I'm having trouble when trying to set up the environment for running the tests. The plan is roughly this:
build the application
shutdown simulator if running
erase the simulator to make a clean install
install my app on the simulator
run UIAutomation tests
Everything is working except when the application is launched by instruments to execute the tests, the alert appears to ask if the user allows notifications. This is all as expected, but I can't find the way to get rid of the alert.
Things I have already tried:
creating onAlert as a first thing in my test script, in case it appears before the my alert callback is defined
delay the target by 5 seconds in case the tests actually run even before the UI of the app is visible in the simulator
I also went through all the permutations of the above that can be found on SO, I never get my onAlert callback invoked, no matter what I do. So another thing I tried was:
try dismissing the alert with applescript
The script I wrote:
tell application "System Events"
tell process "iOS Simulator"
set allUIElements to entire contents of window 1
repeat with anElement in allUIElements
try
log anElement
end try
end repeat
end tell
end tell
and it displays:
static text “MyApp” Would Like to Send You Notifications of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator
static text Notifications may include alerts, sounds, and icon badges. These can be configured in Settings. of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator
UI element 3 of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator
Looks like the buttons are placed inside the "UI element 3" but I can't retrieve any elements from inside it, let alone clicking on it. So I checked with Accessibility Manager:
It sits there as one of the children, the other ones are notification title and message. But when I go to that element, it is highlighted and I see this:
It is identified as generic element, it doesn't have any children...
The interesting thing is when I choose the OK button in the Accessibility Inspector, I can actually see it's a child of the window, yet it is never listed:
Can someone please shed some light on what is going on here? How can I press that button with Applescript?
If you are doing automation using Instrument, the you will need to register callback (onAlert) for performing any action on alerts.
But the problem in your case is that the alert appears before your script actually start executing and at that time no callback is registered for alert.
So if the alert can come with a delay of around 10 sec when you start application, then only it can be handled. But this can only be controlled through source code and not by your Automation code.
So only option which is left is you need to manual dismiss the alert once fresh application is installed
I am also facing same problem and found it to be a limitation of the tool
There are too many limitaion of this tool and thats why i shifted to UFT
I had a similar problem. I just wanted position of the last control on the alert. So I came up with following piece of code:
on get_simulator_last_object_rect(simulator_index)
tell application "System Events"
set ProcessList to (unix id of processes whose name is "iOS Simulator")
set myProcessId to item simulator_index of ProcessList
tell window 1 of (processes whose unix id is myProcessId)
-- Forcefully print the UI elements so that Accessibility hierarchy is built
UI elements
-- Then wait precisely to let the Accessibility view hierarchy is ready
delay 0.5
set lowest_label_lowest_position to 0
set _x to 0
set _y to 0
set _width to 0
set _height to 0
repeat with element in UI elements
set {_x, _y} to position of element
set {_width, _height} to size of element
set current_control_lowest_position to _y + _height
if current_control_lowest_position > lowest_label_lowest_position then set lowest_label_lowest_position to current_control_lowest_position - _height / 2
end repeat
return {{_x, _y}, {_width, lowest_label_lowest_position}}
end tell
end tell
end get_simulator_alert_ok_button_position
I have a desktop app to control my actions. I use this apple script in my Desktop app to get the frame of the last control. Now that I have the frame, I create a mouse event and perform click on the frame, after activating the simulator.
Although I have not yet tried, but I am pretty sure that you can create mouse events from apple script and perform click on the frame / center of the frame.
Hope this helps.
Thanks,
RKS

Resources