2013-05-20

How to find a webelement or webelements in Robotium?

In this article we will see the functions used for getting a web element from a web application using Robotium while doing testing.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

For Single web element : 

Solo object have a function named  solo.getWebElement(), where we use two parameter to get element. First parameter is for element finder(By object) and 2nd one is the index(nth found), So
-The function returns a web element
-We need to specify By object( the object finder)
-We need to specify index. (Zero based integer index and in here 0 means found 1)

-To select a web element by its class name(Actually it is the type name defines what type of web element that is, like button, text box etc)
solo.getWebElement(By.className("Textbox"),0);

-To select a web element by its css selector
solo.getWebElement(By.cssSelector("CSSAsString"),0);

-To select a web element by its ID
solo.getWebElement(By.id("idAsString"), 0);

-To select a web element by its  Name
solo.getWebElement(By.name("NameAsString"), 0);

 -To select a web element by itsTag Name
solo.getWebElement(By.tagName("tagNameAsString"), 0);

-To select a web element by its text content
solo.getWebElement(By.textContent("contentAsString"), 0);

-To select a web element by its Xpath
solo.getWebElement(By.xpath("xpathAsString"), 0);

For Multiple web element : 

There are two function,

1. solo.getCurrentWebElements(By.[--by xpath or above items--])
This is just multiple version of previous single web element finding

2. solo.getCurrentWebElements(); 

For both cases they provide an array list of WebElements displayed in the active WebView.

Thanks..:)

17 comments:

  1. I am currently working on robotium. I have the apk of an hybrid application.

    Is the any way i can identify the attributes of the web element(like name , css , id etc). Pls let me know if there is any way to do it...

    ReplyDelete
  2. Yes, you can. this post explains properly to use functions for ID/CSS/Name or Xpath. before that, use debug edition of application and then test with robotium.

    ReplyDelete
  3. thanks for the reply.. I will check that

    ReplyDelete
  4. how to open url in my robotium of test application? How can I launch any UKL from my android test application project and then test that web site with robotium.
    Is there any way I can use robotium to launch URL's?

    ReplyDelete
    Replies
    1. Yes, its possible. But, you should know difference between selenium and robotium. robotium need to be defined with activity. So , you have to define browser activity with robotium. You can find web elements. you can click or any other options BUT, you have to keep in mind , selenium is specialized in here, not robotium. Robotium is for testing app not web application.

      Delete
  5. Is there any way I can open the URL from my Test project using robotium. I want to test one of the websites on emulator.
    Please let me know whether robotium supports this feature or not..

    ReplyDelete
    Replies
    1. see my previous comment... and a little solve to open URL... first open browser activity, select address box, type the URL string and then press enter or click GO button.. It is not like as selenium , it captures activity, so you have perform actions manually.

      Thanks

      Delete
  6. I am struggling to find out the type of webelements and click on them.

    I try to use Monitor.bat which is inside of Tools (ADT).

    Let us say I have - Button with text on it- "Buy".

    The object's attributes are shown as class=android.widget.Button

    If I use - solo.clickOnButton("Buy"), it is failing.
    solo.clickOnText("Buy") also failing.

    Any suggestion to overcome this issue.

    Thanks,
    VSB

    ReplyDelete
  7. if it is a web element, then clickonText will not work. Click on text is for native applications. Try using solo.getWebElement(By[use any one method])

    ReplyDelete
  8. I am able to open a url using robotium, but i need to do some action with the components on the page like textbox or buttons etc. how can i do that. I am not able to get the element from the webpage opened in mobile app using robotium. Is there any way to do this.

    ReplyDelete
    Replies
    1. You have to instrument the browser. then you can find elements from web page with robotium . BTW, you may use selenium with android driver also.

      Delete
    2. Can you please provide me sample for this. thanks a lot in advance. :)

      Delete
    3. Please mail me your web app's link, i can make a small example on weekends.

      Delete
    4. thanks for the efforts, but example of clicking on google editfield will also help. Thanks :)

      Delete
    5. okay, I will try to do that in this weekend. and send you link in here

      Delete