JavaAppium4.0[截屏]

Java结合TestNG单元测试框架,实现移动端截屏操作【源码】

/*
 * @FileName Test_CapturingScreenshots:
 * @author davieyang
 * @create 2018-11-22 13:48
 */
package testscript;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
public class Test_CapturingScreenshots {
    AndroidDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.android.calculator2");
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.android.calculator2.Calculator");
        driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @Test
    public void testExample() throws IOException{
        WebElement five=driver.findElement(By.name("5"));
        File screenShot=driver.getScreenshotAs(OutputType.FILE);
        File location=new File("screenshots");
        String screenShotName=location.getAbsolutePath()+File.separator+"testCalculator.png";
        FileUtils.copyFile(screenShot,new File(screenShotName));
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}
发布了207 篇原创文章 · 获赞 124 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/dawei_yang000000/article/details/104785258