日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

在測試框架中使用Log4J 2

 小豬窩969 2015-08-28

之前的測試框架:http://www.cnblogs.com/tobecrazy/p/4553444.html


配合Jenkins可持續(xù)集成:http://www.cnblogs.com/tobecrazy/p/4529399.html


這次demo的代碼已經(jīng)放到github:https://github.com/tobecrazy/Demo


打log是一個測試框架必備的功能之一,trace測試執(zhí)行的內(nèi)容,使測試分析更容易和有規(guī)律可循。進(jìn)而進(jìn)一步處理測試log,實(shí)現(xiàn)自動分析測試結(jié)果。


現(xiàn)在java項目寫日志一般使用Log4j 2


log4j 2是一個開源的記錄log的框架,比log4j 效率更高


更多內(nèi)容:http://logging./log4j/2.x/manual/index.html


首先下載相應(yīng)的jar包,放到工程:



接下來創(chuàng)建Log類,使用


clazz.getCanonicalName() 獲取類名字,為將來case執(zhí)行獲取case名和page名和action名

復(fù)制代碼

package com.log;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log {
    private final Class<?> clazz;
    private Logger logger;

    /**
     * 
     * @param clazz
     */
    Log(Class<?> clazz) {
        this.clazz = clazz;
        this.logger = LogManager.getLogger(this.clazz);
    }

    /**
     * @author Young
     * @param message
     * 
     */
    public void info(String message) {
        logger.info(clazz.getCanonicalName() + ": " + message);
    }

    /**
     * @author Young
     * @param message
     */
    public void debug(String message) {
        logger.debug(clazz.getCanonicalName() + ": " + message);
    }

    /**
     * @author Young
     * @param message
     */
    public void error(String message) {
        logger.error(clazz.getCanonicalName() + ": " + message);
    }

    /**
     * @author Young
     * @param message
     */
    public void trace(String message) {
        logger.trace(clazz.getCanonicalName() + ": " + message);
    }

    /**
     * @author Young
     * @param message
     */
    public void warn(String message) {
        logger.warn(clazz.getCanonicalName() + ": " + message);
    }

    /**
     * @author Young
     * @param message
     */
    public void fatal(String message) {
        logger.fatal(clazz.getCanonicalName() + ": " + message);
    }
}

復(fù)制代碼

 


接下來就可以直接調(diào)用這個類:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.log;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Log log=new Log(test.class);
        log.info("this is my test");
        log.debug("this is a debug");
        log.error("this is an error ");
        log.fatal("this is a fatal");
        log.trace("this is a trace");
    }
}


  然后, run as application 吧


結(jié)果如下: holy shit,damn it 居然報錯


ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
23:52:47.679 [main] ERROR com.log.test - com.log.test: this is an error
23:52:47.680 [main] FATAL com.log.test - com.log.test: this is a fatal


原來是缺少config


于是乎準(zhǔn)備一份log4j2.xml文件,放在該package下:




View Code

但是依然不行,沒法加參數(shù)


加上:



1
2
3
File config=new File("C:/Users/Young/workspace/Log4j/src/com/log/log4j2.xml"); 
        ConfigurationSource source = new ConfigurationSource(new FileInputStream(config),config); 
        Configurator.initialize(null, source);


  妥妥的 , 運(yùn)行結(jié)果如下:


2015-06-07 00:06:31 [main] INFO com.log.test - com.log.test: this is my test
2015-06-07 00:06:31 [main] DEBUG com.log.test - com.log.test: this is a debug
2015-06-07 00:06:31 [main] ERROR com.log.test - com.log.test: this is an error
2015-06-07 00:06:31 [main] FATAL com.log.test - com.log.test: this is a fatal
2015-06-07 00:06:31 [main] TRACE com.log.test - com.log.test: this is a trace


并且logs目錄下生城相應(yīng)的log文件


當(dāng)然,這并不完美,log4j可以放在src目錄下,這要就不需要指定位置


接下來把這個log類放在測試框架里:



在basePage加入相應(yīng)的代碼,如下:



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.dbyl.libarary.utils;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BasePage {
    protected WebDriver driver;
    protected String[][] locatorMap;
    protected Log log= new Log(this.getClass());
    protected BasePage(WebDriver driver) throws IOException {
        this.driver = driver;
        locatorMap = ReadExcelUtil.getLocatorMap();
    }
    protected void type(Locator locator, String values) throws Exception {
        WebElement e = findElement(driver, locator);
        log.info("type value is:  "+ values);
        e.sendKeys(values);
    }
    protected void click(Locator locator) throws Exception {
        WebElement e = findElement(driver, locator);
        log.info("click button");
        e.click();
    }
    protected void clickAndHold(Locator locator) throws IOException {
        WebElement e = findElement(driver, locator);
        Actions actions = new Actions(driver);
        actions.clickAndHold(e).perform();
    }
    public WebDriver getDriver() {
        return driver;
    }
    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }
    public WebElement getElement(Locator locator) throws IOException {
        return getElement(this.getDriver(), locator);
    }
    /**
     * get by parameter
     *
     * @author Young
     * @param driver
     * @param locator
     * @return
     * @throws IOException
     */
    public WebElement getElement(WebDriver driver, Locator locator)
            throws IOException {
        locator = getLocator(locator.getElement());
        WebElement e;
        switch (locator.getBy()) {
        case xpath:
            log.debug("find element By xpath");
            e = driver.findElement(By.xpath(locator.getElement()));
            break;
        case id:
            log.debug("find element By id");
            e = driver.findElement(By.id(locator.getElement()));
            break;
        case name:
            log.debug("find element By name");
            e = driver.findElement(By.name(locator.getElement()));
            break;
        case cssSelector:
            log.debug("find element By cssSelector");
            e = driver.findElement(By.cssSelector(locator.getElement()));
            break;
        case className:
            log.debug("find element By className");
            e = driver.findElement(By.className(locator.getElement()));
            break;
        case tagName:
            log.debug("find element By tagName");
            e = driver.findElement(By.tagName(locator.getElement()));
            break;
        case linkText:
            log.debug("find element By linkText");
            e = driver.findElement(By.linkText(locator.getElement()));
            break;
        case partialLinkText:
            log.debug("find element By partialLinkText");
            e = driver.findElement(By.partialLinkText(locator.getElement()));
            break;
        default:
            e = driver.findElement(By.id(locator.getElement()));
        }
        return e;
    }
    public boolean isElementPresent(WebDriver driver, Locator myLocator,
            int timeOut) throws IOException {
        final Locator locator = getLocator(myLocator.getElement());
        boolean isPresent = false;
        WebDriverWait wait = new WebDriverWait(driver, 60);
        isPresent = wait.until(new ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver d) {
                return findElement(d, locator);
            }
        }).isDisplayed();
        return isPresent;
    }
    /**
     * This Method for check isPresent Locator
     *
     * @param locator
     * @param timeOut
     * @return
     * @throws IOException
     */
    public boolean isElementPresent(Locator locator, int timeOut)
            throws IOException {
        return isElementPresent(driver,locator, timeOut);
    }
    /**
     *
     * @param driver
     * @param locator
     * @return
     */
    public WebElement findElement(WebDriver driver, final Locator locator) {
        WebElement element = (new WebDriverWait(driver, locator.getWaitSec()))
                .until(new ExpectedCondition<WebElement>() {
                    @Override
                    public WebElement apply(WebDriver driver) {
                        try {
                            return getElement(driver, locator);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            log.error("can't find element "+locator.getElement());
                            return null;
                        }
                    }
                });
        return element;
    }
    public Locator getLocator(String locatorName) throws IOException {
        Locator locator;
        for (int i = 0; i < locatorMap.length; i++) {
            if (locatorMap[i][0].endsWith(locatorName)) {
                return locator = new Locator(locatorMap[i][1]);
            }
        }
        return locator = new Locator(locatorName);
    }
}


  在UITest類也加入相應(yīng)的代碼:




View Code

接下來就是見證奇跡的時候:


運(yùn)行一下:log能夠清晰記錄測試時所做的內(nèi)容



也有相應(yīng)的file log


今天的工程下載地址:http://pan.baidu.com/s/1kTqvzZx

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多