How to scroll Android app with Appium by Python Client - android

I'm testing Android application and need to scroll text. I have tried everything I found here and on a Internet but nothing is working.
Appium v1.4.1 (Server v1.7.2)
Python 3.x
Using selenium webdriver
I need scroll to the bottom of a page, not to specific element
The closest is
self.driver.execute_script("mobile: scroll", {"direction": "up"})
but it is not working
.
Log is:
selenium.common.exceptions.WebDriverException: Message: Unknown mobile command "scroll". Only shell commands are supported.
Thanks

For Android there are 2 good options when it takes to scrolling:
use TouchActions
actions = TouchActions(driver)
el = driver.find_element_by_id(<id of element you press to start swipe>)
action.press(el).move_to(x=100, y=-1000).release().perform()
You can also get screen size of the device to scroll more precisely:
screen_size = driver.get_window_size()
use native UiAutomator scrollIntoView method
self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().resourceId("<id of scrollable view>")).scrollIntoView(new UiSelector().resourceId("<id of element to scroll to>"))')
You can read more here

Related

Unable to drag and drop in Appium Android

I'm using Appium 1.6.5 and Windows 10.
Using the demo app by Appium (ApiDemos-debug.apk), I am trying to drag and drop dots.
View app screen:
This is my current code:
TouchAction actions = new TouchAction(driver);
actions.tap((AndroidElement)driver.findElementByAndroidUIAutomator("text(\"Views\")")).perform();
driver.findElementByAndroidUIAutomator("text(\"Drag and Drop\")").click();
AndroidElement element1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1"));
AndroidElement element2 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_2"));
actions.longPress(element1).waitAction(3000).perform().release();
This error prints when test is run:
org.openqa.selenium.NoSuchElementException: An element could not be
located on the page using the given search parameters. (WARNING: The
server did not provide any stacktrace information)
Any combination of longPress() calls results in this error. I can click & tap, that's fine. When it comes to using other TouchAction methods, then errors occur.
Any idea how to resolve this? Need to know if it's my setup that's wrong or TouchAction method has issues.
You need to long press on that element and drag it to other element.Currently, you're only pressing long and performing the action without releasing it.
Try this:
actions.longPress(element1).moveTo(element2).release().perform();

How to click accept button after taking photo in selenium

I have used
self.driver.keyevent(27)
to capture an image in Python with selenium. However, the screen that I get is as shown below.
Basically I need to click the accept button (with red border) so that the image capture is complete. I know, that I can use in adb shell,
input tap 1000 1500
and it works great. But how do I achieve the same using the Python script?
Even just a way to execute this via the selenium script would be OK for me
input tap 1000 1500
Something like self.driver.execute("adb shell input tap 1000 1500");
In Python, it is uncommon to click on an element by its coordinates, can you please try looking for this accept buttons's Xpath or Css selector expression?
For Android testing, you may consider using this tool
Below is a Python code snippet about how to click on a pair coordinates, as you can see, you need to use an element as reference.
from selenium import webdriver
driver = webdriver.Firefox() //Or whichever browser you prefer
driver.get("your url")
reference=driver.find_elements_by_xpath("Your xpath here")
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(reference, X, Y)
action.click()
action.perform()
Since you need to locate an element as reference anyway, why not simply click on this element directly without using coordinates?
So i figured it out.
import subprocess
subprocess.call(["adb", "shell", "input keyevent 27"]) # capture
subprocess.call(["adb", "shell", "input tap 1000 1500"]) # accept the captured image

Cordova backspace cannot remove image in contenteditable div

I'm using Cordova 3.6.4 in Visual Studio 2013 Community Update 4 to build an apps with a "chat" functionality, the main reason that I use this technology is because I want to, hopefully, write once and can use it in all platforms such as Android phones, iPhones, all mobile phone browsers, all desktop browsers.
In order to let the users inputting the "message" to be sent, I create a [div] which is contenteditable="true" at the bottom left of the html, at the right hand side of this [div], I have two [image buttons], one is the [happy face] button, the other is the [send button]. (You know, just like Whatsapp, Line and WeChat!)
At any time the user can click the [happy face] button to select one of the many "face image" to insert into the cursor at the [div], then I'll add the html codes into the [div], e.g. div.innerHTML += '< img src="1.jpg">'
So, the innerHTML of this [div] can contain characters AND images, e.g.
12< img src="1.jpg" />34< img src="2.jpg" />
Of course, the actual display is:
12[1st Picture]34[2nd Picture]
If the cursor is at the end of this [div], and I clicked the [BACKSPACE], I expect the [2nd Picture] will be removed, if I clicked the [BACKSPACE] again, I expect the letter [4] will be removed from the [div], this is work at ALMOST every platform I mentioned including all mobile browsers in android and iphone/ipad, all desktop browsers. But it does not work when I build an Android app and run it in any Android phone.
Running it as a WebView in android phone, when I click the the [BACKSPACE], the letter [4] is removed instead of the [2nd Picture], when I click the [BACKSPACE] again, the letter[3] is removed. I can NEVER remove the 2 images no matter which IME I'm using.
To work around, I tried to add a keyup/keydown/keypress listener to the [BACKSPACE] but it never fires.
At last, to work around this [BUG], I need to add a third [X] image button and use JavaScript string.replace to remove the < img> tag when users click this [X] button, but it looks very stupid to the users!
It makes me crazy that ALL IMEs do not remove the image for me by pressing the [BACKSPACE], and if the key events are not fired, I cannot remove the images myself!
I tried ALMOST, I think, ALL the suggestions provided by stackoverflow but they don't work at all, either not applicable to CORDOVA, or with compilation error such as [command failed with exit code 8] in Visual Studio.
What should I do?

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.

HttpUnit/HtmlUnit equivalent for android

I'm looking for a browser-simulating library on android, which handles things like
loading a website (http/https)
Redirections: HTTP (3xx Status Codes), JavaScript, HMTL tags
filling out html-forms
easy html parsing (could fall back to JSoup for that one)
HttpUnit or HtmlUnit would do just fine, but both of them are a pain to get running on android.
Is there any other option other than (Android)HttpClient (and therefore doing lots of the above on my own)? Or can I somehow get use of the android webkit/browser?
Thanks in advance!
I would recommend you to have a look at AndroidDriver for selenium. It seems to be a straightforward approach to easy test WebApplications with the Android Testing Framework.
You must use an Activity that includes a WebView in order to test HTTP/HTTPs websites.
The Driver is instanciated with this Activity:
WebDriver driver = new AndroidWebDriver(getActivity());
Here is a sample test, quoted from the link above:
public void testGoogleWorks()
// Loads www.google.com
driver.get("http://www.google.com");
// Lookup the search box on the page by it's HTML name property
WebElement searchBox = driver.findElement(By.name("q"));
// Enter keys in the search box
searchBox.sendKeys("Android Rocks!");
// Hit enter
searchBox.submit();
// Ensure the title contains "Google"
assertTrue(driver.getTitle().contains("Google"));
// Ensure that there is at least one link with the keyword "Android"
assertTrue(driver.findElements(By.partialLinkText("Android")).size() > 1);
}

Categories

Resources