I am creating TestFramework and want to set in TestListener class the method which take screenshots but during failed test the Browser closes, another browser is opened and the screenshot is created (empty screenshot).

public class TestListener extends TestListenerAdapter {

    private WebDriver driver;

    public void onTestFailure(ITestResult testResult){
        System.out.println(testResult.getName() + " was failure. \n Throwable " + testResult.getThrowable().getMessage());

        this.driver = DriverFactory.getDriver(DriverFactory.getBrowserTypeByProperty());
        Screenshots.captureScreenshot(driver, RandomValuesGenerator.generateRandomString(4));



Class Screenshots:

public class Screenshots {

    public static void captureScreenshot(WebDriver driver, String screenShotName){
            TakesScreenshot ts = (TakesScreenshot)driver;
            File source = ts.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(source, new File("D:\\DemoQA\\ScreenShotsOfFailedTests\\" + screenShotName + ".png"));
            System.out.println("Screenshot taken " + screenShotName + ".png");
        catch (Exception e){
            System.out.println("Exception while taking screenshot \n" + e.getMessage());


class DriverFactory:

public class DriverFactory {

    public enum BrowserType{

        private String value;

        BrowserType(String value){
            this.value = value;

        public String getBrowserName(){
            return this.value;


    public static WebDriver getDriver(BrowserType type){

        WebDriver driver = null;
            case FIREFOX:
                driver = new FirefoxDriver();
            case CHROME:
                driver = new ChromeDriver();
            case IE:
                driver = new InternetExplorerDriver();
            case SAFARI:
                driver = new SafariDriver();
                driver = new ChromeDriver();
        return driver;

    public static BrowserType getBrowserTypeByProperty(){
        BrowserType type = null;
        String browserName = junitx.util.PropertyManager.getProperty("BROWSER");
        for(BrowserType bType : BrowserType.values()){
                type = bType;
                System.out.println("BROWSER = " + type.getBrowserName());
        return type;


public class RegistrationTest {

    public WebDriver driver;
    RegistrationPage registrationPage;

    @BeforeClass(alwaysRun = true)
    public void setup(){
        this.driver = getDriver( DriverFactory.getBrowserTypeByProperty() );
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        registrationPage = PageFactory.initElements(driver, RegistrationPage.class);

    @AfterClass(alwaysRun = true)
    public void teardown(){
        System.out.println("AfterClass is executing ...");

    public void testLoginRegistrationPage() throws Exception{
        registrationPage.fillDateOfBirth("1", "10", "2014");

public class RegistrationPage extends BasePage {

    @FindBy(id = "name_3_firstname") WebElement firstNameField;
    @FindBy(id = "name_3_lastname") WebElement lastNameField;
    @FindBy(xpath = "//input[@value = 'married']")

    public RegistrationPage(WebDriver driver){
        this.PAGE_TITLE = "Registration | Demoqa";
        this.PAGE_URL = "http://demoqa.com/registration/";

    public void setTextFirstNameField(String text){
        setTextOnElement(firstNameField, text);


Now, my problem is with this line-

this.driver = DriverFactory.getDriver(DriverFactory.getBrowserTypeByProperty());

I don't know how to resolve it.

The driver initializes 2 times but I do not know what put in-

this.driver = .....

When I tried to put the driver from BasePage - the error during test occured - cannot cast to RegistrationTest


Your factory class will only ever give you a new instance of a Driver, if you want idempotent behaviour that will give the same driver each time then you need to create it before the tests are run. Then keep it in scope by passing it to the test classes startup methods.

  • But I initialized the browser in RegistrationTest: @BeforeClass(alwaysRun = true) public void setup(){ this.driver = getDriver( DriverFactory.getBrowserTypeByProperty() ); registrationPage = PageFactory.initElements(driver, RegistrationPage.class); } and then wanted to take it from BasePage in TestListener: WebDriver driver; driver = ((BasePage)testResult.getInstance()).driver; the error during test occured - cannot cast to RegistrationTest Maybe I do not catch your answer but I am not too experienced if you have a time:uploadfiles.io/n72wj – Dawid Feb 8 '18 at 17:23
  • So if you have the driver already in the class then don't call the factory to replace it , just use it directly. There are other languages that are easier to use than java for testing, use one you know and keep it simple – Amias Feb 8 '18 at 17:39
  • I wanted to use it directly by getting it from BasePage class but there was a problem with casting as i typed above. – Dawid Feb 8 '18 at 17:58
  • You overwrite this each time in setup , check if it exists and don't recreate it if it does. – Amias Feb 8 '18 at 18:04
  • Please could you type the code how should it look like because I am really stuck :( – Dawid Feb 8 '18 at 18:47

It seems that you send driver incorrectly. Try to debug and check if driver is not null.


I think you are initialising browser in beforeclass and in OnTestFailureMethod, not sure but good to have a look, my suggestion is to add break point at both the initialisations and see how it works. Sorry if i am wrong happy if this helps to solve your issue - tried to add as a comment but dont have enought points to do so :(:(:(

  • Yes I know but I tried to get it from BasePage like: WebDriver driver; driver = ((BasePage)testResult.getInstance()).driver; - and the error during test occured - cannot cast to RegistrationTest – Dawid Feb 8 '18 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.