問題描述
Appium 中用於混合應用程序的多個屏幕截圖 (Multiple Screenshots in Appium for Hybrid Apps)
我一直在 Appium 中使用屏幕截圖[public void getScreenshot(){...}
] 測試失敗並啟動。我想為我的套件中的每個 findElement()
調用相同的方法。我嘗試這樣做:
創建自定義驅動程序為:
public class CustomDriver extends AppiumDriver{
public WebElement scrollToExact(String element) {
WebElement x = null;
return x;
}
public WebElement scrollTo(String element){
WebElement y = null;
return y;
}
@Override
public List<WebElement> findElements(By by) {
try {
getScreenshot();
}
catch (IOException iox){
System.out.println(iox.getMessage());
}
return by.findElements(this);
}
@Override
public WebElement findElement(By by) {
try {
getScreenshot();
}
catch(IOException iox){
System.out.println(iox.getMessage());
}
return by.findElement(this);
}
}
使用此驅動程序創建測試套件。
Q1。雖然這對我來說很好,但是當我將上下文切換到 Webview 時,我的驅動程序不會響應在該上下文中查找元素。我在這裡跟進有什麼問題嗎?[如果需要,會提供詳細信息,自己不確定。]
Q2. 在我的套件中為每個 findElement()
調用獲取屏幕截圖,我缺少什麼更簡單的方法嗎?
參考解法
方法 1:
In my experience screenshots work only in the NATIVE_APP context, so if you are in a WebView context you've got to switch to the native context and then switch back, something like:
@Override
public List<WebElement> findElements(By by) {
String originalContext = getContext();
if ("NATIVE_APP".equals(originalContext)) {
originalContext = null; // no need to switch
} else {
context("NATIVE_APP");
}
try {
getScreenshot(); // Before screenshot
return super.findElements(by);
} finally {
if (originalContext!=null) {
context(originaContext);
}
}
}
(by Naman、Pavel Vlasov)