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.
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.this.driver = testResult.getDriver();where the returning driver is the object used in the test. There are other approaches I'm sure.