How to test a negative test in Appium

Hi there,

I have a valid login and a invalid login testcase.

How to I validate the invalid login and certify it as PASS?

Thanks!

In the app I test when you put in an invalid login the app responds with an alert. Finding and Dismissing that alert is a pass for our negative test. YMMV.

1 Like

In my app the alert is not recogniseable… So I it fails. When it does that I want to pass it. You know?

which language do you use ?
can you surround with a try catch ?
(that allow you to continue even if there is a fail)

I use Java and Appium. thanks.

you welcome ,
tell me if the try catch is working just because i’m curious ^^

you can validate for pass of Fail by validating New Screen, which might open once you are logged in.

Also you can check for an error text if credentials are invalid.

with something like that you mean ? :

new WebDriverWait(mDriver, CommonActionUtil.DRIVER_TIMEOUT).until(ExpectedConditionFactory

Emile, your answer might be going somewhere. Let me try and I will post here again. Thanks!

1 Like

Hi,
Incase if you are not getting any error /you are not able to capture error
1.Take any element of next page
2.Put that element into a list
3.Check list.size()>0 for existence of element on next page
4.Verify If that is true that means you haven’t got any error message and your test case will fail and if false that means ur TCs will pass

This is a work around but In my opinion you should work on getting the error message capture in your script, if you are able to do so please put that error message element into list and again do same check list.size() >0 and assert accordingly

And for multiple negative values you can use DataProvider with excel sheet in your test method

That is a great explanation @Ankit_Jain thx,
But i think if you don’t take care of the element that you select in the next page you could have stale error on next action …
Sadly i don’t know how to select the last element that is arriving in the DOM …

we use method which is similar to @Ankit_Jain

// Android element
@AndroidFindBy(id = "some_id")
private List<AndroidElement> some_element;

// iOS element
@iOSFindBy(accessibility = "some_accessibility")
private List<IOSElement> some_element;

public boolean isSomeElement_Loaded() {
    boolean bool;
    // let's first reduce search time to 5sec
    PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
    bool = !some_element.isEmpty();
     // let's return search time to default 30sec
    PageFactory.initElements(new AppiumFieldDecorator(driver, 30, TimeUnit.SECONDS), this);
    return bool;
}

in some cases it is possible to use time limitation set directly with element variable

e.g. with android element:

@WithTimeout(time = 5, unit = TimeUnit.SECONDS)
@AndroidFindBy(id = "some_id")
private List<AndroidElement> some_element;

@emile
There are 2 cases for your negative scenarios
1.If your list.size()>0 is true for next page element then call driver.navigate().back(); method to come back to the same page
2.If you are writing negative scenarios for login page and if you are logged in (list.size()>0 on next page) then try to navigate upto logout option (write condition in if else) and tap on logout and repeat same for next set of negative values

I have done as you said. Now i have to again do positive test passing the true value in the sign in text box…in same session. Is it possible ?

Of course. Do the negative test first, and clear the dialog. Then clear the previous input and put in the good value. Negative test has to come first or the state of the app will be ‘already registered’.

can you please give one good example in Java ? as i said i have done the negative test now i want the process going to positive test … is this possible ?

Post your negative code and I’ll try to help you change it.

@Test
public void testcase1()throws Exception {

    System.out.println("Application launching successful! ");

    driver.findElement(By.name("Sign in")).click();


        loginCheck();

    MobileElement element = driver.findElementByName("Try again");
    try
    {
        if (element.isDisplayed())
        {
            element.click();
            System.out.println("Negative checked passing wrong id....test pass");

        }
    }
    catch (NoSuchElementException e) {
        System.out.println("Customer Login successful");
    }

}

@After
public void testCaseTearDown() {
    driver.quit();
}



public void loginCheck(){



   MobileElement e1= driver.findElement(By.xpath("//XCUIElementTypeApplication[@name=\"DEVSafe Home\"]" +
            "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
            "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/" +
            "XCUIElementTypeCell/XCUIElementTypeTextField"));



   Assert.assertTrue(e1.isEnabled());

   String s= "[email protected]";

   String s2="[email protected]";



   e1.sendKeys("" + s2);

    driver.findElement(By.xpath("//XCUIElementTypeApplication[@name=\"DEVSafe " +
            "Home\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
            "/XCUIElementTypeOther/XCUIElementTypeOther/" +
            "XCUIElementTypeTable/XCUIElementTypeCell/XCUIElementTypeSecureTextField")
    ).sendKeys("nicework12345");

    driver.tap(1,40,204,1);

}

}

this is negative test … now i need the positive test …for example passing the correct credential in same session…

Parametrize your functions to make your code reusable. I’m a bit rusty on Java (in my job we use Ruby), but this should get you headed in the right direction. It’s hardly perfect:

@Test
public void testcase1()throws Exception {

    System.out.println("Application launching successful! ");

    driver.findElement(By.name("Sign in")).click();


        loginCheck("nicework12345");

    MobileElement element = driver.findElementByName("Try again");
    try
    {
        if (element.isDisplayed())
        {
            element.click();
            System.out.println("Negative checked passing wrong id....test pass");

        }
    }
    catch (NoSuchElementException e) {
        System.out.println("Customer Login successful");
    }

}

@Test
public void testcase2()throws Exception {

    System.out.println("Running testcase2 ");

    driver.findElement(By.name("Sign in")).click();


        loginCheck("goodPassword");

    MobileElement element = driver.findElementByName("Try again");
    try
    {
        if (element.isDisplayed())
        {
            element.click();
            System.out.println("Positive checked passing right id....test pass");

        }
    }
    catch (NoSuchElementException e) {
        System.out.println("Customer Login successful");
    }

}

// AfterSuite will run after all the tests
@AfterSuite
public void testCaseTearDown() {
    driver.quit();
}



public void loginCheck(String password){



   MobileElement e1= driver.findElement(By.xpath("//XCUIElementTypeApplication[@name=\"DEVSafe Home\"]" +
            "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
            "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/" +
            "XCUIElementTypeCell/XCUIElementTypeTextField"));



   Assert.assertTrue(e1.isEnabled());

   String s= "[email protected]";

   String s2="[email protected]";



   e1.sendKeys("" + s2);

    driver.findElement(By.xpath("//XCUIElementTypeApplication[@name=\"DEVSafe " +
            "Home\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
            "/XCUIElementTypeOther/XCUIElementTypeOther/" +
            "XCUIElementTypeTable/XCUIElementTypeCell/XCUIElementTypeSecureTextField")
    ).sendKeys(password);

    driver.tap(1,40,204,1);

}
}

Thank you so much :wink:

In testcase2 there shouldnt be “Try Again” element …

This is nice …