Appium tests Android Launcher to obtain the target application by sliding the form. appium android

Source: Internet
Author: User
Tags appium

Appium tests Android Launcher to obtain the target application by sliding the form. appium android

The so-called Launcher refers to the android Desktop Management Program. All application icons are placed on the launcher. In fact, this is a very simple example, just to verify some ideas.

1. Tutorial Purpose

The purpose of this test is:

  • Try using the form slide function swipe
  • Wondering whether the android Launcher can be properly tested by specifying the package and activity.
2. The experiment background is to use appium to start the launcher, and then slide the window to obtain the "Notes" app that comes with the sdk on the third desktop. As shown in
3. test Step 3.1 obtain the two capabilities of the launcher package and activity, which can be directly viewed by HierarchyViewer.
3.2 coding implementation
Package majcit.com. appiumDemo; import io. appium. java_client.android.AndroidDriver; import java.net. URL; import org. junit. test; import org. junit. after; import org. junit. before; import org. openqa. selenium. dimension; import org. openqa. selenium. noSuchElementException; import org. openqa. selenium. point; import org. openqa. selenium. webElement; import org. openqa. selenium. remote. desiredCapabilities; import static org. h Amcrest. matchers. *; import static org. hamcrest. matcherAssert. assertThat;/*** Unit test for simple App. */public class LauncherTest {/*** Create the test case ** @ param testName of the test case */private AndroidDriver driver; @ Before public void setUp () throws Exception {// set up appium // File classpathRoot = new File (System. getProperty ("user. dir "); // File appDir = new File (classpathRoo T, "apps"); // File app = new File (appDir, "NotePad.apk"); DesiredCapabilities capabilities = new DesiredCapabilities (); capabilities. setCapability ("deviceName", "Android"); // capabilities. setCapability ("platformVersion", "4.2"); // capabilities. setCapability ("platformName", "Android"); // capabilities. setCapability ("app", app. getAbsolutePath (); capabilities. setCapability ("appPackage", "com. miui. home "); Capabilities. setCapability ("appActivity", "com. miui. home. launcher. launcher "); // capabilities. setCapability ("appActivity ",". notesList "); // capabilities. setCapability ("autoLaunch", "false"); // capabilities. setCapability ("noReset", true); driver = new AndroidDriver (new URL ("http: // 127.0.0.1: 4723/wd/hub"), capabilities );} @ After public void tearDown () throws Exception {driver. quit () ;}@ Test pu Blic void launchNotePad () throws InterruptedException {WebElement el = null; WebElement screen = null; Point point = null; Dimension size = null; boolean found = false; int pageCount = 3; // Assume that there are totally 3 screens to be swiped. int xStart =-1; int yStart =-1; int xEnd =-1; int yEnd =-1; // Get the start and end coordinates for swipe Thread. sleep (3000); screen = driver. findElemen TById ("com. miui. home: id/cell_layout "); point = screen. getLocation (); size = screen. getSize (); xEnd = point. getX (); yEnd = point. getY () + size. getHeight ()/2; xStart = point. getX () + size. getWidth ()-5; yStart = yEnd; System. out. println ("starX:" + xStart + "\ nstartY:" + yStart + "\ nendX:" + xEnd + "\ nendY:" + yEnd ); // originally, I wanted to judge how many screens there were by judging the small dots on the screen, but I found that xPath was ineffective and the parent directory could not be positioned at all, only the last // android. widget. im AgeView is effective, so we can find 75 elements. /* List <WebElement> pageImages = driver. findElementsByXPath ("// android. view. view/android. widget. linearLayout/android. widget. imageView "); assertThat (pageImages. size (), is (3); for (WebElement e: pageImages) {e. click ();} * // Swipe all screens till get the expected control int currentPage = 0; while (found = false & currentPage <pageCount) {found = true; currentPage + = 1; try {el = driver. findElementByName ("Notes");} catch (NoSuchElementException e) {found = false; System. out. println (e);} if (found = true) break; driver. swipe (xStart, yStart, xEnd, yEnd, 100); Thread. sleep (1000);} assertThat (found, is (true); assertThat (el, notNullValue (); el. click ();}}
The steps are described as follows:
  • Prepare required capabilities and send them to the appium server. Pay attention to specifying the app because we do not need to re-install Launcher.
  • Find the control that represents the entire screen, and obtain its location and size attributes to calculate the coordinates of the sliding start and end. Note that if the start coordinate is the boundary of the screen, you need to adjust the next pixel (in this example, subtract 5 pixels) to avoid errors.
  • Slide each page until the target control is found.




What is the tool box for mobile app testing?

1. Android black box testing tool
1) includes the built-in monkey. Monkey is a command line tool in Android and can be run in the simulator or on the actual device.

2) robotium and appium, which are used for automated testing of black boxes. You can test the target application with the source code or APK only. Provides APIs that mimic user operations, such as clicking on a control and inputting Text
And so on. (Recommend that you study this tool. It is open-source and I have materials)

2. white box testing. Android integrates the JUnit framework in the SDK. Therefore, you can develop white-box Android testing code based on the JUNIT framework.

3. Android testing methods are mostly similar to web testing methods, but some special testing scenarios are added, for example, traversal testing, story-driven testing, adaptation testing, power testing, weak network testing, and traffic Testing

4. Compatibility testing: First, you can use virtual machines to ensure compatibility between systems. However, more tests must be performed on real machines, because the upper-layer APIs of each real machine are different in use and rendering.

If you want any materials or want to study together, you can mail me: test_sunny@hotmail.com


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.