Code Snippet

Just another Code Snippet site

[Selenium] SeleniumTestCase class example

public abstract class SeleniumTestCase {

    private static final int TIMEOUT = 80; // seconds
    private static final int STEP_WAIT_TIME = 500; // milliseconds
    private static final String BASE_URL = "http://vm-dev:7001/";
    protected boolean quit = false;

    enum Driver {
        Firefox, Chrome, IE;
    }

    protected WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private final StringBuffer verificationErrors = new StringBuffer();
    private final String hubUrl = "";
    private Capabilities actualCapabilities; // Browser settings

    /**  */
    public SeleniumTestCase() {
        getLogger().info("----------------------------------------------------");
        getLogger().info("Create new Selenium test case");
        getLogger().info("BaseUrl : {}", BASE_URL);
        getLogger().info("Timeout (sec) : {}", TIMEOUT);
    }

    /**
     * Call before all TC.  
     * @throws Exception
     */
    @Before
    public void setUp() throws Exception {
        getLogger().info("setUp");

        // Prepare Broswer settings
        DesiredCapabilities capabilities = new DesiredCapabilities();

        // Retrieve param
        Driver browser = Driver.Firefox; // Default Browser
        String browserParam = System.getProperty("selenium.browser");
        String browserVersionParam = System.getProperty("selenium.browser.version");
        String host = System.getProperty("selenium.server.host");
        String port = System.getProperty("selenium.server.port");

        // Check default values
        if (browserParam != null) {
            if (Driver.Chrome.name().equalsIgnoreCase(browserParam)) {
                browser = Driver.Chrome;
            } else if (Driver.IE.name().equalsIgnoreCase(browserParam)) {
                browser = Driver.IE;
            } else {
                browser = Driver.Firefox;
            }
        }

        if (host == null || host.isEmpty()) {
            host = "localhost"; // Default value
        }
        if (port == null || port.isEmpty()) {
            port = "4444"; // Default value
        }
        hubUrl = System.getProperty("selenium.hub.url");
        if (hubUrl == null || hubUrl.isEmpty()) {
            hubUrl = "http://" + host + ":" + port + "/wd/hub";
        }

        switch (browser) {
            case Chrome:
                capabilities = DesiredCapabilities.chrome();
                capabilities.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
                break;
            case IE:
                capabilities = DesiredCapabilities.internetExplorer();
                capabilities.setBrowserName(DesiredCapabilities.internetExplorer().getBrowserName());
                break;
            case Firefox:
            default:
                FirefoxProfile profile = new FirefoxProfile();
                profile.setEnableNativeEvents(true);
                capabilities = DesiredCapabilities.firefox();
                capabilities.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
                capabilities.setCapability(FirefoxDriver.PROFILE, profile);
        }
        capabilities.setJavascriptEnabled(true);

        // Set browser version
        if (browserVersionParam != null && !browserVersionParam.isEmpty()) {
            capabilities.setVersion(browserVersionParam);
        }

        // Log
        getLogger().info("Browser : {} ({})", browser, browserVersionParam);
        getLogger().info("Selenium hubUrl : {}", hubUrl);

        // Get a handle to the driver. This will throw an exception
        // if a matching driver cannot be located
        driver = new RemoteWebDriver(new URL(hubUrl), capabilities);

        // Query the driver to find out more information
        actualCapabilities = ((RemoteWebDriver) driver).getCapabilities();

        // And now use it
        baseUrl = BASE_URL;
        driver.manage().timeouts().implicitlyWait(TIMEOUT, TimeUnit.SECONDS);
    }

    /**
     * Call after each TC.
     * @throws Exception
     */
    @After
    public void tearDown() throws Exception {
        getLogger().info("tearDown");
        String source = driver.getPageSource();
        String verificationErrorString = verificationErrors.toString();
        if (quit) {
            driver.quit();
            if (!"".equals(verificationErrorString)) {
                getLogger().error(verificationErrorString);
                getLogger().error(source);

                File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                // Now you can do whatever you need to do with it, for example copy somewhere
                FileUtils.copyFile(scrFile, new File("screenshot.png"));

                fail(verificationErrorString);
            }
        }
    }

    public abstract Logger getLogger();

    /**
     * Open an URL in the browser
     * @param url
     */
    public void goTo(final String url) {

        WebDriverWait wait = new WebDriverWait(driver, 10); // seconds

        // Wait 10sec max
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                driver.get(url);
                return true;
            }
        });
    }

, ,


Comments are currently closed.