Thursday 11 February 2016

How to use APIs to Locate elements and perform actions?



This is basic Appium tutorial for all who want to start with Appium Automation. I will give you brief description of how to use Appium APIs for selecting particular Mobile element and performing desired operation on it.

Good for all of us Appium provides a list of user-friendly APIs for locating elements using ids, xpath, class name, content description etc. Once we locate the element next thing we want to do is perform operations like type text, click a button, long press, swipe, scroll etc.

In this post I will show you most frequently used APIs while writing test cases with example.This basics actions and assertions will help you to quickly start writing test cases. Have a look at these sample test case and comment if you have any doubts.

Video -




How to Use Find Element and FindElements methods in Appium - (FindElement Vs FindElements)

The difference between Find Element and Find Elements method is the first returns a WebElement object otherwise it throws an exception and the latter returns a List of WebElements,  The Find methods take a locator or query object called By. ‘
let us learn about By’ strategies  -

By Name (By Content-desc)

This is also an efficient way to locate an element but again the problem is same as with ID that UI developer make it having non-unique names on a page or auto-generating the names. With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementException will be raised.

driver.findElement(By.name("Shop")).click();
//or 
driver.findElementByName("Shop").click();
//or
driver.findElements(By.name("Shops")).get(1).click();

By Class Name

With this you can find elements based on the value of the class attribute. If an element has many classes then this will match against each of them. A class can contain many elements.

By ID

With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique.

By XPath

Xpath is important locator in Appium, as you will end up many situation where you do not have any options but to use xpath. Xpath can be relative and absolute but it is suggested to always use relative xpath.

Sample Test Case Example -


@Test

 public void testEditText()
{
    //Find element by id and enter Hello in Text box.

    driver.findElementById("edit_name").sendKeys("Hello");
    //Find element by class name and enter Hello in Text box.

     driver.findElementByClassName("com.android.EditText").sendKeys("Hello");
    //Find element by xpath and enter Hello in Text box.

    driver.findElementByXPath("//cass[@value='Enter Name']").sendKeys("Hello");
    //Find element by link text and enter Hello in Text box.

    driver.findElementByLinkText("Enter Name").sendKeys("Hello");
    Assert.assertEquals("Hello", driver.findElementByLinkText("Enter Name").getText());

}

@Testpublic void testButtonClick()
{
    Assert.assertTrue(driver.findElementById("ok_button").isDisplayed());
    driver.findElementById("ok_button").click();
    Assert.assertEquals("OK",driver.findElementById("ok_button").getText());
}

@Testpublic void testCheckBoxSelection()
{
    driver.findElement(By.id("checkbox_1")).click();
    Assert.assertTrue(driver.findElement(By.id("checkbox_1")).isSelected());
}

@Afterpublic void testCaseTearDown()
{
    driver.quit();
}


I hope from the above samples it is clear to you how to use different properties like class name, resource id , xpath etc to select a element and to perform type & click actions which are most commonly used.

Once you are done with this tutorial then you can start writing your test cases right away because now you are clear with how to locate an element and perform action on it so you can start with converting your manual test cases into automated scripts.

If you find this tutorial helpful please share it with your friends and leave your questions or suggestions in comment box . Give your feed back guys it helps me a lot to write more and more. Thanks for reading and sharing




15 comments:

  1. Hi this is maheswaran, Having 4+ years of exp in Android native applicaiton development. Right now i am following your Appium tutorials (simply u did grate job). I have done all initial setup. I struggling to write test case, so kindly can you share me the source code for login page and navigate to home screen.

    ReplyDelete
    Replies
    1. Hi, Thanks for your comments. You can refer the above test case and also view the post on how to locate elements using ids and xpath once you learn about this then you can perform actions like type and click and you can automate login screen in same way.

      Thanks & Regards,
      Anuja

      Delete
  2. The comprehension of instructive framework can likewise be enhanced with the change in the social examinations and current means, the showing designs, course's administration, expanding enthusiasm among the understudies and so forth.

    ReplyDelete
  3. Hi...Iam new to appium and I tried to input values in text box using send keys but it's not inputting in my android virtual device can u help me

    ReplyDelete
  4. Interesting article. You've shared informative article with us. As i already told you ghostwriting is not easy as someone's consider. You've to know details about everything. Here is a link posted below. https://www.bestghostwriters.net/essays-ghostwriting/

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This lokoja fulokoja federal university is really introducing some great services to the student and this is why the student is really wanted to join there. useful site for the students that is very helpful for the writing services.

    ReplyDelete
  7. It is really easy to use the epis to locat something which you wanted to get found and this si why the epis are really interesting though.this site will give you some handy tips and guide to write your term and others academic papers.

    ReplyDelete
  8. This is the basic guide of how to use the API's to locate the elements for the corporateprofileservices.com/professional-company-profile-writing-services/. I understand that guide and will use in the next week.

    ReplyDelete
  9. This is the best way to locate elements. Your guidance will help me to locate elements. Our professionals letter writing services will help the students in their admission. Checkout on this site. I'm waiting for another post.

    ReplyDelete
  10. Its been so long I was looking for some organized and informative article about API and I liked this one.You guys also go here and read this post.Its really helpful to read for automation.

    ReplyDelete
  11. Hi can someone help me I am struggling to run `uiautomationviewer` it returns the following error "Could not create the Java Virtual Machine" "A fatal exception has occurred. Program will exit."

    Thank you in advance

    ReplyDelete