Selenium WebDriver中的TestNG侦听器及示例
Java提供了不同的接口,使您可以修改TestNG行為。 這些接口在Selenium WebDriver中進(jìn)一步稱為TestNG偵聽器。 TestNG Listeners還允許您根據(jù)項目要求自定義測試日志或報告。
Selenium WebDriver中的TestNG偵聽器是偵聽某些事件并跟蹤測試執(zhí)行情況的模塊,同時在測試執(zhí)行的每個階段執(zhí)行某些操作。
這是一個TestNG教程,在這里我將通過示例幫助您實現(xiàn)不同的TestNG偵聽器,以便下次計劃使用TestNG和Selenium時可以熟練使用它們。
Selenium WebDriver中的TestNG偵聽器可以在兩個級別上實現(xiàn):
如果您不了解TestNG,建議您查看我們的TestNG教程以使用TestNG和Selenium運(yùn)行您的第一個自動化腳本。
Selenium WebDriver中的TestNG偵聽器類型
Selenium WebDriver中有許多TestNG偵聽器,其中一些經(jīng)常被測試社區(qū)使用,而有些則幾乎被遺忘。 在本TestNG教程中,我將通過示例演示最受歡迎的TestNG偵聽器,但在此之前,讓我在Selenium WebDriver中注冊各種TestNG偵聽器。
常用的TestNG偵聽器及示例
現(xiàn)在,在這個TestNG教程中,讓我們首先查看帶有示例的最受歡迎和使用最廣泛的TestNG偵聽器。
1. ITestListener
ITestListener是Selenium WebDriver中使用最廣泛的TestNG偵聽器。 通過普通的Java類為您提供易于實現(xiàn)的接口,該類將覆蓋ITestListener內(nèi)部聲明的每個方法。 通過在Selenium WebDriver中使用此TestNG偵聽器,可以通過向方法添加不同的事件來更改測試的默認(rèn)行為。 它還定義了一種新的日志記錄或報告方式。
以下是此接口提供的一些方法:
onStart:在執(zhí)行任何測試方法之前,將調(diào)用此方法。 這可以用來獲取運(yùn)行測試的目錄。
onFinish:執(zhí)行所有測試方法后,將調(diào)用此方法。 這可用于存儲所有已運(yùn)行測試的信息。
onTestStart:在調(diào)用任何測試方法之前,將先調(diào)用此方法。 這可以用來指示特定的測試方法已經(jīng)開始。
onTestSkipped:跳過每個測試方法時,將調(diào)用此方法。 這可以用來指示特定的測試方法已被跳過。
onTestSuccess:成功執(zhí)行任何測試方法時,將調(diào)用此方法。 這可以用來指示特定的測試方法已成功完成其執(zhí)行。
onTestFailure:當(dāng)任何測試方法失敗時,將調(diào)用此方法。 這可以用來指示特定的測試方法已失敗。 您可以創(chuàng)建一個截屏事件,以顯示測試失敗的地方。
onTestFailedButWithinSuccessPercentage:每次測試方法失敗但在提及的成功百分比之內(nèi),都會調(diào)用此方法。 為了實現(xiàn)此方法,我們在TestNG中使用兩個屬性作為測試注釋的參數(shù),即successPercentage和invocationCount。 成功百分比取成功百分比的值,調(diào)用計數(shù)表示特定測試方法將執(zhí)行的次數(shù)。
例如:@Test(successPercentage = 60,invocationCount = 5),在此注釋中,成功百分比為60%,調(diào)用計數(shù)為5,這意味著如果至少3次((?)* 100 = 60),則表示5次以上測試方法通過,則視為通過。
如果您不了解TestNG和Selenium,建議您檢查我們的TestNG教程以運(yùn)行第一個自動化腳本。
對于每個ITestListener方法,我們通常傳遞以下參數(shù):
- “ ITestResult”接口及其實例“ result”(描述測試結(jié)果)。
注意:如果要通過ITestResult跟蹤異常,則需要避免try / catch處理。
- “ ITestContext”接口及其實例“上下文”描述了包含給定測試運(yùn)行的所有信息的測試上下文。
現(xiàn)在,在此TestNG偵聽器教程中,我們將獲取一個基本的示例代碼,用于在類級別運(yùn)行測試。 日志將在控制臺上生成,它將幫助您了解哪些測試通過,失敗和跳過。
第一個類(ListentersBlog.java)將包含ITestListener接口實現(xiàn)的所有方法:
package TestNgListeners;import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult;public class ListenersBlog implements ITestListener {public void onTestStart(ITestResult result) {System.out.println("New Test Started" +result.getName());}public void onTestSuccess(ITestResult result) {System.out.println("Test Successfully Finished" +result.getName());}public void onTestFailure(ITestResult result) {System.out.println("Test Failed" +result.getName());}public void onTestSkipped(ITestResult result) {System.out.println("Test Skipped" +result.getName());}public void onTestFailedButWithinSuccessPercentage(ITestResult result) {System.out.println("Test Failed but within success percentage" +result.getName());}public void onStart(ITestContext context) {System.out.println("This is onStart method" +context.getOutputDirectory());}public void onFinish(ITestContext context) {System.out.println("This is onFinish method" +context.getPassedTests());System.out.println("This is onFinish method" +context.getFailedTests());} } 以下是包含測試方法(TestNGListenersTest.java)的代碼。 確保在類名上方添加一個Listeners注釋,以實現(xiàn)上述添加的方法。
語法: @Listeners(PackageName.ClassName.class)
控制臺輸出屏幕:
現(xiàn)在,假設(shè)您的項目中有多個類,那么將Selenium WebDriver中的TestNG偵聽器添加到每個類中可能會很麻煩。 在這種情況下,您可以創(chuàng)建一個測試套件并將Listeners標(biāo)記添加到套件(xml文件)中,而不是將Listeners添加到每個類中。
這是用于在套件級別運(yùn)行測試的示例代碼(testng.xml):
<suite name="TestNG Listeners Suite" parallel="false"><listeners><listener class-name="TestNgListeners.ListenersBlog"></listener></listeners><test name="Test"><classes><class name="TestNgListeners.TestNGListenersTest"></class></classes></test></suite>2. IAnnotationTransformer
IAnnotationTransformer是一個提供“轉(zhuǎn)換”方法的接口,TestNG會調(diào)用該方法來修改測試類中Test注釋方法的行為。
轉(zhuǎn)換方法提供各種參數(shù):
注意:至少一個參數(shù)為非null。
以下是將在套件級別執(zhí)行的示例代碼。 在此代碼中,我們在Test批注中使用了一個參數(shù)“ alwaysRun = true” ,該參數(shù)指示即使該方法所依賴的參數(shù)失敗,該測試方法也將始終運(yùn)行。 但是,我們將通過IAnnotationTransformer Listener轉(zhuǎn)換測試方法的這種行為,這將不允許執(zhí)行特定的測試方法。
偵聽器類文件:
package TestNgListeners;import java.lang.reflect.Constructor; import java.lang.reflect.Method;import org.testng.IAnnotationTransformer; import org.testng.annotations.ITestAnnotation;public class AnnotationTransformers implements IAnnotationTransformer {public boolean isTestRunning(ITestAnnotation ins) {if(ins.getAlwaysRun()){return true;}return false;}public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {if(isTestRunning(annotation)){annotation.setEnabled(false);} }}測試類文件:
package TestNgListeners;import org.testng.annotations.Listeners; import org.testng.annotations.Test;public class AnnotationTransformerTests {@Test(alwaysRun=true)public void test1(){System.out.println("This is my first test whose behaviour would get changed while executing"); }@Testpublic void test2(){System.out.println("This is my second test executing"); }}控制臺輸出屏幕:
3. IInvokedMethodListener
該界面允許您在執(zhí)行方法之前和之后執(zhí)行一些操作。 調(diào)用此偵聽器以進(jìn)行配置和測試方法。 Selenium WebDriver中的此TestNG偵聽器與ITestListerner和ISuiteListerner相同。 但是,您應(yīng)該記下&的區(qū)別,即在IInvokedMethodListener中,它在每個方法之前和之后進(jìn)行調(diào)用。
有兩種方法可以實現(xiàn):
beforeInvocation(): This method is invoked prior every method.
afterInvocation(): This method is invoked post every method.
這是此偵聽器的示例代碼,在類級別實現(xiàn)。
InvokedMethodListeners.java(includes listeners implemented methods)package TestNgListeners;import org.testng.IInvokedMethod; import org.testng.IInvokedMethodListener; import org.testng.ITestResult;public class InvokedMethodListeners implements IInvokedMethodListener {public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {System.out.println("Before Invocation of: " + method.getTestMethod().getMethodName() + "of Class:" + testResult.getTestClass()); }public void afterInvocation(IInvokedMethod method, ITestResult testResult) {System.out.println("After Invocation of: " + method.getTestMethod().getMethodName() + "of Class:" + testResult.getTestClass());} }文件名: InvokedMethodListenersTest.java(包括配置和測試方法)
package TestNgListeners;import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Listeners; import org.testng.annotations.Test;@Listeners(value=InvokedMethodListeners.class) public class InvokedMethodListenersTest { @Testpublic void test1(){System.out.println("My first test");}@Testpublic void test2(){System.out.println("My second test");}@BeforeClasspublic void setUp() {System.out.println("Before Class method");}@AfterClasspublic void cleanUp() {System.out.println("After Class method");} }控制臺輸出屏幕:
4. ISuiteListener
Selenium WebDriver中的此TestNG偵聽器以稱為ISuiteListener的套件級別實現(xiàn)。 它有2種方法:
onStart:在測試套件執(zhí)行之前調(diào)用此方法。
onFinish:在測試套件執(zhí)行后調(diào)用此方法。
此偵聽器基本上偵聽套件執(zhí)行之前和之后發(fā)生的事件。如果父套件還包含子套件,則在運(yùn)行父套件之前執(zhí)行子套件。
步驟1:使用普通的Java類實現(xiàn)ISuiteListener并添加未實現(xiàn)的方法。
類:SuiteListeners
package TestNgListeners;import org.testng.ISuite; import org.testng.ISuiteListener;public class SuiteListeners implements ISuiteListener {public void onStart(ISuite suite) { System.out.println("Suite executed onStart" + suite.getName());}public void onFinish(ISuite suite) {System.out.println("Suite executed onFinish" + suite.getName());} }步驟2:創(chuàng)建要添加到兩個不同子套件中的兩個測試類。
第1類:SuiteListenersTests1
package TestNgListeners;import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test;public class SuiteListenersTests1 {@BeforeSuitepublic void test1(){System.out.println("BeforeSuite method in Suite1"); }@Testpublic void test2(){System.out.println("Main Test method 1");}@AfterSuitepublic void test3(){System.out.println("AfterSuite method in Suite1"); }}第2類:SuiteListenersTests2
package TestNgListeners;import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test;public class SuiteListenersTests2 {@BeforeSuitepublic void test1(){System.out.println("BeforeSuite method in Suite2"); }@Testpublic void test2(){System.out.println("Main Test method 2");}@AfterSuitepublic void test3(){System.out.println("AfterSuite method in Suite2"); }}步驟3:將測試類添加到子套件中。
套件1:測試套件One.xml
<!--?xml version="1.0" encoding="UTF-8"?-->套件2:測試套件Two.xml
<!--?xml version="1.0" encoding="UTF-8"?-->步驟4:創(chuàng)建一個父套件xml文件,該文件將結(jié)合其他2個定義的套件以及l(fā)isteners類。
<!--?xml version="1.0" encoding="UTF-8"?-->控制臺輸出屏幕:
5. IReporter
Selenium WebDriver中的此TestNG偵聽器提供了一個界面,可幫助您自定義TestNG生成的測試報告。 它提供了generateReport方法,該方法將在所有套件執(zhí)行后被調(diào)用。 該方法還包含三個參數(shù):
以下是套件級別的IReporterer偵聽器示例。
文件名:ReporterListener.java
package TestNgListener;import java.util.List; import java.util.Map;import org.testng.IReporter; import org.testng.ISuite; import org.testng.ISuiteResult; import org.testng.ITestContext; import org.testng.xml.XmlSuite;public class ReporterListener implements IReporter {public void generateReport(List xmlSuites, List suites, String outputDirectory) {for(ISuite isuite : suites){Map<string, isuiteresult=""> suiteResults = isuite.getResults();String sn = isuite.getName();for(ISuiteResult obj : suiteResults.values()){ITestContext tc = obj.getTestContext();System.out.println("Passed Tests of" + sn + "=" + tc.getPassedTests().getAllResults().size());System.out.println("Failed Tests of" + sn + "=" + tc.getFailedTests().getAllResults().size());System.out.println("Skipped Tests of" + sn + "=" + tc.getSkippedTests().getAllResults().size());}}}} </string,>文件名:ReporterTest.java
package TestNgListener;import org.testng.SkipException; import org.testng.annotations.Listeners; import org.testng.annotations.Test;import junit.framework.Assert;public class ReporterTest {@Testpublic void FirstTest(){System.out.println("The First Test Method");Assert.assertTrue(true);}@Testpublic void SecondTest(){System.out.println("The Second Test Method");Assert.fail("Failing this test case");}@Testpublic void ThirdTest(){System.out.println("The Third Test Method");throw new SkipException("Test Skipped");}}控制臺輸出屏幕:
Selenium WebDriver中不太常用的TestNG偵聽器
在本節(jié)中,我將重點(diǎn)介紹那些TestNG偵聽器,這些偵聽器沒有上一節(jié)中討論的那樣知名。 我已經(jīng)避免了這些TestNG偵聽器及其示例的實際演示,因為它們很少使用。 但是,我將幫助您了解其目的。
6. IConfigurationListener
Selenium WebDriver中的此TestNG偵聽器僅在通過,失敗或跳過配置方法時才用于創(chuàng)建事件。
下面是此偵聽器提供的未實現(xiàn)的方法:
- onConfigurationSuccess:配置方法成功時將調(diào)用它。
- onConfigurationFailure:配置方法失敗時,將調(diào)用它。
- onConfigurationSkip:顧名思義,當(dāng)您的配置方法被跳過時,它將調(diào)用onConfigurationSkip方法。
7. IExecutionListener
此偵聽器用于跟蹤測試或套件運(yùn)行開始和結(jié)束的時間。 它提供了兩種方法:
onExecutionStart:在套件或測試開始運(yùn)行之前被調(diào)用。
onExecutionFinish:在套件或測試執(zhí)行后調(diào)用。
注意:此偵聽器不可能阻止執(zhí)行,而只能以某種方式創(chuàng)建事件。 此外,在配置TestNG時,您可以提供多個“ IExecution”偵聽器。
8. IHookable
此接口跳過測試方法的調(diào)用,并提供一個被調(diào)用的run方法,而不是找到的每個@Test方法。 然后,一旦調(diào)用IHookCallBack參數(shù)的callBack()方法,就會調(diào)用測試方法。
當(dāng)您希望對需要JAAS身份驗證的類執(zhí)行測試時,可以使用IHookable偵聽器。 這可以用來設(shè)置權(quán)限,即測試對象應(yīng)該針對誰運(yùn)行以及何時跳過測試方法。
9. IMethodInterceptor
→要返回IMethodInstance的列表,請執(zhí)行TestNG。
→對測試方法列表進(jìn)行排序。
TestNG將按照返回值中定義的相同順序執(zhí)行測試方法。
IMethodInterceptor接口僅包含一種實現(xiàn)“攔截”的方法,該方法返回修改后的測試方法列表。
示例:一種測試方法SampleTestOne是測試日志,因此我們將其分組在“ LogCheck”中。
現(xiàn)在,假設(shè)我們只想運(yùn)行LogCheck分組測試,而不是其他測試,因此,我們必須提供一個IMethodInterceptor偵聽器,該偵聽器可以消除其他測試并僅返回LogCheck分組測試。
10. IConfigurable
ICongurable偵聽器與IHookable偵聽器有些相似。 此接口跳過測試方法的調(diào)用,并提供一個run方法,而不是找到的每個配置方法都將被調(diào)用。 一旦調(diào)用IConfigureCallBack參數(shù)的callBack()方法,便會調(diào)用配置方法。
您在Selenium WebDriver中使用最多的哪些TestNG偵聽器?
我希望這個TestNG教程能夠幫助您了解哪種TestNG偵聽器最適合您的項目要求。 關(guān)于很少使用的TestNG偵聽器,如果您發(fā)現(xiàn)Selenium中有任何特定的TestNG偵聽器非常有用,請隨時在下面的評論部分中共享它們。 另外,如果您對本文有任何疑問,請告訴我。 我期待您的答復(fù)。 測試愉快!
翻譯自: https://www.javacodegeeks.com/2019/06/testng-listeners-in-selenium-webdriver-examples.html
總結(jié)
以上是生活随笔為你收集整理的Selenium WebDriver中的TestNG侦听器及示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 就能让电脑连接网络电脑如何设网络连接
- 下一篇: 电脑是怎么知道它自己已经联上网怎么知道电