appium-the screen shot after the test failed

Source: Internet
Author: User
Tags appium testng

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

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.