TestNG Dinleyiciler Selenium
ฤฐki ana dinleyici var.
- WebDriver Dinleyicileri
- TestNG Dinleyiciler
Bu eฤitimde ลunlarฤฑ tartฤฑลacaฤฤฑz: Test Dinleyiciler.
Dinleyiciler Nedir? TestNG?
Dinleyici, varsayฤฑlanฤฑ deฤiลtiren arayรผz olarak tanฤฑmlanฤฑr TestNG'nin davranฤฑลฤฑ. Adฤฑndan da anlaลฤฑlacaฤฤฑ gibi, Dinleyiciler Selenium betiฤinde tanฤฑmlanan olayฤฑ "dinler" ve buna gรถre davranฤฑr. Selenium'da Dinleyiciler Arayรผzรผ uygulanarak kullanฤฑlฤฑr. รzelleลtirmeye izin verir TestNG raporlar veya gรผnlรผkler. Birรงok tรผrรผ var TestNG dinleyiciler mevcuttur.
Dinleyici Tรผrleri TestNG
deฤiลtirmenize izin veren birรงok dinleyici tรผrรผ vardฤฑr. TestNGdavranฤฑลฤฑ.
Aลaฤฤฑda birkaรงฤฑ var TestNG dinleyiciler:
- IAnnotationTransformer,
- IAnnotationTransformer2 ,
- Yapฤฑlandฤฑrฤฑlabilir,
- IConfigurationListener ,
- IExecutionListener,
- Kancalanabilir,
- IInvokedMethodListener ,
- IInvokedMethodListener2 ,
- IMethodInterceptor ,
- IR Muhabir,
- ISuiteListener,
- ITestListener .
Yukarฤฑdaki Arayรผz denir TestNG Dinleyiciler. Bu arayรผzler Selenium'da gรผnlรผkler oluลturmak veya รถzelleลtirmek iรงin kullanฤฑlฤฑr TestNG raporlar.
Bu eฤitimde ITestListener'ฤฑ uygulayacaฤฤฑz.
ITestListener'ฤฑn aลaฤฤฑdaki yรถntemleri vardฤฑr
- OnStart- Herhangi bir Test baลladฤฑฤฤฑnda OnStart yรถntemi รงaฤrฤฑlฤฑr.
- onTestSuccess- Herhangi bir Testin baลarฤฑsฤฑ รผzerine onTestSuccess yรถntemi รงaฤrฤฑlฤฑr.
- onTestFailure- Herhangi bir Testin baลarฤฑsฤฑz olmasฤฑ durumunda onTestFailure yรถntemi รงaฤrฤฑlฤฑr.
- onTestSkipped- onTestSkipped yรถntemi herhangi bir Testin atlanmasฤฑ durumunda รงaฤrฤฑlฤฑr.
- onTestFailedButinSuccessPercentage- Test her baลarฤฑsฤฑz olduฤunda ancak baลarฤฑ yรผzdesi dahilinde olduฤunda yรถntem รงaฤrฤฑlฤฑr.
- onFinish- Tรผm Testler yรผrรผtรผldรผkten sonra onFinish yรถntemi รงaฤrฤฑlฤฑr.
Test Senaryosu
Bu test senaryosunda Giriล iลlemini otomatikleลtirip 'ItestListener'ฤฑ uygulayacaฤฤฑz.
- Baลlatฤฑn Firefox ve siteyi aรง https://demo.guru99.com/V4/
- Uygulamaya giriล yapฤฑn.
Oluลturma adฤฑmlarฤฑ TestNG dinleyici
Yukarฤฑdaki test senaryosu iรงin Listener'ฤฑ uygulayacaฤฤฑz.
) 1 Adฤฑm 'ITestListener'ฤฑ uygulayan โListenerTestโ sฤฑnฤฑfฤฑnฤฑ oluลturun. Fareyi kฤฑrmฤฑzฤฑ รงizgili metnin รผzerine getirin ve Eclipse Aลaฤฤฑdaki ekranda gรถsterildiฤi gibi size 2 hฤฑzlฤฑ dรผzeltme รถnerecektir:
โUygulanmayan yรถntemleri ekleโ seรงeneฤine tฤฑklamanฤฑz yeterlidir. Koda birden fazla uygulanmamฤฑล yรถntem (gรถvde olmadan) eklenir. Aลaฤฤฑyฤฑ kontrol edin-
package Listener_Demo;
import org.testng.ITestContext ;
import org.testng.ITestListener ;
import org.testng.ITestResult ;
public class ListenerTest implements ITestListener
{
@Override
public void onFinish(ITestContext arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStart(ITestContext arg0) {
// TODO Auto-generated method stub
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onTestFailure(ITestResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onTestSkipped(ITestResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onTestStart(ITestResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onTestSuccess(ITestResult arg0) {
// TODO Auto-generated method stub
}
}
'ListenerTest' sฤฑnฤฑfฤฑnฤฑ deฤiลtirelim. รzellikle, aลaฤฤฑdaki yรถntemleri deฤiลtireceฤiz-
onTestFailure, onTestSkipped, onTestStart, onTestSuccess, etc.
Deฤiลiklik basittir. Sadece Testin adฤฑnฤฑ yazdฤฑrฤฑyoruz.
Gรผnlรผkler konsolda oluลturulur. Kullanฤฑcฤฑnฤฑn hangi testin baลarฤฑlฤฑ, baลarฤฑsฤฑz ve atlanmฤฑล durumu olduฤunu anlamasฤฑ kolaydฤฑr.
Deฤiลiklikten sonra kod ลรถyle gรถrรผnรผr:
package Listener_Demo;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class ListenerTest implements ITestListener
{
@Override
public void onFinish(ITestContext Result)
{
}
@Override
public void onStart(ITestContext Result)
{
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult Result)
{
}
// When Test case get failed, this method is called.
@Override
public void onTestFailure(ITestResult Result)
{
System.out.println("The name of the testcase failed is :"+Result.getName());
}
// When Test case get Skipped, this method is called.
@Override
public void onTestSkipped(ITestResult Result)
{
System.out.println("The name of the testcase Skipped is :"+Result.getName());
}
// When Test case get Started, this method is called.
@Override
public void onTestStart(ITestResult Result)
{
System.out.println(Result.getName()+" test case started");
}
// When Test case get passed, this method is called.
@Override
public void onTestSuccess(ITestResult Result)
{
System.out.println("The name of the testcase passed is :"+Result.getName());
}
}
) 2 Adฤฑm Oturum aรงma iลlemi otomasyonu iรงin baลka bir "TestCases" sฤฑnฤฑfฤฑ oluลturun. Selenium otomatik olarak oturum aรงmak iรงin bu 'TestCases'i yรผrรผtecektir.
package Listener_Demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Listeners;
Import org.testng.annotations.Test;
public class TestCases {
WebDriver driver= new FirefoxDriver();
// Test to pass as to verify listeners .
@Test
public void Login()
{
driver.get("https://demo.guru99.com/V4/");
driver.findElement(By.name("uid")).sendKeys("mngr34926");
driver.findElement(By.name("password")).sendKeys("amUpenu");
driver.findElement(By.name("btnLogin")).click();
}
// Forcefully failed this test as to verify listener.
@Test
public void TestToFail()
{
System.out.println("This method to test fail");
Assert.assertTrue(false);
}
}
) 3 Adฤฑm Daha sonra bu dinleyiciyi normal proje sฤฑnฤฑfฤฑmฤฑz olan โTestCasesโe uygulayฤฑn. Sฤฑnฤฑfa ve arayรผze baฤlanmanฤฑn iki farklฤฑ yolu vardฤฑr.
ฤฐlk yol, aลaฤฤฑda gรถsterildiฤi gibi Dinleyici ek aรงฤฑklamasฤฑnฤฑ (@Listeners) kullanmaktฤฑr:
@Listeners(Listener_Demo.ListenerTest.class)
Bunu aลaฤฤฑda gรถsterildiฤi gibi โTestCasesโ sฤฑnฤฑfฤฑnda kullanฤฑyoruz.
Sonunda "TestCases" sฤฑnฤฑfฤฑ, Dinleyici ek aรงฤฑklamasฤฑnฤฑ kullandฤฑktan sonra ลuna benzer:
package Listener_Demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(Listener_Demo.ListenerTest.class)
public class TestCases {
WebDriver driver= new FirefoxDriver();
//Test to pass as to verify listeners.
@Test
public void Login()
{
driver.get("https://demo.guru99.com/V4/");
driver.findElement(By.name("uid")).sendKeys("mngr34926");
driver.findElement(By.name("password")).sendKeys("amUpenu");
driver.findElement(By.id("")).click();
}
//Forcefully failed this test as verify listener.
@Test
public void TestToFail()
{
System.out.println("This method to test fail");
Assert.assertTrue(false);
}
}
Proje yapฤฑsฤฑ ลรถyle gรถrรผnรผr:
Adฤฑm 4): โTestCasesโ sฤฑnฤฑfฤฑnฤฑ yรผrรผtรผn. โListenerTestโ sฤฑnฤฑfฤฑndaki yรถntemler, @Test olarak aรงฤฑklamalฤฑ yรถntemlerin davranฤฑลฤฑna gรถre otomatik olarak รงaฤrฤฑlฤฑr.
Adฤฑm 5): Gรผnlรผklerin konsolda gรถrรผntรผlendiฤi รฤฑkฤฑลฤฑ doฤrulayฤฑn.
'TestCase'lerin รงฤฑktฤฑsฤฑ ลu ลekilde gรถrรผnecektir:
[TestNG] Running: C:\Users\gauravn\AppData\Local\Temp\testng-eclipse--1058076918\testng-customsuite.xml Login Test Case started The name of the testcase passed is:Login TestToFail test case started This method to test fail The name of the testcase failed is:TestToFail PASSED: Login FAILED: TestToFail java.lang.AssertionError: expected [true] but found [false]
Birden fazla sฤฑnฤฑf iรงin Dinleyici kullanฤฑmฤฑ.
Projenin birden fazla sฤฑnฤฑfฤฑ varsa, her birine Dinleyici eklemek hantal ve hataya aรงฤฑk olabilir.
Bu gibi durumlarda testng.xml oluลturup XML'e dinleyici etiketi ekleyebiliriz.
Bu dinleyici, sahip olduฤunuz sฤฑnฤฑf sayฤฑsฤฑna bakฤฑlmaksฤฑzฤฑn test paketinin tamamฤฑna uygulanฤฑr. Bu XML dosyasฤฑnฤฑ รงalฤฑลtฤฑrdฤฑฤฤฑnฤฑzda dinleyiciler bahsedilen tรผm sฤฑnฤฑflar รผzerinde รงalฤฑลacaktฤฑr. Ayrฤฑca istediฤiniz sayฤฑda dinleyici sฤฑnฤฑfฤฑnฤฑ da bildirebilirsiniz.
รZET
Dinleyicilerin gรผnlรผk oluลturmasฤฑ veya รถzelleลtirmesi gerekir TestNG raporlar Selenium Web sรผrรผcรผsรผ.
- Pek รงok dinleyici tรผrรผ vardฤฑr ve gereksinimlere gรถre kullanฤฑlabilir.
- Dinleyiciler Selenium web sรผrรผcรผsรผ komut dosyasฤฑnda kullanฤฑlan arayรผzlerdir
- Dinleyicinin kullanฤฑmฤฑnฤฑ gรถsterdi Selenium
- Birden fazla sฤฑnฤฑf iรงin Dinleyiciler uygulandฤฑ







