UI自動化テストの実行中、検査が失敗した場合、印刷されたログが問題を効果的に見つけるのに役立たないことがよくあります。その時点で失敗シーンを再現できなかった瞬間のスクリーンショットが必要で、エラーの原因のトラブルシューティングが必要です。
この要件に基づいて、Seleniumのスクリーンショット関数を使用できます。
実装コードは次のとおりです。
java.io.file; Import java.io.ioexception; Import org.apache.commons.io.fileutils; import org.apache.commons.lang3.time.dateutils; import org.openqa.selenium.outputtype; Import org.openqa.selenium.takescreenshot; Import; org.openqa.selenium.webdriver; Import org.openqa.selenium.interactions.actions; public static void takecreeshot(string screenpath、webdriver chrome){try {// getScreenshotas()のパラメーターとしてoutputtype.fileを指定します。ファイルscrfile =((takesscreenshot)chrome).getScreenshotas(outputType.file); //キーコード、スクリーンショットを実行すると、デフォルトでは、スクリーンショットをTEMPディレクトリFileUtils.CopyFile(scrfile、new file(screenpath))に保存します。 // fileutilsツールクラスのコピーファイル()メソッドを使用して、getScreenshotas()によって返されたファイルオブジェクトを保存します。 } catch(ioException e){system.out.println( "スクリーンショットエラー:" + screenpath); system.out.println( "このエラーは、スクリーンショットで表示できます:"+screenpath); } catch(Exception e){// todo:handle例外}} public static void takecreenshot(webdriver chrome、string imgname){string screenname = imgname+dateutils.millis_per_day+"。Jpg"; string filestring = "d:// selenium // schoolpalerp_qtp // image"; if(!(new file(filestring).isdirectory())){//ディレクトリがnew file(filestring).mkdir(); //存在しない場合は、新しいディレクトリを作成}ファイルdir = new file(fileString); if(!dir.exists())dir.mkdirs();文字列screenpath = dir.getabsolutepath() + "//" + screenname; TakeCreeshot(ScreenPath、Chrome); }SeleniumのUIオートメーションテストのスクリーンショット関数の上記の例コードは、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。