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;
}
}
Class Screenshots:
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());
}
}
}
class DriverFactory:
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);
}
.....
}
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