# Testing with java. Need an example test

**URL:** <https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350>\
**Category:** Support\
**Created:** [May 24, 2016, 7:59am UTC](https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350 "2016-05-24T07:59:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lera\_Leschenko](https://discuss.appium.io/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lera\_Leschenko](https://discuss.appium.io/u/Lera_Leschenko)\
**Post date:** [May 24, 2016, 7:59am UTC](https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350/1 "2016-05-24T07:59:07Z")

</div>

Hello I am working on automating login Android hybrid applications with Java. Who can set an example for this test automation , as I’m confused .

---

<div class="post-metadata">

**Author:** ![afwang](https://discuss.appium.io/user_avatar/discuss.appium.io/afwang/32/2419_2.png) [@afwang](https://discuss.appium.io/u/afwang)\
**Post date:** [May 24, 2016, 4:42pm UTC](https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350/2 "2016-05-24T16:42:01Z")

</div>

What are you confused about?

A simple example using TestNG:

```
@BeforeMethod 
public void setUpDriver() {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability(MobileCapabilityType.PLATFORM, MobilePlatform.ANDROID);
    caps.setCapability(MobileCapabilityType.APP, "/path/to/my/app");
    caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
    driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), caps);
}

@Test 
public void checkActivity() {
    Assert.assertEquals(driver.currentActivity(), "com.example.MainActivity");
}

@AfterMethod(alwaysRun = true)
public void quitDriver() {
    driver.quit();
}
```

---

<div class="post-metadata">

**Author:** ![Lera\_Leschenko](https://discuss.appium.io/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lera\_Leschenko](https://discuss.appium.io/u/Lera_Leschenko)\
**Post date:** [May 25, 2016, 6:31am UTC](https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350/3 "2016-05-25T06:31:09Z")

</div>

I need a test to check the login and password and login authorization for app

---

<div class="post-metadata">

**Author:** ![afwang](https://discuss.appium.io/user_avatar/discuss.appium.io/afwang/32/2419_2.png) [@afwang](https://discuss.appium.io/u/afwang)\
**Post date:** [May 25, 2016, 4:15pm UTC](https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350/4 "2016-05-25T16:15:30Z")

</div>

Well, I can’t really do your job for you, but I can try answering any questions you might have about how to use Appium.

---

<div class="post-metadata">

**Author:** ![Lera\_Leschenko](https://discuss.appium.io/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lera\_Leschenko](https://discuss.appium.io/u/Lera_Leschenko)\
**Post date:** [May 26, 2016, 6:13am UTC](https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350/5 "2016-05-26T06:13:30Z")

</div>

OK. In the application, the default AlertDialog with two buttons to appear after 30 seconds. Please tell me , how do I make the alert waiting for ?

`@Test public void ScroooToTa() throws IOException{ try { driver.findElement(By.className("android.widget.ImageButton")).click(); }catch(Exception e){ captureScreenShots(); Assert.fail(); }}`

---

<div class="post-metadata">

**Author:** ![afwang](https://discuss.appium.io/user_avatar/discuss.appium.io/afwang/32/2419_2.png) [@afwang](https://discuss.appium.io/u/afwang)\
**Post date:** [May 26, 2016, 6:03pm UTC](https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350/6 "2016-05-26T18:03:05Z")

</div>

You need your test script to wait. My suggestion is one of the following:

- If the dialog always appears after exactly 30 seconds, use Thread.sleep(…)
- If the dialog appears after an inconsistent amount of time, use [WebDriverWaits](http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html)

WebDriverWaits are my favorite types of waiting used for automation. You only have to pass in an anonymous class whose only function is to check if the condition you’re waiting on has appeared.

```
WebDriverWait dialogWait = new WebDriverWait(driver, 60); //60 for 60 seconds
dialogWait.until(new Predicate<WebDriver>() {
    @Override
    public boolean apply(WebDriver input) {
        //Your check on the app's condition goes here
        List<WebElement> myDialogButtons = input.findElements(By.className("android.widget.ImageButton"));
        return myDialogButtons != null && myDialogButtons.size() > 0;
    }
});

```

The way this works is that the WebDriverWait class’s until() method runs your supplied apply() method in a loop until apply() returns true (or the timeout supplied in the WebDriverWait constructor is reached). Your apply() method should only return true when the state of your application is ready to continue with the rest of your test. If your apply() method returns false or throws a NoSuchElementException, then the WebDriverWait will wait 0.5 seconds before starting the next iteration in the loop.

---

<div class="post-metadata">

**Author:** ![argneshu](https://discuss.appium.io/user_avatar/discuss.appium.io/argneshu/32/9061_2.png) [@argneshu](https://discuss.appium.io/u/argneshu)\
**Post date:** [September 28, 2016, 12:14pm UTC](https://discuss.appium.io/t/testing-with-java-need-an-example-test/10350/7 "2016-09-28T12:14:16Z")

</div>

How to verify the same thing in case of IOS apps.  
I need to verify current screen Name(View controller) in IOS app
