きゅうりのシナリオがあり、ステップではassertEquals
. 結果レポートにスタック トレースが表示されますが、これはエンド ユーザー フレンドリーではありません。どうすれば抑えることができますか
Scenario: Add two numbers
Given I have two inputs "3" and "2"
When I add them
Then the output should be "15"
きゅうりのシナリオがあり、ステップではassertEquals
. 結果レポートにスタック トレースが表示されますが、これはエンド ユーザー フレンドリーではありません。どうすれば抑えることができますか
Scenario: Add two numbers
Given I have two inputs "3" and "2"
When I add them
Then the output should be "15"
Cucumber-Selenium-Java プロジェクトでも同じ問題に直面していました。キュウリのレポートでは、約 40 行のスタック トレースが生成されていました。このため、レポートのルック アンド フィールに影響を与えていました。そして、エンドユーザー/クライアントはそれについてほとんど心配していませんでした. 彼/彼女は、このスタックトレースの実際の使用法を実際に理解できなかったからです。そこで、以下のアイデア/アプローチを思いつきました。少しトリッキーですが、価値があります。
開始前の注意事項:
すべての例外を処理するための共通メソッドを作成し、このメソッドを必要なすべてのクラスで再利用する必要があります。例: メソッドに「processException」という名前を付け、「ReusableMethod」クラスに配置しました。
すべてのテストクラスがこのパッケージに配置されているため、以下のメソッド (8 行目) でパッケージ名「ページ」を使用していることに注意してください。あなたの場合、必要に応じてパッケージ名を更新する必要があります。また、NoSuchElementException と AssertionError の 2 つの例外のみのカスタム ケースを作成しました。必要に応じて、さらにケースを作成する必要がある場合があります。
public void processException(Throwable e) throws Exception {
StackTraceElement[] arr = e.getStackTrace();
String className = "";
String methodName = "";
int lineNumber = 0;
for (int i = 0; i < arr.length; i++) {
String localClassName = arr[i].getClassName();
if (localClassName.startsWith("page")) {
className = localClassName;
methodName = arr[i].getMethodName();
lineNumber = arr[i].getLineNumber();
break;
}
}
String cause = "";
try {
cause = e.getCause().toString();
} catch (NullPointerException e1) {
cause = e.getMessage();
}
StackTraceElement st = new StackTraceElement(className, methodName, "Line", lineNumber);
StackTraceElement[] sArr = { st };
if (e.getClass().getName().contains("NoSuchElementException")) {
String processedCause = cause.substring(cause.indexOf("Unable to locate"), cause.indexOf("(Session info: "))
.replaceAll("\\n", "");
Exception ex = new Exception("org.openqa.selenium.NoSuchElementException: " + processedCause);
ex.setStackTrace(sArr);
throw ex;
} else if (e.getClass().getName().contains("AssertionError")) {
AssertionError ae = new AssertionError(cause);
ae.setStackTrace(sArr);
throw ae;
} else {
Exception ex = new Exception(e.getClass() + ": " + cause);
ex.setStackTrace(sArr);
throw ex;
}
}
以下は、テストクラスメソッドで上記のメソッドの使用法を紹介するサンプルメソッドです。私の場合は「reuseMethod」であるクラス参照を使用して、上記で作成したメソッドを呼び出しています。そして、キャッチされた Throwable 参照 "e" を上記のメソッドの catch ブロックに渡します。
public void user_Navigates_To_Home_Page() throws Exception {
try {
//Certain lines of code as per your tests
//element.click();
} catch (Throwable e) {
reuseMethod.processException(e);
}
}
NoSuchElementException の実装のスクリーンショットを次に示します。
このアプローチを実装する前に:
このアプローチを実装した後: