用WebDriver实现基于jira过滤器视图的统计自动化-阿里云开发者社区

开发者社区> 开发与运维> 正文

用WebDriver实现基于jira过滤器视图的统计自动化

简介: 在Jira上通过过滤器我们可以做出多种视图,以方便统计我们想要收集的结果。比如:我想查看所有分派给我的任务。在Jira上,我保存了一个过滤器,叫做“分派给我的所有任务”。这个过滤器可以过滤出所有分配给我的任务。

在Jira上通过过滤器我们可以做出多种视图,以方便统计我们想要收集的结果。比如:我想查看所有分派给我的任务。在Jira上,我保存了一个过滤器,叫做“分派给我的所有任务”。这个过滤器可以过滤出所有分配给我的任务。下面我要对每一个分配给我的任务做一下统计,收集一些关键的字段值,并输出到excel表:

 

利用WebDriver实现,代码如下:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class TestInfoGet {

    public static void main(String[] args) throws InterruptedException, IOException {
        String webUrl = "http://jira.ms.netease.com/browse/LOTTERY-xxx?filter=xxxx";
        WebDriver webDriver = new FirefoxDriver();
        webDriver.get(webUrl);
        waitUntilPageLoadedId(webDriver, "login-form-username");
        webDriver.findElement(By.id("login-form-username")).sendKeys("username");
        webDriver.findElement(By.id("login-form-password")).sendKeys("password");
        webDriver.findElement(By.id("login-form-submit")).click();
        webDriver.get(webUrl);

        // Main title.
        waitUntilPageLoadedId(webDriver, "search-header-view");
        String mainTitle = webDriver.findElement(By.id("search-header-view")).findElement(By.className("search-title"))
                .getText();
        System.out.println(mainTitle);

        // Write an excel.
        try {
            FileSystemView fsv = FileSystemView.getFileSystemView();
            WritableWorkbook book = Workbook
                    .createWorkbook(new File(fsv.getHomeDirectory() + "\\" + mainTitle + ".xls"));
            WritableSheet sheet = book.createSheet(mainTitle, 0);
            List<String> labels = Arrays.asList("活动名称", "JIRA链接", "类型", "状态", "优先级", "解决结果", "模块", "平台", "测试类型", "bug数",
                    "用例数据", "报告人", "产品人员", "前端", "开发", "分派给");
            for (int i = 0; i < labels.size(); i++) {
                sheet.addCell(new Label(i, 0, labels.get(i)));
            }

            // Get all sub titles.
            waitUntilPageLoadedClass(webDriver, "issue-list");
            WebElement webElement = webDriver.findElement(By.className("issue-list"));
            List<WebElement> webElements = webElement.findElements(By.tagName("li"));
            List<String> titles = new ArrayList<String>();
            for (WebElement webElement2 : webElements) {
                titles.add(webElement2.getAttribute("title"));
            }

            // For each sub title.
            for (int i = 1; i <= titles.size(); i++) {
                String xPath = "//*[@title='" + titles.get(i - 1) + "']";
                webDriver.findElement(By.xpath(xPath)).click();
                waitUntilPageLoadedId(webDriver, "issue-content");
                Thread.sleep(1000);
                System.out.println("--------------------------------------------");
                // Get the JIRA link.
                System.out.println("活动名称:" + webDriver.findElement(By.id("summary-val")).getText());
                sheet.addCell(new Label(0, i, webDriver.findElement(By.id("summary-val")).getText()));
                String href = webDriver.findElement(By.id("key-val")).getAttribute("href");
                // String link = "http://jira.ms.netease.com/browse/" + href;
                System.out.println("JIRA链接:" + href);
                sheet.addCell(new Label(1, i, href + " "));
                waitUntilPageLoadedId(webDriver, "issuedetails");
                // Issue details.
                WebElement issueDetails = webDriver.findElement(By.id("issuedetails"));
                String detailName, detailValue;
                for (WebElement issueDetail : issueDetails.findElements(By.tagName("li"))) {
                    detailName = issueDetail.getText().split(":")[0];
                    detailValue = issueDetail.getText().split(":")[1].replaceAll("\n", "").split(" ")[0];
                    System.out.println(detailName + ":" + detailValue);
                    sheet = insertData(detailName, detailValue, sheet, i);
                }
                // Custom field module.
                try {
                    WebElement customModule = webDriver.findElement(By.id("customfieldmodule"));
                    String moduleText, moduleName, moduleValue;
                    for (WebElement cModule : customModule.findElements(By.className("item"))) {
                        moduleText = cModule.getText().replaceAll("\n", "");
                        System.out.println(moduleText);
                        moduleName = moduleText.split(":")[0];
                        moduleValue = moduleText.split(":")[1];
                        sheet = insertData(moduleName, moduleValue, sheet, i);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println(e.toString());
                }
                // Participates.
                WebElement pd = webDriver.findElement(By.id("peoplemodule"))
                        .findElement(By.className("people-details"));
                String pCategory, pName;
                for (WebElement webElement3 : pd.findElements(By.tagName("dl"))) {
                    System.out.println(webElement3.findElement(By.tagName("dt")).getText()
                            + webElement3.findElement(By.tagName("dd")).getText());
                    pCategory = webElement3.findElement(By.tagName("dt")).getText().split(":")[0];
                    pName = webElement3.findElement(By.tagName("dd")).getText();
                    sheet = insertData(pCategory, pName, sheet, i);
                }
            }
            book.write();
            book.close();
        } catch (WriteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

    private static WritableSheet insertData(String name, String value, WritableSheet sheet, int i)
            throws RowsExceededException, WriteException {
        // TODO Auto-generated method stub
        int j = 16;
        switch (name) {
        case "类型":
            j = 2;
            sheet.addCell(new Label(j, i, value));
            break;
        case "状态":
            j = 3;
            sheet.addCell(new Label(j, i, value));
            break;
        case "优先级":
            j = 4;
            sheet.addCell(new Label(j, i, value));
            break;
        case "解决结果":
            j = 5;
            sheet.addCell(new Label(j, i, value));
            break;
        case "模块":
            j = 6;
            sheet.addCell(new Label(j, i, value));
            break;
        case "平台":
            j = 7;
            sheet.addCell(new Label(j, i, value));
            break;
        case "测试类型":
            j = 8;
            sheet.addCell(new Label(j, i, value));
            break;
        case "bug数":
            j = 9;
            sheet.addCell(new Label(j, i, value));
            break;
        case "用例数据":
            j = 10;
            sheet.addCell(new Label(j, i, value));
            break;
        case "用例数":
            j = 10;
            sheet.addCell(new Label(j, i, value));
            break;
        case "报告人":
            j = 11;
            sheet.addCell(new Label(j, i, value));
            break;
        case "产品人员":
            j = 12;
            sheet.addCell(new Label(j, i, value));
            break;
        case "前端":
            j = 13;
            sheet.addCell(new Label(j, i, value));
            break;
        case "开发":
            j = 14;
            sheet.addCell(new Label(j, i, value));
            break;
        case "分派给":
            j = 15;
            sheet.addCell(new Label(j, i, value));
            break;
        }
        return sheet;
    }

    private static void waitUntilPageLoadedId(WebDriver iw, String v) throws InterruptedException {
        try {
            iw.findElement(By.id(v));
        } catch (Exception ex) {
            System.out.println(ex.toString());
            Thread.sleep(1000);
            waitUntilPageLoadedId(iw, v);
        }
    }

    private static void waitUntilPageLoadedClass(WebDriver iw, String v) throws InterruptedException {
        try {
            iw.findElement(By.className(v));
        } catch (Exception ex) {
            System.out.println(ex.toString());
            Thread.sleep(1000);
            waitUntilPageLoadedClass(iw, v);
        }
    }

}

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章