最近一直在研究appium,偶爾的機會發現斷言后獲取屏幕截圖。覺得這個方法不錯,分享給大家
這樣以后在遇到斷言,想截圖錯誤屏幕的時候,能夠用的上。
1.首先需要2個類,一個是測試類(TestDropList),另外一個是監聽類(ScreenshotListener)
public class ScreenshotListener extends TestListenerAdapter{
@Override
public void onTestFailure(ITestResult tr){
AppiumDriver driver = Screenshot.getDriver();
File location = new File("screenshots1"); //在默認的工作目錄下面創建一個名字叫screenshots1的文件夾,用來存放圖片的
String screenShotName = location.getAbsolutePath()+File.separator+tr.getMethod().getMethodName()+".png"; //
File screenShot = driver.getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(screenShot, new File(screenShotName));
}
catch(IOException e){
e.printStackTrace();
}
}
}
2. 創建測試類,並添加斷言
@Test //本測試用來用來測試選項是否是France
//測試過程中選擇的是India選擇,期望結果是France
//這樣測試完成后會把錯誤的信息記錄在圖片中
public void testExample() throws IOException{
WebElement spinner = driver.findElement(By.id("android:id/text1"));
spinner.click();
driver.scrollToExact("India");
WebElement optionindia = driver.findElement(By.name("India"));
optionindia.click();
WebElement result= optionindia;
//Check the calculated value on the edit box
assert result.getText().equals("France"):"Actual value is :"+result.getText()+" did not match with expected value: France";
}
這樣在完成測試后會將錯誤的斷言結果用圖片的形式保存
完整代碼如下:
package appTest;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
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.Listeners;
import org.testng.annotations.Test;
import com.lib.ctrip.ScreenshotListener;
@Listeners({ScreenshotListener.class})
public class Screenshot {
static AppiumDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "AndroidUI");
capabilities.setCapability("platformVersion", "1.0");
capabilities.setCapability("appPackage", "com.android.androidui");
capabilities.setCapability("appActivity", "com.android.androidui.MainActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample() throws IOException{
WebElement spinner = driver.findElement(By.id("android:id/text1"));
spinner.click();
driver.scrollToExact("India");
WebElement optionindia = driver.findElement(By.name("India"));
optionindia.click();
WebElement result= optionindia;
//Check the calculated value on the edit box
assert result.getText().equals("France"):"Actual value is :"+result.getText()+" did not match with expected value: France";
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
public static AppiumDriver getDriver(){
return driver;
}
}
package com.lib.ctrip;
import io.appium.java_client.AppiumDriver;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import appTest.Screenshot;
public class ScreenshotListener extends TestListenerAdapter{
@Override
public void onTestFailure(ITestResult tr){
AppiumDriver driver = Screenshot.getDriver();
File location = new File("screenshots1");
String screenShotName = location.getAbsolutePath()+File.separator+tr.getMethod().getMethodName()+".png";
File screenShot = driver.getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(screenShot, new File(screenShotName));
}
catch(IOException e){
e.printStackTrace();
}
}
}