Module automated stress testing using the testng

Source: Internet
Author: User
Tags appium testng

Preface
As a result of the recent work on the Mirage project, when analyzing a customer problem, the customer mentioned the use of testng to carry out some stress tests, such as continuous shooting more than 500 times, continuous access to set, slide, exit 200 times. At that time consulted the test colleagues in the next project, found that we do less stress testing, performance testing mainly used in the monkey test. Helpless, only oneself study pondering.
This article is used to document what has been learned, and also for testing or development colleagues who will use this tool for reference.
With this tool, development colleagues can verify that their modules are having problems.
PS: If the text is incorrect, please be sure to help correct it.
testng Introduction
    • TESTNG is a test framework in Java, similar to JUnit and NUnit, with the same functionality, just more powerful and more convenient to use.
    • You can use testng to perform some automated tests, such as stress testing.
    • The stress test verifies that the system app, such as the camera, settings, and so on, will hang after repeated runs, and will be stuck.
testng Environment Building (Windows-based)
    • Install JDK (TestNG requires JDK 7 or higher)
: http://www.oracle.com/technetwork/java/javase/downloads/index.html Note: x64 is 64-bit, if the computer is 32-bit, please go to the official website to download windows X86jdk-7u79-windows-i586.exe Configuring Environment Variables 1. Locate the JDK installation directory, such as D:\Program files\java\jdk1.7.0_51\bin2. Advanced system settings, right-click Properties, Computer- > Environment variables, find the PATH variable in the system variable, add the above directory to the variable value, remember not to accidentally delete the other values, each value is used; Save. 3. Open the cmd console, execute java-version, and make sure the version is installed correctly
    • Install Eclipse
    • Configuring the SDK
Configure the SDK directory to an environment variable, as in configuring a JDK
    • Installing Appinum
: https://bitbucket.org/appium/appium.app/downloads/
    • The necessary jar packages
: Http://pan.baidu.com/s/1jGy588I
    • Installing the testng Eclipse plugin
1. Select Help/install new Software2. After entering Http://beust.com/eclipse, select TestNG, install 3. After the installation is successful, select Wondow->show view-> Find TestNG (under Java)->ok
testng Code Writing
    • Create a project import necessary package
File->new->java project, enter the item name, complete the right-click Project name->build path->configure Build Path Select Libraries->add External JARs., import all packages under testng copied from the server
    • Writing test Classes

EG1: Write 500 consecutive shots-Testcamera.java

Package com.ckt.test; Import Io.appium.java_client. Appiumdriver;import Io.appium.java_client.remote.MobileCapabilityType; Import Java.net.url;import Java.util.scanner;import java.util.concurrent.TimeUnit; Import Org.openqa.selenium.by;import Org.openqa.selenium.remote.desiredcapabilities;import Org.testng.annotations.aftertest;import Org.testng.annotations.beforetest;import org.testng.annotations.Test;    public class Testcamera {private Appiumdriver mappiumdriver;    Scanner Mscanner;  int num = 500; Perform the Test 500 times @BeforeTest public void SetUp () throws Exception {/** dynamic input execution times, requiring dynamic input to remove comments SYSTEM.OUT.PR        Intln ("Please enter the No. times you want to capture Picture:");        Mscanner = new Scanner (system.in);        num = Mscanner.nextint (); *//Set up desired capabilities and pass the Android app-activity and app-package to Appium Desiredcapabi        Lities capabilities = new Desiredcapabilities (); Capabilities.setcapability (mobilecapabIlitytype.browser_name, "Android");   Capabilities.setcapability (mobilecapabilitytype.version, "4.4.2");        SDK Platform version capabilities.setcapability (mobilecapabilitytype.platform, "Windows");   Capabilities.setcapability ("DeviceName", "0123456789ABCDEF");        ADB devices,devices name capabilities.setcapability ("PlatformName", "Android");         Capabilities.setcapability ("Newcommandtimeout", 120); Test package name, the source of the words, directly see Androidmanifest.xml//cmd console input uiautomatorviewer, start the camera, click on the Uiautomatorviewer device S Creenshot//The current app package name Capabilities.setcapability ("Apppackage", "Org.codeauro") is displayed in the Uiautomatorviewer right note detail        Ra.snapcam "); Execute adb shell Dumpsys activity|grep-a 5 "Running activities", find currently running activity//no grep for Windows, execute adb shell Dumpsys Activity activities > D:\activity.txt, search Running activities capabilities.setcapability ("Appactivity", "Com.androi         D.camera.cameraactivity "); Mappiumdriver = New Appiumdriver (New URL ("Http://127.0.0.1:4723/wd/hub"), capabilities);    Mappiumdriver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); } @Test public void capturing () throws Exception {for (int i = 0; i < num; i++) {//click Uiautom Atorviewer left camera button, right note detail in Resource-id will display the camera take button element Mappiumdriver.findelement (by.id ("Org.codeaurora.sna            Pcam:id/shutter_button ")). Click ();            Thread.Sleep (2000);        SYSTEM.OUT.PRINTLN ("Testcamera cases" + (I+1) + "successfully executed"); }} @AfterTest public void Quit () throws Exception {if (mappiumdriver! = null) {MAPPIUMDR        Iver.quit (); }    }}

EG2: Write into settings, swipe, exit-Testsettings.java

Package Com.ckt.test;import io.appium.java_client. Appiumdriver;import Io.appium.java_client.remote.MobileCapabilityType; Import Java.net.url;import Java.util.scanner;import java.util.concurrent.TimeUnit; Import Org.openqa.selenium.remote.desiredcapabilities;import Org.testng.annotations.aftertest;import Org.testng.annotations.beforetest;import org.testng.annotations.Test;    public class TestSettings {private Appiumdriver mappiumdriver;     Scanner Mscanner; @BeforeTest public void SetUp () throws Exception {desiredcapabilities capabilities = new Desiredcapabilities        ();        Capabilities.setcapability (Mobilecapabilitytype.browser_name, "Android");        Capabilities.setcapability (mobilecapabilitytype.version, "4.4.2");        Capabilities.setcapability (Mobilecapabilitytype.platform, "Windows");        Capabilities.setcapability ("DeviceName", "0123456789ABCDEF");        Capabilities.setcapability ("PlatformName", "Android"); Capabilities.setcapability ("ApppackagE "," com.android.settings ");        Capabilities.setcapability ("Appactivity", "com.android.settings.Settings");        Mappiumdriver = new Appiumdriver (New URL ("Http://127.0.0.1:4723/wd/hub"), capabilities);    Mappiumdriver.manage (). Timeouts (). implicitlywait (5, timeunit.seconds);        } @Test public void settings () throws Exception {thread.sleep (1000);        Mappiumdriver.swipe (661, 1105, 597, 50, 0);        Mappiumdriver.swipe (661, 1105, 597, 50, 0);        Mappiumdriver.swipe (661, 1105, 597, 50, 0);        Mappiumdriver.swipe (661, 1105, 597, 50, 0);        Mappiumdriver.swipe (599, 300, 661, 1105, 0);        Mappiumdriver.swipe (599, 300, 661, 1105, 0);        Mappiumdriver.swipe (599, 300, 661, 1105, 0);        Mappiumdriver.swipe (599, 300, 661, 1105, 0);    System.out.println ("testsettings successfully executed"); } @AfterTest public void Quit () throws Exception {if (mappiumdriver! = null) {Mappiumdriver.qui        T (); }    }}

eg3: Writing a batch execution file-Excetesetting.xml

<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" > <suite name= "TestSuite" >     <test name= " Testcamera ">        <classes>            <class name=" Com.ckt.test.TestCamera "/>        </classes>    </test>     <test name= "testsettings" >        <classes>            <class name= " Com.ckt.test.TestSettings "/>        </classes>    </test> </suite>

    • Demo download
: Http://pan.baidu.com/s/1pJFWvLh Download to import Eclipse,java project, import necessary package to run
TestNG Performing tests
    • Start Appium Server
Run the installed appium and click the Triangle button on the top right corner to start the service
    • Connect your phone to your PC
    • Perform a single test
Select a test class, such as Testcamera, right-click Run as->testng Test
    • Perform a batch test
Select a bulk profile, such as Excetesetting.xml, right-click Run as->testng TestNG

Execution is displayed in the console

Other stress test methods
    • For a lot of stress testing tools, I did it with a simple shell script before I contacted TestNG, such as:

Put the following into capture.sh, chmod +x capture.sh, add executable permissions

#!/bin/bashfor ((i=1;i<1000;i++)) do    adb shell input tap 550 1800    echo $i    sleep 2done

Connect the device and execute the./capture.sh

    • Defects
The above method uses the ADB shell input analog input event implementation, (550,1800) is the camera photo button on the screen coordinates. Different resolution requires re-modification
Reference Documents
Http://testng.org/doc/index.html

Module automated stress testing using the testng

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.