Runing test cases in all browser one after another - selenium-webdriver

I want to run selenium webdriver test cases in all multiple browser but not in parallel.Is it possible to run test cases in all multiple browser without using xml and selenium grid.Can we do it by using annotation and java classes.I wanted that my test cases should execute in firefox first and after completion of execution in firefox it should start execution in chrome and so on.
I have tried this code to execute my test cases from one browser to another but not simultaneously.but it throw exceptions.
ManyBrowsers.java
import java.io.File;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ManyBrowsers implements MethodRule {
public static WebDriver driver;
#Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
#Override
public void evaluate() throws Throwable {
//RUN FIREFOX
driver = new FirefoxDriver();
base.evaluate();
driver.quit();
//RUN CHROME
File f = new File("D:\\SeleniumTestCases\\Selenium_Drivers\\chromedriver" ) ;//PATH to CHROME DRIVER
System.setProperty("webdriver.chrome.driver", f.getAbsolutePath());
driver = new ChromeDriver();
base.evaluate();
driver.quit();
}
};
}
}
Example Test
import java.util.concurrent.TimeUnit;
import org.junit.Rule;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;
import com.thoughtworks.selenium.webdriven.commands.WaitForPageToLoad;
public class TestClass1 {
protected static WebDriver driver;
protected static String result;
#Rule
public ManyBrowsers browsers = new ManyBrowsers();
#BeforeClass
public static void setup() {
ManyBrowsers.driver.navigate().to("http://www.floraindia.com");
}
#Test
void Testcase1() {
System.out.println("Testcase1");
driver.findElement(By.id("kwsch")).sendKeys("Red");
driver.findElement(By.xpath("//input[#src='image/go.gif']")).click();
}
#Test
public void testGmail() throws Exception {
driver.get("http://www.gmail.com");
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("testemail");
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("123456");
driver.findElement(By.id("signIn")).click();
}
#AfterClass
public static void teardown() {
driver.close();
driver.quit();
}

Create different classes for the different tests in different browsers. For example:
First class:
public class FirefoxTest
{
WebDriver driver;
#BeforeClass
public void beforeClass() {
this.driver = new FirefoxDriver();
}
#Test
public void test() {
this.driver.get("http://google.com");
}
#AfterClass
public void afterClass() {
this.driver.quit();
}
}
Second class:
public class ChromeTest
{
WebDriver driver;
#BeforeClass
public void beforeClass() {
this.driver = new ChromeDriver();
}
#Test
public void test() {
this.driver.get("http://google.com");
}
#AfterClass
public void afterClass() {
this.driver.quit();
}
}
If you run your project as a TestNG Test, it will create your xml file programatically which will include all of the classes, so in this case you don't have to create it manually.

You get NullPointerException because method evaluate from class ManyBrowsers is invoked only before #Test methods.
You should use class ManyBrowsers only in methods with #Test annotation.

Related

BaseTest class doesn't initialize the Webdriver instance

I have gone through similar type Q&As, but couldn't figure out the issue in my code.
This is my BaseTest class
`
package com.supportiveTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.After;
import io.cucumber.java.Before;
public class BaseTests {
public static WebDriver driver;
#Before
public void setUpDriver() {
System.setProperty("webdriver.chrome.driver", "src/main/resources/Drivers/chromedriver.exe");
driver = new ChromeDriver();
}
#After
public void quitDriver() {
this.driver.quit();
System.out.println("done AfterTest");
}
}
`
This is my stepDefinition class (LoginPageSteps)
`
package com.stepDefinitions;
import com.pageObjects.LoginPage;
import com.supportiveTests.BaseTests;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.testng.annotations.Test;
#Test
public class LoginPageSteps extends BaseTests {
LoginPage obj_login;
#Given("User is on Google Home page")
public void user_is_on_google_home_page() {
BaseTests.driver.navigate().to("https://www.google.com/");
}
#Given("User is navigated to SwagLabs Login page")
public void user_is_navigated_to_swag_labs_login_page() {
BaseTests.driver.navigate().to("https://www.saucedemo.com/");
}
#When("^User enters valid (.*) and (.*)$")
public void user_enters_valid_standard_user_and_secret_sauce(String username, String password) {
obj_login = new LoginPage(driver);
obj_login.enterUserName(username);
obj_login.enterPassword(password);
}
#When("clicks on LOGIN button")
public void clicks_on_login_button() {
obj_login.clickOnLogin();
}
#Then("User is navigated to SwagLAbs Home page")
public void user_is_navigated_to_swag_l_abs_home_page() throws InterruptedException {
BaseTests.driver.getCurrentUrl().contains("https://www.saucedemo.com/inventory.html");
Thread.sleep(2000);
BaseTests.driver.quit();
}
}
`
This is my TestRunner class
`
package com.supportiveTests;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(features = "src/test/Features",
glue = {"com/stepDefinitions"},
monochrome = true,
plugin = {
"pretty", "html:target/HTMLReports/report.html",
"json:target/JSONReports/report.json",
"junit:target/JUnitReports/report.xml"
}
)
public class TestRunner {
}
`
This is my pageObject class (LoginPage)
`
package com.pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
By txt_username = By.xpath("//input[#id='user-name']");
By txt_password = By.xpath("//input[#id='password']");
By btn_login = By.xpath("//input[#id='login-button']");
public void enterUserName(String username){
driver.findElement(txt_username).sendKeys(username);
}
public void enterPassword(String password){
driver.findElement(txt_password).sendKeys(password);
}
public void clickOnLogin(){
driver.findElement(btn_login).click();
}
}
When the above TestRunner class is executed, I get this error
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.navigate()" because "com.supportiveTests.BaseTests.driver" is null
at com.stepDefinitions.LoginPageSteps.user_is_on_google_home_page(LoginPageSteps.java:16)
I went through the similar Q&As, where the solutions for driver initialization were provided, but couldn't figure out the issue. I am at basic level of Selenium testing framework, so appreciate any guidance on fixing this.

Method under #test not works, untill placed with the url

#Test method is not working, but works when placed after flipkart url section.
i think that if i place the click function with url then it will be a wrong approach, code should be independent.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Testdemo {
WebDriver driver;
#BeforeTest
public void test2() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","\\driver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
}
#Test
public void test3() throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.className("_29YdH8")).click();
}
}
java.lang.NullPointerException
at Test.Flipkart.data(Flipkart.java:40)
Problem is that even though you defined a class level variable for driver as below
public class Testdemo {
WebDriver driver;
You never initiated , because in #BeforeTest you defined it as a local variable again
So it throws null point excepption for the driver, To solve this please remove webdriver , so the class level variable will be used
**WebDriver** driver=new ChromeDriver();
Corrected code
#BeforeTest
public void test2() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","\\driver\\chromedriver.exe");
driver=new ChromeDriver();

Selenium-webdriver is giving me blank screen of google chrome and a Java null pointer exception. Can anyone suggest?

TestBase which contains the intialization method which is creating problem :
package TestBase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class testBasesetup {
public static Properties prop;
public static WebDriver driver;
public static testBasesetup testBaseObj;
public static EventFiringWebDriver e_driver;
public testBasesetup() throws Exception
{
try {
prop = new Properties();
File src = new File("C:\\Users\\LENOVO\\eclipse-workspace\\Demon\\src\\main\\java\\Config\\config.properties");
FileInputStream ip = new FileInputStream(src) ;
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization() throws Exception
{
String browsername = prop.getProperty("browser");
if(browsername.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "F:\\Eclipse\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
}
else if(browsername.equals("firefox"))
{
System.setProperty("webdriver.gecko.driver", "F:\\Eclipse\\browser\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
String URLtoTest = prop.getProperty("URL");
Thread.sleep(1000);
}
}
Test Class which I am running and this is creating issue. I guess there is some issue with driver variable but don't know the error:
package PageTest;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Pages.LoginPage;
import TestBase.testBasesetup;
public class LoginPageTest extends testBasesetup{
LoginPage LoginPageObj;
testBasesetup testBaseObj;
public LoginPageTest() throws Exception
{
super();
}
#BeforeMethod
public void setup() throws Exception
{
initialization();
LoginPageObj = new LoginPage();
Thread.sleep(2000);
}
#Test()
public void LoginCheck() throws Exception
{
LoginPageObj.login("first.customer#kkr.com","Potatok");
}
#AfterMethod
public void tearDown()
{
driver.quit();
}
}
Image of error:
Code
Your static instance of WebDriver remain uninitialized. You need to initialize static instance instead of local driver instance in initialization() method. Like Below:
public class testBasesetup {
public static Properties prop;
public static WebDriver driver;
public static testBasesetup testBaseObj;
public static EventFiringWebDriver e_driver;
public testBasesetup() throws Exception
{
try {
prop = new Properties();
File src = new File("C:\\Users\\LENOVO\\eclipse-workspace\\Demon\\src\\main\\java\\Config\\config.properties");
FileInputStream ip = new FileInputStream(src) ;
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization() throws Exception
{
String browsername = prop.getProperty("browser");
if(browsername.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "F:\\Eclipse\\chromedriver.exe");
**driver = new ChromeDriver();**
}
else if(browsername.equals("firefox"))
{
System.setProperty("webdriver.gecko.driver", "F:\\Eclipse\\browser\\geckodriver.exe");
**driver = new FirefoxDriver();**
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
String URLtoTest = prop.getProperty("URL");
Thread.sleep(1000);
}
}

how to call function/method form one class to another class in selenium webdriver using java

I want to call login and search method from two different class into the main class. I have written the two diff classes for login and search but when I am calling both method in class containg main method .it gives me an error as below>>
Exception in thread "main" java.lang.NullPointerException
at Test_package_Ted_baker.search.search_1(search.java:14)
at Test_package_Ted_baker.SHopping.main(SHopping.java:16)
Can someone please help me to find out solution about how to call methods from one class to another class. My code is as below>>
Main class
package Test_package_Ted_baker;
import org.openqa.selenium.WebDriver;
public class SHopping {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
login_Ted_Baker obj=new login_Ted_Baker();
obj.login_to_Ted_Baker("ss3777010#gmail.com", "google#123");
System.out.println("Login sucessfully");
search obj1=new search();
obj1.search_1();
obj.user_logout();
System.out.println("User log out sucessfully");
}
}
Login class
package Test_package_Ted_baker;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class login_Ted_Baker {
WebDriver driver;
public static void main (String [] args) throws InterruptedException
{
login_Ted_Baker obj=new login_Ted_Baker();
obj.login_to_Ted_Baker("ss3777010#gmail.com", "google#123");
System.out.println("Login Sucessfully");
obj.user_logout();
}
public void usename( String Username)
{
WebElement Username_TB=driver.findElement(By.name("j_username"));
Username_TB.sendKeys(Username);
}
public void password(String Password)
{
WebElement Password_TB=driver.findElement(By.name("j_password"));
Password_TB.sendKeys(Password);
}
public void submit()
{
driver.findElement(By.xpath(".//*[#id='command']/div/input ")).click();
}
public void login_Ted_Baker(WebDriver driver){
this.driver = driver;
}
public void user_logout() throws InterruptedException
{
Thread.sleep(1000);
WebElement Sign_in_link=driver.findElement(By.xpath(".//*[#id='page']/header/div/nav[1]/ul/li[3]/div/a"));
WebElement Sign_out_button=driver.findElement(By.xpath(".//*[#id='account']/ul/li[2]/a"));
Thread.sleep(1000);
Actions Act1=new Actions(driver);
Act1.moveToElement(Sign_in_link).click();
Act1.moveToElement(Sign_out_button).click().build().perform();
System.out.println("Logout sucessfully");
}
public void login_to_Ted_Baker(String Username,String Password) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver_win32\\chromedriver.exe");
//System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
Thread.sleep(2000);
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
try{
driver.get("http://www.tedbaker.com/");
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")));
if(driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).isDisplayed())
{
driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).click();
}
driver.findElement(By.xpath(".//*[#id='page']/header/div/nav[1]/ul/li[3]/div/a")).click();
Thread.sleep(2000);
this.usename(Username);
this.password(Password);
this.submit();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
searching class
package Test_package_Ted_baker;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class search {
WebDriver driver;
public void search_1() throws InterruptedException
{
WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));
Search_Item.click();
WebElement Seacrh_item_message=driver.findElement(By.id("search"));
WebElement Search_textbox=driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[1]/input"));
Search_textbox.sendKeys("Watches");
driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[2]/input")).click();
WebElement SearchResult_pagemessage=driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/h1"));
driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/div/div/span[1]")).click();
WebElement Item_Tobuy = driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[2]/div[1]/article/div[2]/header/div/h4/a"));
JavascriptExecutor jse1= (JavascriptExecutor)driver;
jse1.executeScript("window.scrollBy(0,400)", "");
Item_Tobuy.click();
driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/ol/li[2]/span/span/select")).click();
Select dropdown1=new Select(driver.findElement(By.id("qty")));
dropdown1.selectByVisibleText("1");
driver.findElement(By.id("qty")).click();
// driver.findElement(By.xpath("*[#classname='button add_to_cart major full_mobile colour_dark']/div[2]/div[1]/div/input[1]")).click();
// driver.findElement(By.xpath("//*[#id='add_to_cart_form']/div[2]/div[2]/a/span[1]/span[1]")).click();
driver.findElement(By.xpath("//input[contains(#class,'add_to_cart')]")).click();
Thread.sleep(1000);
Actions act=new Actions(driver);
WebElement My_cart_button=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[3]/a/span[2]"));
//WebElement My_cart_button=driver.findElement(By.xpath("//input[contains(#class,'My bag')]"));
WebElement View_bag_checkout_button=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[3]/div/ul/li/a"));
act.moveToElement(My_cart_button).moveToElement(View_bag_checkout_button).click().build().perform();
driver.findElement(By.xpath("html/body/div[2]/div/div/div/section/table/tbody/tr/td[2]/form/ul/li[2]/a")).click();
String Cart_empty_mesg=driver.findElement(By.xpath("html/body/div[1]/div/div/div/header/h1")).getText();
System.out.println(Cart_empty_mesg);
String Actual_cart_empty_msg="Your shopping bag is empty";
if(Cart_empty_mesg==Actual_cart_empty_msg)
{
System.out.println("Cart is empty, you can add product cart");
}
}
}
This might sound harsh, but first of all you should read about
Null Pointer Exception from this link.
How to debug from a StackTrace from this link.
Why using absolute xpath, or xpaths locators in general is a bad strategy from here.
From your stack trace,
Exception in thread "main" java.lang.NullPointerException at
Test_package_Ted_baker.search.search_1(search.java:14) at
Test_package_Ted_baker.SHopping.main(SHopping.java:16)
I see that your code in Search class
WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));
is not correct. I suspect your locator is not correct for this one. The element can easily be identified using an id attribute, as
driver.findElement(By.id("search"));
or , if you're using xpaths, then
driver.findElement(By.xpath("//*[#id="search"]"));
should do away the NPE - but not sure since you've used a lot of absolute xpaths - which is a horrible practise.

Selenium Web driver--Failure Screenshot is not captured in TestNG report

With below mentioned code,if the test case is pass-screenshot captured successfully and displayed in report.But when the test is failed--screenshot is not displayed.Even screenshot hyperlink is not displayed in report.Anybody can sort out the mistake in code?
package listeners;
import java.io.File;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import java.util.logging.Logger;
#Listeners
public class CountryChoserLayer extends TestListenerAdapter {
#Test(priority=1)
public void choseCountry() throws Exception{
driver.findElement(By.id("intselect")).sendKeys("India");
driver.findElement(By.xpath(".//*[#id='countryChooser']/a/img")).click();
//window.onbeforeunload = null;
Date date=new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");
File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");
FileUtils.copyFile(scrnsht, new File(NewFileNamePath));
System.out.println(NewFileNamePath);
Reporter.log("Passed Screenshot");
System.out.println("---------------------------------------");
System.out.println("Country choser layer test case-Success");
System.out.println("---------------------------------------");
}
public String baseurl="http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605";
public WebDriver driver;
public int Count = 0;
#Test(priority=0)
public void openBrowser() {
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.get(baseurl);
}
#Test(priority=2)
public void closeBrowser() {
driver.quit();
}
#Override
public void onTestFailure(ITestResult result){
Reporter.log("Fail");
System.out.println("BBB");
//Reporter.setCurrentTestResult(result);
Date date=new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");
File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//File scrFile = ((TakesScreenshot) WebDriver.globalDriverInstance).getScreenshotAs(OutputType.FILE);
String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");
//System.out.println("AAA" + NewFileNamePath);
try {
//System.out.println("CCC");
FileUtils.copyFile(scrnsht,new File(NewFileNamePath));
System.out.println(NewFileNamePath);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("DDD");
e.printStackTrace();
}
Reporter.log("Failed Screenshot");
Reporter.setCurrentTestResult(null);
System.out.println("---------------------------------------");
System.out.println("Country choser layer test case Failed");
System.out.println("---------------------------------------");
}
#Override
public void onTestSkipped(ITestResult result) {
// will be called after test will be skipped
Reporter.log("Skip");
}
#Override
public void onTestSuccess(ITestResult result) {
// will be called after test will pass
Reporter.log("Pass");
}
}
Your onTestFailure method is not being called because you didn't specify listener for your test class. You are missing a value in #Listeners annotation. It should be something like
#Listeners({CountryChoserLayer.class})
You can find more ways of specifying a listener in official TestNg's documentation.
Another problem you are likely to encounter would be NullPointerException while trying to take screenshot in onTestFailure method. The easiest workaround for that would be changing the declaration of driver field to static. I run the code with those fixes and I got the report with screenshot.
I must add that in my opinion putting both test and listener methods into one class is not a good practice.

Resources