0

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;

    @Override
    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));

        //((TestBaseClass)result.getInstance()).driver;
    }

}

public class Screenshots {

    public static void captureScreenshot(WebDriver driver, String screenShotName){
        try{
            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());
        }

    }
}

public class DriverFactory {

    public enum BrowserType{
        FIREFOX("firefox"),
        CHROME("chrome"),
        IE("internet_explorer"),
        SAFARI("safari");

        private String value;

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

        public String getBrowserName(){
            return this.value;
        }

    }

    public static WebDriver getDriver(BrowserType type){

        WebDriver driver = null;
        switch(type){
            case FIREFOX:
                System.setProperty("webdriver.gecko.driver","D://DemoQA//drivers//geckodriver.exe");
                driver = new FirefoxDriver();
                break;
            case CHROME:
                System.setProperty("webdriver.chrome.driver","D://DemoQA//drivers//chromedriver.exe");
                driver = new ChromeDriver();
                break;
            case IE:
                driver = new InternetExplorerDriver();
                break;
            case SAFARI:
                driver = new SafariDriver();
                break;
            default:
                System.setProperty("webdriver.chrome.driver","D://DemoQA//drivers//chromedriver.exe");
                driver = new ChromeDriver();
                break;
        }
        return driver;
    }

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

}

@Listeners(TestListener.class)
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 ...");
        this.driver.close();
    } 

    @Test()
    public void testLoginRegistrationPage() throws Exception{
        driver.manage().deleteAllCookies();
        registrationPage.loadPage();
        registrationPage.setTextFirstNameField("Michal");
        registrationPage.clickMaritalRadioButton();
        registrationPage.tickAllCheckboxHobbies();
        registrationPage.selectCountryFromList("Poland");
        registrationPage.fillDateOfBirth("1", "10", "2014");
        registrationPage.clickSubmitButton();
        registrationPage.verifyLoggingHeader();
    }

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){
        super(driver);
        this.PAGE_TITLE = "Registration | Demoqa";
        this.PAGE_URL = "http://demoqa.com/registration/";
    }

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

}

The problem is in this line

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

I do not know how to solve it.

7
  • I'm not sure but maybe the problem that the driver is recreated in the afformentioned line this.driver = DriverFactory.getDriver(DriverFactory.getBrowserTypeByProperty());? My hunch is that the original driver (the one running the test) should be passed to onTestFailure method (maybe contained in testResult). I'm in the dark as well a bit. Commented Feb 7, 2018 at 13:36
  • Gabor- yes I think so, but how to pass the original Driver in there ? Commented Feb 7, 2018 at 13:43
  • without the complete code of how the testResult is created I cannot really tell, and it is up to the maintainer of the whole framework to choose the best approach. The goal of the modifications can result in something like this: this.driver = testResult.getDriver(); where the returning driver is the object used in the test. There are other approaches I'm sure. Commented Feb 7, 2018 at 14:06
  • I uploaded the project in here: uploadfiles.io/n72wj If you have a minute take a look. Commented Feb 7, 2018 at 14:24
  • I'm behind proxy and cannot reach that site. Please seek other sources of help if you can as my own free time is usually quite limited. One last suggestion I would make is to make an extremely ugly hack first, where the original driver is saved in a static context, just to verify that the original problem was the driver re-creation. Commented Feb 7, 2018 at 15:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.