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();
}
}