严智鑫
2025-11-13 945bc394f40d8af1072a53da9a94f24207124e6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.northglass.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("任务管理");
        }
    }
}