Finding an element by id in Appium - android

I have got id associated with the element, but when tried locating this using findElement(By.id()) it doesn't work.
Also, I had gone through few blogs with the same question as given here, but I saw the resource-id in there was prefixed with packagename and :id. In my case, these aren't associated. Screenshot for the element details is below :
I have used the below code to locate the element by id.
- driver.findElement(By.id("loginHome"));
- driver.findElement(By.id("com.packagename:id/loginHome"));
- driver.findElement(By.id("android:id/loginHome"));
- driver.findElement(By.xpath(//*[#id='loginHome']));
But none of the above code snippets worked. Can someone please help me get through with this. Thanks in Advance.

Not exactly answering your question, but providing an alternative method for you to complete the test.
I follow this link https://github.com/appium/appium-dot-app and it works.
You don't need to code everything. The application provides some easy ways to record your clicks and gestures.
For example, when I click something, it will print out the script automatically.
wd.find_element_by_xpath("//android.view.View[1]/android.widget.FrameLayout[2]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]").click()
And you just need to choose your favorite programming language and save it after testing is done.
One thing that I have to point out is that the swipe function is corrupted. Use the following code to perform swipe gestures
wd.swipe(421, 424, 70, 424, 500)

You can try other options as well. Try this:
//add some wait command before the element
Thread.sleep(2000);
driver.findElementsByXPath("//*[#class='android.widget.Button' and #index='4']");

Try adding explicit sleep for page to load before clicking usind ID.

First try to wait for the particular element
MobileElement expelement=(MobileElement) (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//android.widget.Button[#index='4']")));
Then try to do the click(or the activity you suppose to do)
driver.findElement(By.xpath("//android.widget.Button[#index='4']")).click();

Related

Not able to click on an element in Appium

My UI Automator Viewer Screenshot
I want to click on Play Now button and tried the following code:
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Play Now')]"));
and this code:
driver.findElement(By.name("Play Now!"));
These codes are not showing any error but not clicking on Play Now button even.
Please help.
try this one
driver.findElementByAccessibilityId("content written on the button").click();
If you feel your Xpath is right but you are not click on that means that is because of synchronization problem. Just give time to find the element and click. Try like below example
Thread.sleep(5000);
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Play Now')]")).click();
Thread.sleep(5000) gives 5 second time to search for the element. This should work in your case.
if click works fine not need to use any waits.
You can further use implicit or explicit wait once you are perfect with your automation.

Android hierarchy for XPath to interact with specific item

I'm new to automatization, Android, Selenium, Appium and xpath, too. I know it's suck a great beggining.
I write tests for Android devices, but the application I have to test have a lot of costum views. I found out the best way to interact with these custom items is to put an "android:contentDescription" field in the Views. My only question is how to get access to the element with have a specified contentDescription? This is az android specific question, I'm not even sure that the content-desc is the field I'm looking for.
I have the hierarchy provided by Android UI Animator Viewer:
http://i.imgur.com/NUGc56o.png
The ways i've tried:
xpath: //*[contains(#android:contentDescription,'example text')]
I was able to get access by finding them as an ImageView, but as I mentioned I need to work with custom Views
My code looks like somtihng like this:
driver.findElementByXPath("//*[constains(#content-desc,'Login')]").click();
Thanks for the help!
You could also try using Accessibility labels or the UIAutomator locator strategy.
Here's Appium's documentation on those.
Your xpath is incorrect. It should be: "//android.widget.ImageView[#content-desc='Login']"
Here's some pseudocode of what you should do:
login_image = driver.findElementByXPath("//android.widget.ImageView[#content-desc='Login']"); // Gets you the WebElement
print login_image.getClass(); // Just for debugging, make sure it's not nil/null
login_image.click(); // Click on it!

If statement in App Inventor

I'm designing a spaceship game in App Inventor. I have a label (lblScore) update when the ship is hit each time. When the ship is hit 3 times, I want the code inside that to execute yet it doesn't work. I've tried multiple variations of this, such as setting it to lblScore.Text instead. Any idea's on how I can address the issue?
Is the lblscore a label?
If it is all you need to do is have a collision block saying whenever the spaceship gets hit, set lblscore = lblscore + 1
This should fix your issue but I would like to see all of your blocks
Do you increment your lblScore in the Ship.CollidedWith event?
If yes, you should move your if statement there, but instead of using the lblScore component as currently, you should better use the lblScore.Text property instead.
Probably it would help us to help you, if you provide a screenshot of your Ship.CollidedWith event...

MonkeyTalk Android Detect String Containing \n for Button Tap

I am using MonkeyTalk to automate some user test cases for my Android app. Everything is working fine except for when I try and detect a button containing this string:
"Connect\n(Code Required)"
I get this error:
FAILURE: Unable to find Button(Connect\n(Code required))
If I change the button to "Connect" and perform a tap on that value MonkeyTalk has no trouble, but something about the line break must be throwing it off.
After some searching I found this thread that confirmed my suspicious about the line break. There was one suggested fix here, to set the default encoding to UTF-8 (Select the Project > File > Properties > Resources)
However this did not work for me.
I have also tried to find the button using a wildcard like so:
"*(Code Required)"
But this does not seem to be supported either.
Maybe there is an alternative line break character I could use?
Thanks in advance for the help!
Maybe there's a carriage return in there? I know in most text editors a new line actually consists of (carriage return)+(newline).
Also take a look at this:
TextView carriage return not working
Also, depending on how flexible your requirements are, you could use the #N MonkeyId replacement to get the Nth button.
IN javascript you can use below command
app.button("buttonname").tap(x, y);
Use android:contentDesxription="your_component_id" in your view xml file definition or view.setContentDescription("your_component_id"); directly on view in code to make it easy to access in MonkeyTalk.

Android XML Parsing Issue - Multiple occurances of same tag

I followed a tutorial located here: http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ and so far everything worked fine with it. However this only worked nice when there were specific XML tags to go through, such as:
<tag1>
<gettag></gettag>
<gettag2></gettag2>
</tag1>
However my main issue is that I need to get multiple items from within a single tag, like this:
<tag1>
<tag2>text</tag2>
<tag2>text again</tag2>
<tag2>more text</tag2>
</tag1>
I am not able to use the tutorial code to get all of the tag2 items (it stops at the first one). Does anyone know of a good way to grab those "tag2" tags above and spit out the results into a listview?
Any help is appreciated.
Something you may need to do is,
keep a list, If endElement() is tag2, add readtext to list. if endElement is tag1, then create new List and elements.
Check this discussion.

Categories

Resources