XCode UI Test fails 'element not found' check - xcode

I'm writing classes extending from XCTestCase to do some UI Automation tests. In my test base class I have something like:
- (BOOL)isOnLoginFormView
{
return self.app.textFields[#"User ID"] != nil && self.app.secureTextFields[#"Password"] != nil && self.app.buttons[#"<b>Login</b>"] != nil;
}
I use this method in another test case method. In a situation when the login form is not present it should continue with some other code (checking for the existence of another view) but my UI test fails in any case if the above method return NO. Is there anything I can do so the test is not failing if the above method returns NO? Optimally the test cases should only fail if I fail them explicitly with an assert.

Nevermind! I figured out it works with this:
return self.app.textFields[#"User ID"].exists && self.app.secureTextFields[#"Password"].exists && self.app.buttons[#"<b>Login</b>"].exists;

Related

Multiple conditions in While controller not work

I need multiple conditions in While controller, but if I have more then one condition every time is infinite loop.
${__javaScript("${type}" != "book",)} && ${__javaScript("${author_surname}" != "Novak",)} && ${__javaScript("${author_name}" != "John",)}
As per Using the While Controller in JMeter article I think you need to put all the conditions into single __javaScript() function like:
${__javaScript("${type}" != "book" && "${author_surname}" != "Novak" && "${author_name}" != "John",)}
also as per the function documentation:
javaScript is not the best scripting language for performances in JMeter. If your plan requires a high number of threads it is advised to use __jexl3 or __groovy functions.
so it worth considering migrating

Is this a bug of findbugs?

I have a jenkins plugin which contains code like this:
public int getLastBuildNumber() {
if (project != null && project.getLastBuild() != null ) {
return project.getLastBuild().getNumber();
}
return 0;
}
When I publish the code with mvn release:prepare release:perform -Dusername=myusername -Dpassword=mypassword, I got a bug saying Possible null pointer dereference.
Here's the result of mvn findbugs:gui:
Why does it say The return value from a method is dereferenced without a null check?
I think project != null && project.getLastBuild() != null is the null check, isn't it?
Is this a bug of findbugs? How can I solve it? Or can I disable findbugs when releasing my jenkins plugin?
You can visit here for the full code.
Unless your Project class is immutable or (at least) the value returned by getLastBuild() is a final field, your null-checks are not enough.
That's because, in theory, some other thread may call project.setLastBuild(null) just before your return statement:
if (project != null && project.getLastBuild() != null) {
// thread T does project.setLastBuild(null) here -- for whatever reason
return project.getLastBuild().getNumber(); // NPE!
}
To make this bullet proof, you need to move away from those moving parts and make your own local copies (synchronization is hardly an option in this case, imho):
LastBuild lastBuild = project != null ? project.getLastBuild() : null; // this only works as long as your project reference is final
return lastBuild != null ? lastBuild.getNumber() : 0;
Or, even better, make use of the Java8 Optional to perform all the necessary null-checks (implicitly) in one go:
return Optional.ofNullable(project)
.map(Project::getLastBuild)
.map(LastBuild::getNumber)
.orElse(0);

If not let - in Swift

is there is a way to negate the "if let" in swift?
This looks silly to me:
if let type = json.type {
} else {
XCTFail("There is no type in the root element")
}
I can't use XCTAssertNotNil, because json.type is a enum.
enum JSONDataTypes {
case Object
case Array
case Number
case String
}
Thanks a lot
EDIT: it is a:
var type: JSONDataTypes? = nil
Swift 2.0 (Xcode 7) and later have the new guard statement, which sort of works like an "if not let" -- you can conditionally bind a variable in the remainder of the enclosing scope, keeping the "good path" in your code the least-indented.
guard let type = json.type else {
XCTFail("There is no type in the root element")
}
// do something with `type` here
The catch to this is that the else clause of a guard must exit that scope (because otherwise you'd fall into code after that clause, where the guarded variables, like type above, are unbound). So it has to end with something like return, break, continue or a function that is known to the compiler to never return (i.e. annotated #noreturn, like abort()... I don't recall offhand if that includes XCTFail, but it should (file a bug if it's not).
For details, see Early Exit in The Swift Programming Language.
As for really-old stuff... There's no negated form of if-let in Swift 1.x. But since you're working with XCTest anyway, you can just make testing the optional part of an assertion expression:
XCTAssert(json.type != nil, "There is no type in the root element")
Here's how you do it:
if json.type == nil {
// fail
}
Another alternative I've used a few times:
switch json.type
{
case .None: // ...
case .Some(.Object): // ...
case .Some(.Array): // ...
case .Some(.Number): // ...
case .Some(.String): // ...
}
Since the ? is actually Optional<T> which is an enum on its own, defined as:
enum Optional<T> : Reflectable, NilLiteralConvertible
{
case None
case Some(T)
...
}

failing a XCTestCase with assert without the test continuing to run but without stopping other tests

I'm trying to test my application using the XCTest framework.
I want my single test case to fail if some logical condition holds (using an assertion).
I don't want the rest of the code in the test case to run, because this might lead to problems (access to null pointers, for example)
I also want the rest of the test case to run normally, and just the failed test to be marked as failed.
I've noticed XCTestCase has a property called continueAfterFailure.
However, setting it to YES caused the failed test to continue executing lines after the assertion, and setting it to NO caused the rest of the tests not to run at all.
Is there a solution to this issue?
Pascal's answer gave me the idea to achieve this properly. XCTool now behaves like OCUnit when an assertion fails: the execution of the test case is aborted immediately, tearDown invoked and the next test case is run.
Simply override the method invokeTest in your base class (the one that inherits from the XCTestCase class):
- (void)invokeTest
{
self.continueAfterFailure = NO;
#try
{
[super invokeTest];
}
#finally
{
self.continueAfterFailure = YES;
}
}
That's it!
The easiest way is to add:
continueAfterFailure = false
into setUp() method. So it will look like this:
Swift
override func setUp() {
super.setUp()
continueAfterFailure = false
}
Objective-C
- (void)setUp {
[super setUp];
[self setContinueAfterFailure:NO];
}
One option would be to check the condition normally, then fail and return from the test if it is false.
Something like this:
if (!condition) {
XCFail(#"o noes");
return;
}
You could wrap this up in a helper macro to preserve readability.
BDD test libraries like Kiwi are more elegant for this sort of thing, as they make it easier to share setup between many tests which leads to fewer assertions per test.
I am able to use continueAfterFailure and let the other tests run by using this pattern:
self.continueAfterFailure = NO;
#try
{
// Perform test code here
}
#finally
{
self.continueAfterFailure = YES;
}
In Swift projects, I use a helper function (defined in a shared superclass of all my tests which itself extends XCTestCase):
/// Like `XCTFail(...)` but aborts the test.
func XCTAbortTest(_ message: String,
file: StaticString = #file, line: UInt = #line
) -> Never {
self.continueAfterFailure = false
XCTFail(message, file: file, line: line)
fatalError("never reached")
}
As the comment suggests, the call to fatalError is never actually executed; XCTFail aborts the test in an orderly fashion (tearDown is called, next test runs, etc.). The call is only there to trick the compiler into accepting Never as return type since XCTFail returns Void (it does return if continueAfterFailure == true).
Note that self.continueAfterFailure is reset to the default true for every test method. You can also make that explicit in setUp().

Looking for Task.IsPending

I often do something like this:
if (task != null && !task.IsCompleted && !task.IsCanceled && !task.IsFaulted)
{
// do something, e.g. cancel the task
}
It would be great to have task.IsPending as a shortcut for !task.IsCompleted && !task.IsCanceled && !task.IsFaulted, but it's not there. And task.Status == TaskStatus.Running is not the same, as task can be in one of the waiting states.
I have a custom Task extension method for this, but I'm curious why it is not there in the first place. Is checking for pending status this way considered somehow deprecated?
I think you're just looking for:
if (task != null && !task.IsCompleted)
As documented, IsCompleted covers faulted and canceled states as well as RanToCompletion:
IsCompleted will return true when the task is in one of the three final states: RanToCompletion, Faulted, or Canceled.

Resources