0

我正在创建 TestFramework 并希望在 TestListener 类中设置截取屏幕截图的方法,但在测试失败期间浏览器关闭,打开另一个浏览器并创建屏幕截图(空屏幕截图)。

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);
    }
     .....

}

问题出在这一行

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

我不知道如何解决它。

4

0 回答 0