This article references: http://www.cnblogs.com/hexianl/p/4958556.html
Manage tests using the TESTNG test framework
1. Create a listener with the following code:
Importio.appium.java_client. Appiumdriver;ImportJava.io.File;Importjava.io.IOException;Importorg.apache.commons.io.FileUtils;ImportOrg.openqa.selenium.OutputType;ImportOrg.testng.ITestResult;ImportOrg.testng.TestListenerAdapter;ImportCom.globalegrow.test.Screenshot; Public classScreenshotlistenerextendstestlisteneradapter{@Override Public voidontestfailure (Itestresult tr) {Appiumdriver driver=Screenshot.getdriver (); File Location=NewFile ("Screenshots1"); String Screenshotname= Location.getabsolutepath () +file.separator+tr.getmethod (). Getmethodname () + ". png"; File screenshot=Driver.getscreenshotas (Outputtype.file); Try{fileutils.copyfile (screenshot,NewFile (screenshotname)); } Catch(IOException e) {e.printstacktrace (); } }}
2. Create the test class with the following code:
Importjava.net.MalformedURLException;ImportJava.net.URL;ImportJava.util.concurrent.TimeUnit;Importio.appium.java_client. Appiumdriver;ImportIo.appium.java_client.android.AndroidDriver;Importorg.openqa.selenium.By;Importorg.openqa.selenium.remote.DesiredCapabilities;ImportOrg.testng.annotations.AfterClass;ImportOrg.testng.annotations.BeforeClass;Importorg.testng.annotations.Test; Public classscreenshot {StaticAppiumdriver driver; @BeforeClass Public voidSetUp ()throwsmalformedurlexception{desiredcapabilities Capabilities=Newdesiredcapabilities (); Capabilities.setcapability ("PlatformName", "Android"); Capabilities.setcapability ("DeviceName", "192.168.35.102:5555"); Capabilities.setcapability ("Platformversion", "5.1"); Capabilities.setcapability ("Browsername", "Chrome"); Driver=NewAndroiddriver (NewURL ("Http://127.0.0.1:4723/wd/hub"), capabilities); Driver.manage (). Timeouts (). Implicitlywait (6, Timeunit.seconds); } @Test Public voidTestexample ()throwsinterruptedexception{Driver.get ("http://wap-sammydress.com.trunk.s1.egomsl.com/"); Driver.findelement (By.id ("Js_top_cate") . Click (); Thread.Sleep (3000); Driver.findelement (By.cssselector ("#nav > div > Ul > Li:nth-child (2) > P") . Click (); Thread.Sleep (5000); Driver.findelement (By.cssselector ("#header > Div.top.on > A.icon_tag.top_user.isnologin") . Click (); Thread.Sleep (2000); Driver.findelement (By.id ("Email"). SendKeys ("[Email protected]"); Driver.findelement (By.id ("PASSWORDSIGNL")). SendKeys ("xxxx")); Driver.findelement (By.id ("Js_signinbtn") . Click (); Thread.Sleep (2000); } @AfterClass Public voidTearDown () {driver.quit ();; } Public Staticappiumdriver Getdriver () {returndriver; } }
The Tesng XML file configuration is monitored as follows:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd"><Suitename= "Test"> <!--Configuring the TestNG listener - <Listeners> <ListenerClass-name= "Com.xxx.ScreenshotListener" /> </Listeners> <Testname= "Version"Preserve-order= "true"> <Classes> <classname= "Com.xxx.test.Screenshot"> <Methods> <includename= "Testexample" /> </Methods> </class> </Classes> </Test></Suite>
To run the test, the testexample fails after the following run:
appium-test fails after the screen