com.surenpi.autotest.suite.parser.XmlSuiteParser.java Source code

Java tutorial

Introduction

Here is the source code for com.surenpi.autotest.suite.parser.XmlSuiteParser.java

Source

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.surenpi.autotest.suite.parser;

import com.surenpi.autotest.suite.runner.Suite;
import com.surenpi.autotest.suite.runner.SuiteAction;
import com.surenpi.autotest.suite.runner.SuitePage;
import com.surenpi.autotest.utils.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
import org.jaxen.SimpleNamespaceContext;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * xml???
 * @author suren
 * @date 201697 ?9:36:16
 */
public class XmlSuiteParser implements SuiteParser {
    /** ??? */
    public static final String NS_URI = "http://suite.surenpi.com";

    /** ?? */
    private final List<String> supportList = new ArrayList<String>(1);
    private SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();

    /**
     * ??
     * @param suiteInputStream ??
     * @return 
     * @throws DocumentException
     */
    public Suite parse(InputStream suiteInputStream) throws DocumentException {
        SAXReader reader = new SAXReader();
        reader.setEncoding("utf-8");

        Document document = reader.read(suiteInputStream);

        simpleNamespaceContext.addNamespace("ns", NS_URI);

        XPath xpath = new DefaultXPath("/ns:suite");
        xpath.setNamespaceContext(simpleNamespaceContext);
        Element suiteEle = (Element) xpath.selectSingleNode(document);
        if (suiteEle == null) {
            suiteEle = document.getRootElement();
            //         throw new RuntimeException("Can not found suite config.");
        }

        Suite suite = new Suite();
        String xmlConfPath = suiteEle.attributeValue("pageConfig");
        String pagePackage = suiteEle.attributeValue("pagePackage", "");
        String rows = suiteEle.attributeValue("rows", "1");
        String lackLines = suiteEle.attributeValue("lackLines", "nearby");
        String errorLines = suiteEle.attributeValue("errorLines", "stop");
        String afterSleep = suiteEle.attributeValue("afterSleep", "0");

        suite.setXmlConfPath(xmlConfPath);
        suite.setPagePackage(pagePackage);
        suite.setRows(rows);
        suite.setLackLines(lackLines);
        suite.setErrorLines(errorLines);
        suite.setAfterSleep(Long.parseLong(afterSleep));

        pagesParse(document, suite);

        return suite;
    }

    /**
     * ?page?
     * @param document
     * @param suite
     */
    private void pagesParse(Document document, Suite suite) {
        List<SuitePage> pageList = new ArrayList<SuitePage>();
        suite.setPageList(pageList);

        String pagePackage = suite.getPagePackage();
        if (!StringUtils.isBlank(pagePackage)) {
            pagePackage = (pagePackage.trim() + ".");
        }

        XPath xpath = new DefaultXPath("/ns:suite/ns:page");
        xpath.setNamespaceContext(simpleNamespaceContext);

        @SuppressWarnings("unchecked")
        List<Element> pageNodes = xpath.selectNodes(document);
        if (pageNodes == null || pageNodes.size() == 0) {
            throw new RuntimeException("Can not found page config.");
        }

        for (Element pageEle : pageNodes) {
            String pageCls = pageEle.attributeValue("class");

            xpath = new DefaultXPath("ns:actions");
            xpath.setNamespaceContext(simpleNamespaceContext);

            Element actionsEle = (Element) xpath.selectSingleNode(pageEle);
            if (actionsEle == null) {
                throw new RuntimeException("Can not found actions config.");
            }

            String disable = actionsEle.attributeValue("disable", "false");
            if (Boolean.parseBoolean(disable)) {
                continue;
            }

            String beforeSleep = actionsEle.attributeValue("beforeSleep", "0");
            String afterSleep = actionsEle.attributeValue("afterSleep", "0");
            String repeat = actionsEle.attributeValue("repeat", "1");

            List<SuiteAction> actionList = new ArrayList<SuiteAction>();

            SuitePage suitePage = new SuitePage(String.format("%s%s", pagePackage, pageCls));
            suitePage.setActionList(actionList);
            suitePage.setRepeat(Integer.parseInt(repeat));

            pageList.add(suitePage);

            parse(actionList, actionsEle, beforeSleep, afterSleep);
        }
    }

    /**
     * ?action
     * @param actionList
     * @param actionsEle action
     * @param afterSleep action
     * @param beforeSleep action
     */
    private void parse(List<SuiteAction> actionList, final Element actionsEle, String beforeSleep,
            String afterSleep) {
        DefaultXPath xpath = new DefaultXPath("ns:action");
        xpath.setNamespaceContext(simpleNamespaceContext);

        @SuppressWarnings("unchecked")
        List<Element> actionEleList = xpath.selectNodes(actionsEle);
        for (Element actionEle : actionEleList) {
            String field = actionEle.attributeValue("field");
            String name = actionEle.attributeValue("name", "click");
            String invoker = actionEle.attributeValue("invoker");
            String actionBeforeSleep = actionEle.attributeValue("beforeSleep", beforeSleep);
            String actionAfterSleep = actionEle.attributeValue("afterSleep", afterSleep);
            String repeat = actionEle.attributeValue("repeat", "1");
            String disable = actionEle.attributeValue("disable", "false");

            if (Boolean.parseBoolean(disable)) {
                continue;
            }

            SuiteAction suiteAction = new SuiteAction(field, name);
            suiteAction.setInvoker(invoker);
            suiteAction.setBeforeSleep(Long.parseLong(actionBeforeSleep));
            suiteAction.setAfterSleep(Long.parseLong(actionAfterSleep));
            suiteAction.setRepeat(Integer.parseInt(repeat));

            parseActionParam(actionEle, suiteAction);

            actionList.add(suiteAction);
        }
    }

    /**
     * @param actionEle
     * @param suiteAction
     */
    private void parseActionParam(Element actionEle, SuiteAction suiteAction) {
        List<String> paramList = suiteAction.getParamList();

        @SuppressWarnings("unchecked")
        List<Element> paramEleList = actionEle.elements("param");
        if (paramEleList != null) {
            for (Element paramEle : paramEleList) {
                paramList.add(paramEle.attributeValue("value"));
            }
        }
    }
}