Selenium: simple WebDriver (ChromeDriver) Setup class

Last updated on:


import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
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.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by slavkurochkin on 12/23/14.
 */



public class DriverSetup {

    public static WebDriver driver;

    public static int testPassed = 0;
    public static int testFailed = 0;

    @BeforeMethod
    public void setupTheChromeDriverSystemProperty()throws Exception {

        try {

        /*
            // tell webdriver where to find the chrome driver
            String currentDir = System.getProperty("user.dir");
            String chromeDriverLocation = currentDir + "/chromedriver";
            System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
        */

            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setBrowserName("Chrome");
            capabilities.setPlatform(Platform.extractFromSysProperty("Mac"));

            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");

      driver = new ChromeDriver();

            System.out.println("App launched");
            Success();
            Thread.sleep(5000);

        } catch (Exception e) {


            System.out.println("App launch fail");
            Failure();
        }


    }

    @AfterMethod
    public void tearDown(ITestResult result) throws Exception {
        if(!result.isSuccess()){


            driver =  (WebDriver) new Augmenter().augment(driver);
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            System.out.println("Screenshot completed");
            System.setProperty("org.uncommons.reportng.escape-output", "false");

            try {

                DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");
                Date date = new Date();

                File directory = new File(".");
                String newFileNamePath = directory.getCanonicalPath() + "/test-output/screenshots/";

                File testScreenShot = new File(newFileNamePath + dateFormat.format(date) +".jpg");

                Reporter.setCurrentTestResult(result);
                Reporter.log("<br> <img src=" + testScreenShot + "/> <br>");
                Reporter.setCurrentTestResult(null);

                Reporter.log("<p align="left">Error screenshot at " + new Date()+ "</p>");

                Reporter.log("<p><img width="800" src="" + testScreenShot + "" alt="screenshot at " + new Date() + ""/></p><br />");

                // Copy the file to screenshot folder

                FileUtils.copyFile(scrFile, testScreenShot);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        driver.quit();
    }

    public void Failure() {
        System.out.println("Failure");
        testFailed++;
    }

    public void Success() {
        System.out.println("Success");
        testPassed++;
    }

}
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.