jeudi 26 décembre 2019

Allure Create Screenshot - Selenium

I'm having trouble taking screenshots with allure. I have configured my interface in which in case of failure test I call the function whose task is to create a screenshot in the report. However, this does not work. Screenshot are not created.

Below is my interface:

package Test;

import Test.resources.Base;
import io.qameta.allure.Allure;
import io.qameta.allure.Attachment;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ListenersT extends Base implements ITestListener {
    Base b = new Base();


    public void onFinish(ITestContext arg0) {
        // TODO Auto-generated method stub

    }


    public void onStart(ITestContext arg0) {
        // TODO Auto-generated method stub

    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
        // TODO Auto-generated method stub

    }



    public void onTestFailure(ITestResult result) {
        // TODO Auto-generated method stub
        //screenshot

        try {
            b.getScreenshot(result.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Object testClass = result.getInstance();
        WebDriver driver = ((Base) testClass).getDriver();
        if(driver instanceof  WebDriver) {
            takeScreenshot(driver);
        }
    }

    @Attachment(value = "Page screenshot", type ="image/png")
    public byte[] takeScreenshot(WebDriver driver) {
        byte[] screenshotAs = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
        return screenshotAs;
    }


    public void onTestSkipped(ITestResult arg0) {
        // TODO Auto-generated method stub

    }

    public void onTestStart(ITestResult arg0) {
        // TODO Auto-generated method stub

    }

    public void onTestSuccess(ITestResult arg0) {
        // TODO Auto-generated method stub

    }


}

And here is my base class form which extend my interface:

package Test.resources;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Listeners;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class Base {

    public static WebDriver driver;


    public WebDriver initializeDriver() throws IOException {

        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "\\src\\main\\java\\Test\\resources\\data.properties");
        prop.load(fis);

        Properties propBuykers = new Properties();
        FileInputStream fisBuykers = new FileInputStream(System.getProperty("user.dir") + "\\src\\main\\java\\Test\\resources\\data.propertiesBuykers");
        propBuykers.load(fisBuykers);

        String browserName;

        if(System.getProperty("browser") == null) {
            browserName = "chrome";
        }
        else {
            browserName = System.getProperty("browser");
        }


        //String browserName = prop.getProperty("browser");

        if(browserName.contains("chrome")) {
            System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\ChromeDriver\\chromedriver2.exe");
            ChromeOptions options = new ChromeOptions();


            if(browserName.contains("headless")) {
                options.addArguments("headless");
            }
            driver = new ChromeDriver(options);
        }
        else if(browserName.equals("firefox")) {
            System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\GeckoDriver\\geckodriver.exe");
            driver = new FirefoxDriver();
        }
        else if(browserName.equals("edge")) {
            System.setProperty("webdriver.edge.driver", "C:\\Program Files (x86)\\MicrosoftWebDriver\\msedgedriver.exe");
            driver = new EdgeDriver();
        }

        else if (browserName.equals("remoteBrowser")) {
            DesiredCapabilities caps = DesiredCapabilities.chrome();
            caps.setCapability("platform", "Windows 10");
            caps.setCapability("version", "66.0");

            RemoteWebDriver driver = new RemoteWebDriver(new URL("g42664-b435-4a95-9b68-f43b51a43134@ondemand.eu-central-1.saucelabs.com:443/wd/hub"), caps);
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            return driver;
        }

        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        return driver;
    }

    public void getScreenshot(String result) throws IOException {
        File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(src, new File("C://s//"+result+"screenshot.png"));
    }

    public WebDriver getDriver() {
        return driver;
    }
}

Please help me what I am doing wrong.

Aucun commentaire:

Enregistrer un commentaire