Robotium ---> Instead of webview button, it clicks on edittext box - android

I'm pretty new with Appium. I tried robotium blackbox way with .apk file to fill up a small web-view form which is auto injected by other rails server and every thing is working fine for me but
When i tried to click on Save & Next button it clicks on the edit-text box in which my previous entry filed through script.
I used all the way
solo.waitForText("SaveAndNext");
solo.clickOnWebElement(By.id("SaveAndNext"));
solo.clickOnWebElement(By.name("Save & Next"));
solo.clickOnWebElement(By.textContent("Save & Next"));
But it click on the edittext box.
Here is my code :-
solo.waitForActivity("ViewQuestions");
getInstrumentation().waitForIdleSync();
solo.clickOnText("(?i).*?Yes.*");
solo.enterTextInWebElement(By.className("text_answer"), "2");
solo.hideSoftKeyboard();
solo.waitForText("SaveAndNext");
//solo.clickOnWebElement(By.id("SaveAndNext"));
//solo.clickOnWebElement(By.name("Save & Next"));
//solo.clickOnWebElement(By.textContent("Save & Next"));
for (WebElement webElement : solo.getCurrentWebElements()) {
Log.d("Robotium", "id: " + webElement.getId() + " textContent: "
+ webElement.getTagName());
if (webElement.getId() == "SaveAndNext") {
solo.clickOnWebElement(By.id("SaveAndNext"));
}
}
I have checked that if (webElement.getId() == "SaveAndNext") is found passed.
And in logcat
**Robotium id: SaveAndNext textContent: INPUT**
is shown.
Any help will be appreciate.

Remove this for loop and use just:
solo.clickOnWebElement(By.id("SaveAndNext"));
Btw you cannot compare Strings like:
webElement.getId() == "SaveAndNext"
You should rather use equals:
"SaveAndNext".equals(webElement.getId())

I had a similar problem with view clicks were not proper. Got this solution from robotium.org
Why do text and button clicks get wrong?
If this problem happens on one of the supported versions then try to add this tag to the test project's AndroidManifest.xml
uses-sdk android:targetSdkVersion="YOUR_VERSION"
Where YOUR_VERSION is 6 for Android 2.0, 7 for Android 2.1 and 8 for Android 2.2.
If that does not solve the problem then try to add this tag to the AndroidManifest.xml of the application you want to test:
supports-screens android:anyDensity="true"

Related

How to use hints of Android Studio correctly

When I work with android studio it shows hints very often, I have 2 questions about it:
1) Why do android studio when I press enter (in scenario like on screenshot) makes just
noteRef.get()
.addOnSuccessListener {}
}
instead of
noteRef.get()
.addOnSuccessListener { documentSnapshot -> }
}
Is there a way generate { documentSnapshot -> } automatically?
2) Second problem: imagine, I have code like this:
var planetMarsSize = 0
var planetEarthSize=0
planetMarsSize = 5
for example I made mistake in this code and have to make "planetEarthSize = 5" instead of "planetMarsSize = 5" so i place cursor between "Mars" and "size", delete "Mars" and start tipping "Earth" - in this case Android studio shows me a hint to autocomplete text, I press enter. At the end text become "planetEarthSizeSize = 5" so android studio shows error. How can I make android studio to autocomplete "planetEarthSize" instead of "planetEarthSizeSize"?
Video of problem: https://photos.app.goo.gl/qAbcxtZsV4URwWwR8
1) The only difference between the two methods is that in the second, you can choose the name of the parameter. In the first one, you can access the parameter with it
2) You can press TAB on your keyboard to overwrite the whole variable name (it actually says this in the little dialog that pops up :) )

how to clear a text edit field? android python appium

how to clear text edit field? android python appium?
I tried that way:
el = driver.find_element_by_id('text edit field id').clear()
el.click()
el.clear()
however, it actually deletes only one symbol.
I think may by call an el.click() twice and send keycode to delete highlighted text area but how to highlight the text?
Interestingly, such problem reproduces in Sony Xperia z3 compact, android 5.0.1.
In lg, nexus android 4.4,5.1 accordingly, the above code works well
i had faced same issue with my android application but i am doing automation in java , so for the solution i am performing tap Action twice near the location of the element so it will select all the text which already written in text field and after that click on that "cut" symbol so it will clear text field.
i have written a method for sendkeys in android device.
public void typeForAndroid(AppiumDriver appiumDriver,WebElement ele,String text)
{
if(ele.getText().isEmpty())
{
ele.sendKeys(text);
}
else
{
TouchAction action=new TouchAction(appiumDriver);
action.tap(ele.getLocation().getX()+5, ele.getLocation().getY()+5);
action.tap(ele.getLocation().getX()+5, ele.getLocation().getY()+5);
appiumDriver.performTouchAction(action);
pause(2);
try{
appiumDriver.findElementById("android:id/cut").click();
}
catch(Exception e)
{
appiumDriver.sendKeyEvent(67);
}
pause(2);
ele.sendKeys(text);
}
}
this is working for me. I hope this might works for you.
in this i have used appiumdriver but you can also use androiddriver.
Thanks

Robotium for Android - solo.searchText () not working

I'm having a problem with searchText() function used with Robotium.
I'm searching for this string:
<string name="provisioning_wizard_title_2">Service Activation Wizard (Step 2)</string>
I defined it in string xml, and I'm searching it this way:
Activity act=solo.getCurrentActivity();
String string = solo.getString(act.getResources().getIdentifier("provisioning_wizard_title_2", "string", act.getPackageName()));
It fails when I call
assertTrue(solo.searchText(string));
and it fails also if i call:
assertTrue(solo.searchText("Service Activation Wizard (Step 2)"));
even if that is the string I'm seeing in the focused screen activity.
The strange thing is that it works if I use the same string without last character:
assertTrue(solo.searchText("Service Activation Wizard (Step 2"));
and it works if I use
assertTrue(solo.searchText(string.substring(0, string.length()-1)));
I hope someone could help me.
Sorry for my english!
Thank you.
PROBLEM SOLVED
I solved problem thanks to Renas Reda (Founder and maintainer of Robotium).
The problem is due to regex support in searchText(); some characters will trigger it.
Use Pattern.quote(string)
Example: Pattern.qoute("provisioning_wizard_title_2") and eveything works good!
I usually use Solo#waitForText(...) so I make sure I'm not losing some race condition.
Try this:
assertTrue(solo.waitForText( solo.getString(R.string. provisioning_wizard_title_2) );
Make sure you are importing the correct R.java file from your production project (not the test project).
I'm not sure, what string you are fetching. Try to log it:
Log.d("Debug", "String is: " + string);
you should rather call, here may be another issue related to package as activity may be in another package than main package of application:
String string = solo.getString(act.getResources().getIdentifier(act.getPackageName()+":string/provisioning_wizard_title_2"), null, null));
You can also get all visible texts, to make sure, what is on the screen:
for(TextView textView : solo.getCurrentViews(TextView.class)) {
Log.d("DEBUG", "Text on the screen: " + textView.getText().toString());
}

jQuery.mobile popup immediately hides after showing

I have a small phonegap application with jquery mobile and backbone.
I'm trying to show popup to user by manually calling .popup() method.
Everything works fine on iOS but on android I got strange issue: popup is showing for few moments and than disappear.
Here the actual code:
var PostView = Backbone.View.extend({
events: {
'touchend .add-comment-button': 'addComment'
},
addComment: function() {
this.$(".comment-popup").popup('open', { history: false });
return false; // Stop bubbling.
}
});
I'm using history: false because this popup is actualy part of subpage.
The code looks very simple, I'm just can't understand why it can disappear, and why this happen only on android devices.
Thanks, and sorry for my bad english.
I spent hours trying to fix this problem.
Finally I ended up doing the following two things that seemed to fix the problem.
1 - Use the uncompressed jqm file. i.e jquery.mobile.1.2.0.js
2 - I was triggering the popup programatically using the 'tap' option - once changed to the 'click' option it worked.
$('.option').live('click', function() {
$('#popup-div').popup('open');
});
I spent hours trying to fix this problem.
Finally I ended up doing the following two things that seemed to fix the problem.
this code snippet may help you ->
$('#testBtn').on('tap',function(e){
console.log("button clicked");
e.preventDefault();
$('#testPOPUP').popup("open");
});
Please note i have used e.perventDefault().
I didn't feel like changing my .tap() events to the click event and I didn't have a case where I could use preventDefault()so I just added a timeout to the popup('open') line. My hoverdelay in jqm is set to 150 so I set this timeout to 600 just to be on the safe side. Works fine, doesn't feel sluggish for the user.
One way to 'fix' it is by setting data-history="false" on the popup div
See also this question
JQuery Mobile popup with history=false autocloses
I have the exact same problem when trying to use popup('open') on an android 2.3 device (both in native browser and in firefox) and it works just fine on browsers on other devices. I'm also using backbone event management to open my popup (used the tap event and no aditionnal options to popup).
What I did to 'correct' the problem is that I removed the backbone event management for this event and added a listener in the render function. In your case this would look something like this :
events: {
// 'touchend .add-comment-button': 'addComment'
},
render: function() {
$(this.el).html(this.template(this.model));
$(this.el).find('.add-comment-button').tap(function(el){
this.addComment(el);
return false;
}.bind(this));
}
I have no idea where the problem comes from (must be some incompatibility between backbone and jquery mobile) and why we only see it on android but for the moment with this workaround my app seems to work fine on any device.
Edit: oops, it turns out that in my case the problem was I was missing "return false;" in the function dealing with the event.
Now that I added it, it works correctly with the backbone event management.
Sadly that doesn't explain why you have the issue and why I was seeing it only on android.
In case it helps anyone, I had the same problem occurring with Bing Maps, with the Microsoft.Maps.Events.addHandler(pin, 'click', callback) method.
Not particularly nice, but instead I stored an ID in pushpin._id and did the following:
$("#page").on('vclick', function (event) {
if (event.target.parentElement.className === "MapPushpinBase") {
$("#stopPopup").popup('open');
}
});
One brute force option is to check whether popup was hidden and reopen it.
In a loop, because the exact time the popup becomes hidden seems to be varied.
var hidden = $('#' + id + '-popup') .hasClass ('ui-popup-hidden')
if (hidden) $('#' + id) .popup ('open')
A working example: http://jsfiddle.net/ArtemGr/hgbdv9s7/
Another option could be to bind to popupafterclose:
var reopener = function() {$('#' + id) .popup ('open')}
$('#' + id) .on ('popupafterclose', reopener)
$('#' + id) .popup ('open')
Like here: http://jsfiddle.net/ArtemGr/gmpczrdm/
But for some reason the popupafterclose binding fails to fire on iPhone 4 half of the time.

How to select any option of select tag using android web driver

I am writing test cases for a website for android device. In which I need to select an option from the drop down list of the page. But it seems that android web driver does not provide any solution regarding it.
I tried the Select API but it is not working.
Code snippet
Select loginType = new Select(this.driver.findElement(By.xpath(LOGIN_TYPE_CHOICE)));
loginType.selectByValue("smartphone");
driver.findElement(By.id(LOGIN_BUTTON)).click();
Looking for some workaround.
I'm using c# to run selenium tests against android, firefox Chrome and IE and I enounterd the same problem with the android driver.
This worked for me : (it should work in java if you refactor the code according to the Java conventions)
string jsToBeExecuted="document.getElementById('<insert dropdown Id here>').selectedIndex=<insert index here>";
IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)this.Driver;
jsExecutor.ExecuteScript(jsToBeExecuted);
Note that no changes will be rendered on screen ! ! !
Upon submitting the element with the selected index will be used.
It is up to you if you want to add some tweaks to it to select the elements by text or whatever you like.
I have the following assumption from my expirience of automation web applications with selenium.
as I know selenium is uncapable of interacting direcrly with dropdown options as they considered to be invisible (inactive).
The way it always works - is to use js for this.
First of all locate element properly with css selector properly and verify it with firepath (addon to firebug in ffox)
So you got css sselector:
String css=....;
public void jsClickByCss(String cssLocator){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssLocator+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
}
jsClickByCss(css);
You can also try another approach using Builder, advanced user interactions API:
The idea is quite simple. First of all you should make dropdown roll down so element you want to click on become visible , wait with driver.manage.timeout and then select needed dropdown element and click.
WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.MoveToElement(mnEle).perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();
You can read some additional info here
Hope this works for you)
here is solution for Ruby:
To select value from list needs to execute javascript, here is example:
HTML:
<select id="select_id">
<option id="option_id_1" value="value_1" name="OPTION1"> OPTION1 </option>
<option id="option_id_2" value="value_2" name="OPTION2"> OPTION2 </option>
Updated:
much easier way:
$('#select_id').val(value_1)
Code:
find elements by id attribute:
browser.execute_script("$('#select_id').val($('#option_id_1').val())")
find elements by name attribute:
browser.execute_script("$('#select_id').val($('option[name=OPTION2]').val())")
Works perfectly for me.

Categories

Resources