cn.aozhi.songify.functional.BaseSeleniumTestCase.java Source code

Java tutorial

Introduction

Here is the source code for cn.aozhi.songify.functional.BaseSeleniumTestCase.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.aozhi.songify.functional;

import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.springside.modules.test.selenium.Selenium2;
import org.springside.modules.test.selenium.SeleniumSnapshotRule;
import org.springside.modules.test.selenium.WebDriverFactory;

/**
 * Selenium.
 * 
 * BaseFunctionalTestCase?Selenium.
 * 
 * @author calvin
 */
public class BaseSeleniumTestCase extends BaseFunctionalTestCase {

    protected static Selenium2 s;

    // ?
    @Rule
    public TestRule snapshotRule = new SeleniumSnapshotRule(s);

    @BeforeClass
    public static void initSelenium() throws Exception {
        createSeleniumOnce();
        loginAsUserIfNecessary();
    }

    /**
     * Selenium.
     */
    protected static void createSeleniumOnce() throws Exception {
        if (s == null) {
            // ??Selenium driver.
            String driverName = propertiesLoader.getProperty("selenium.driver");

            WebDriver driver = WebDriverFactory.createDriver(driverName);

            s = new Selenium2(driver, baseUrl);
            s.setStopAtShutdown();
        }
    }

    /**
     * ?, .
     */
    protected static void loginAsUserIfNecessary() {
        s.open("/task");

        if (s.getTitle().contains("")) {
            s.type(By.name("username"), "user");
            s.type(By.name("password"), "user");
            s.check(By.name("rememberMe"));
            s.click(By.id("submit_btn"));
            s.waitForTitleContains("?");
        }
    }
}