我编写了一个try catch块来执行测试,如果测试失败,请在catch块中截屏。但是try块中的方法失败时,catch块不会执行,而@After方法将被执行。请参见下面的代码。
我无法理解我在哪里出问题了。
@Test
public void testScenarios(){
try {
test();
}catch (Exception e){
log.error(e.getLocalizedMessage(), e);
log.info("Capturing the screenshot for the failed test.");
takeScreenshot();
}
}
public void test() {
MyAccountPage myAccountPage = initElements(driver(), MyAccountPage.class);
myAccountPage.clickOrderHistoryAndDetails();
OrderHistoryPage orderHistoryPage = initElements(driver(), OrderHistoryPage.class);
orderHistoryPage.selectLatestOrder();
orderHistoryPage.verifyProduct(colour);
}
我的测试将在orderHistoryPage.verifyProduct(colour)中失败;由于找不到元素。
答案 0 :(得分:0)
“测试”方法不会引发任何异常。
<LinearLayout
android:id="@+id/kanji"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:background="@drawable/rounded_corner_2"
android:layout_margin="0dp">
<TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="漢"
android:textColor="@color/green"
android:gravity="center"
android:textSize="70sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/rounded_corner_2"
android:layout_margin="10dp"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="漢字"
android:textColor="@color/green"
android:textSize="20sp"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:text="漢字"
android:textColor="@android:color/darker_gray"
android:textSize="10sp"
android:gravity="left"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ProgressBar
android:layout_width="160dp"
android:layout_height="match_parent"
style="?android:attr/progressBarStyleHorizontal"
android:outlineSpotShadowColor="@color/green"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end"
android:text="100/140"
android:textColor="@android:color/darker_gray"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/goi"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:background="@drawable/rounded_corner_2"
android:layout_margin="0dp">
<TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="漢"
android:textColor="@color/green"
android:gravity="center"
android:textSize="70sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/rounded_corner_2"
android:layout_margin="10dp"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="漢字"
android:textColor="@color/green"
android:textSize="20sp"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:text="漢字"
android:textColor="@android:color/darker_gray"
android:textSize="10sp"
android:gravity="left"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ProgressBar
android:layout_width="160dp"
android:layout_height="match_parent"
style="?android:attr/progressBarStyleHorizontal"
android:outlineSpotShadowColor="@color/green"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end"
android:text="100/140"
android:textColor="@android:color/darker_gray"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
很显然,您可以选择自己喜欢的执行逻辑,这只是一个例子。
在您的情况下,方法“ verifyProduct”而不是捕获错误,应将其扔给调用方。
我不知道“ verifyProduct”的工作原理,但是可能是这样的:
//Exception is generic, you can throw your own Exception subclass
public void test() throws Exception{
if(OK){
//Good code
}else{
//Launch your exception. Es.
throw new Exception();
}
}
但这仅在verifyProduct如果找不到元素的情况下返回布尔值的情况下(Es。1表示成功,0表示错误)
有关更多信息,请检查this page
答案 1 :(得分:0)
我发现了错误并纠正了它。首先,该控件根本不会尝试。因此解决了该问题,并且每当try块失败并捕获相应的屏幕截图时,立即执行catch块。感谢您的帮助。