Instead of using sleep for 40 seconds and then checking you can use waitForExists API of UiObject.
UiObject wifi = new UiObject(new UiSelector().text("Successfull"));
boolean result = wifi.waitForExists(40000);//40 secs specified as timeout in ms
if(!result){ //View not found in 40 secs
// throw error
}
wifi.clickAndWaitForNewWindow();
Hope this will help !!