Exemple #1
0
        public void Load(string SettingsFilePath, string ResultFilePath)
        {
            try
            {
                XDocument settings = new XDocument();
                if (File.Exists(SettingsFilePath))
                {
                    try
                    {
                        settings = XDocument.Load(SettingsFilePath);

                        XmlSchemaSet schemas = new XmlSchemaSet();
                        schemas.Add("", AppDomain.CurrentDomain.BaseDirectory + "Settings.xsd");

                        string validationErrorMessage = string.Empty;
                        settings.Validate(schemas, (o, ex) =>
                        {
                            validationErrorMessage = ex.Message;
                        });

                        if (!string.IsNullOrEmpty(validationErrorMessage))
                        {
                            LogMessage("File " + SettingsFilePath + " has incompartible schema structure. Error: " + validationErrorMessage);
                            throw new ApplicationException(validationErrorMessage);
                        }
                    }
                    catch (FileLoadException ex)
                    {
                        LogMessage("File " + SettingsFilePath + " was not found or not available. Error: " + ex.Message);
                        throw;
                    }
                }
                else
                {
                    try
                    {
                        settings = XDocument.Parse(SettingsFilePath); //try to load text as XML
                    }
                    catch (XmlException ex)
                    {
                        LogMessage("Settings argument is not valid xml: " + ex.Message);
                        throw;
                    }
                }

                var proc = new XProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"Settings.xsl\"");
                settings.Root.AddBeforeSelf(proc);
                XAttribute runAttribute = new XAttribute("RunAt", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                settings.Root.Add(runAttribute);
#if DEBUG
                Debug.Flush();
#endif
                foreach (XElement server in settings.Descendants("ReportServer"))
                {
                    string reportServerPath = server.Attribute("Path").Value;
                    string userName         = string.Empty;
                    string userPassword     = string.Empty;
                    if (server.Attribute("UserName") != null && server.Attribute("UserPassword") != null)
                    {
                        userName     = server.Attribute("UserName").Value;
                        userPassword = server.Attribute("UserPassword").Value;
                    }
                    HttpClientCredentialType reportHttpClientCredentialType = HttpClientCredentialType.Windows;
                    if (server.Attribute("HttpClientCredentialType") != null &&
                        server.Attribute("HttpClientCredentialType").Value != null &&
                        server.Attribute("HttpClientCredentialType").Value.ToLower() == "ntlm")
                    {
                        reportHttpClientCredentialType = HttpClientCredentialType.Ntlm;
                    }

                    //Console.WriteLine(string.Format("ReportServer: {0}; userName:{1}; userPassword:{2}", reportServerPath, userName, userPassword));
                    string           ParameterLanguage = (server.Attribute("ParameterLanguage") != null ? server.Attribute("ParameterLanguage").Value : "en-us").ToLower();
                    ReportServerMode reportServerMode  = (server.Attribute("Mode").Value == "Native" ? ReportServerMode.Native : ReportServerMode.SharePoint);

                    foreach (XElement folder in server.Descendants("Folder"))
                    {
                        string reportFolder = folder.Attribute("Name").Value;
                        //Console.WriteLine(string.Format("reportFolder: {0};", reportFolder));
                        foreach (XElement report in folder.Descendants("Report"))
                        {
                            string reportName = report.Attribute("Name").Value;
                            List <KeyValuePair <string, string> > reportParams = new List <KeyValuePair <string, string> >();
                            if (report.Element("Params") != null)
                            {
                                foreach (XElement param in report.Element("Params").Descendants("Param"))
                                {
                                    if (!string.IsNullOrEmpty(param.Attribute("Value").Value))
                                    {
                                        reportParams.Add(new KeyValuePair <string, string>(param.Attribute("Name").Value, param.Attribute("Value").Value));
                                    }
                                }
                            }
                            #region Render Report
                            if (report.Attribute("RenderFormat") != null) //&& report.Attribute("RenderPath") != null
                            {
                                //XML|CSV|IMAGE|PDF|EXCEL|WORD|HTML 4.0|MHTML|NULL http://msdn.microsoft.com/en-us/library/ms154606.aspx
                                string renderFormat = report.Attribute("RenderFormat").Value;
                                string renderPath   = (report.Attribute("RenderPath") != null ? report.Attribute("RenderPath").Value : string.Empty);

                                Render(reportServerPath, userName, userPassword, reportServerMode, reportHttpClientCredentialType, reportFolder, reportName, reportParams, ParameterLanguage, renderFormat, renderPath);
                            }
                            #endregion

                            #region linked reports
                            if (report.Element("LinkedReports") != null)
                            {
                                foreach (XElement linkedReport in report.Element("LinkedReports").Descendants("LinkedReport"))
                                {
                                    string linkedReportPath            = linkedReport.Attribute("Path").Value;
                                    ReportingService2010.Property prop = new ReportingService2010.Property();
                                    prop.Name = "Description";
                                    string linkedReportDescription = string.Empty;
                                    if (linkedReport.Attribute("Description") != null)
                                    {
                                        linkedReportDescription = linkedReport.Attribute("Description").Value;
                                    }
                                    prop.Value = linkedReportDescription;

                                    ReportingService2010.Property[] linkedReportProperties = new ReportingService2010.Property[1];
                                    linkedReportProperties[0] = prop;

                                    CreateLinkedReport(reportServerPath, userName, userPassword, reportServerMode, reportHttpClientCredentialType, reportFolder, reportName
                                                       , linkedReportPath
                                                       , linkedReportProperties
                                                       , linkedReport.Element("Params")
                                                       //, ParameterLanguage
                                                       );
                                }
                            }
                            #endregion

                            #region test cases
                            if (report.Element("TestCases") != null)
                            {
                                XElement xDocument = null;
                                Render(reportServerPath, userName, userPassword, reportServerMode, reportHttpClientCredentialType, reportFolder, reportName, reportParams, ParameterLanguage, out xDocument);
                                foreach (XElement test in report.Element("TestCases").Descendants("TestCase"))
                                {
                                    if (test.Attribute("Assert").Value == "IsNotNull")
                                    {
                                        string     value     = GetValue(xDocument, reportName, test.Attribute("Path").Value);
                                        XAttribute attribute = new XAttribute("Passed", ((!string.IsNullOrEmpty(value)).ToString()));
                                        test.Add(attribute);
                                    }
                                    else if (test.Attribute("Assert").Value == "AreEqual")
                                    {
                                        string     parentValue    = GetValue(xDocument, reportName, test.Attribute("Path").Value);
                                        string     childValue     = string.Empty;
                                        XAttribute attributeValue = test.Attribute("Value");
                                        if (attributeValue != null)
                                        {
                                            childValue = attributeValue.Value;
                                        }
                                        else
                                        {
                                            XElement childReport = test.Element("DrillDownReport");
                                            if (childReport != null)
                                            {
                                                List <KeyValuePair <string, string> > childReportParams = new List <KeyValuePair <string, string> >();
                                                foreach (XElement param in childReport.Element("Params").Descendants("Param"))
                                                {
                                                    childReportParams.Add(new KeyValuePair <string, string>(param.Attribute("Name").Value, param.Attribute("Value").Value));
                                                }
                                                XElement xChildDocument = null;
                                                Render(reportServerPath, userName, userPassword, reportServerMode, reportHttpClientCredentialType, reportFolder, childReport.Attribute("Name").Value, childReportParams, ParameterLanguage, out xChildDocument);
                                                childValue = GetValue(xChildDocument, childReport.Attribute("Name").Value, childReport.Attribute("Path").Value);
                                            }
                                        }
                                        XAttribute attribute = new XAttribute("Passed", (parentValue == childValue).ToString());
                                        test.Add(attribute);
                                    }
                                }
                                if (string.IsNullOrEmpty(ResultFilePath))
                                {
                                    ResultFilePath = AppDomain.CurrentDomain.BaseDirectory + "TestSuite " + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ".xml";
                                }
                            }
                            #endregion
                        }
                    }
                }
                if (!string.IsNullOrEmpty(ResultFilePath))
                {
                    try
                    {
                        settings.Save(ResultFilePath);
                    }
                    catch (IOException ex)
                    {
                        LogMessage("File " + ResultFilePath + " can not be saved. Error: " + ex.Message);
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex.Message + "  \n" + ex.StackTrace);
                throw;
            }
        }
Exemple #2
0
        public void Load(string SettingsFilePath, string ResultFilePath)
        {
            try
            {
                XDocument settings = new XDocument();
                if (File.Exists(SettingsFilePath))
                {
                    try
                    {
                        settings = XDocument.Load(SettingsFilePath);

                        XmlSchemaSet schemas = new XmlSchemaSet();
                        schemas.Add("", AppDomain.CurrentDomain.BaseDirectory + "Settings.xsd");

                        string validationErrorMessage = string.Empty;
                        settings.Validate(schemas, (o, ex) =>
                        {
                            validationErrorMessage = ex.Message;
                        });

                        if (!string.IsNullOrEmpty(validationErrorMessage))
                        {
                            LogMessage("File " + SettingsFilePath + " has incompartible schema structure. Error: " + validationErrorMessage);
                            throw new ApplicationException(validationErrorMessage);
                        }
                    }
                    catch (FileLoadException ex)
                    {
                        LogMessage("File " + SettingsFilePath + " was not found or not available. Error: " + ex.Message);
                        throw;
                    }
                }
                else
                {
                    try
                    {
                        settings = XDocument.Parse(SettingsFilePath); //try to load text as XML
                    }
                    catch (XmlException ex)
                    {
                        LogMessage("Settings argument is not valid xml: " + ex.Message);
                        throw;
                    }
                }

                var proc = new XProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"Settings.xsl\"");
                settings.Root.AddBeforeSelf(proc);
                XAttribute runAttribute = new XAttribute("RunAt", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                settings.Root.Add(runAttribute);
            #if DEBUG
                Debug.Flush();
            #endif
                foreach (XElement server in settings.Descendants("ReportServer"))
                {
                    string reportServerPath = server.Attribute("Path").Value;
                    string userName = string.Empty;
                    string userPassword = string.Empty;
                    if (server.Attribute("UserName") != null && server.Attribute("UserPassword") != null)
                    {
                        userName = server.Attribute("UserName").Value;
                        userPassword = server.Attribute("UserPassword").Value;
                    }
                    HttpClientCredentialType reportHttpClientCredentialType = HttpClientCredentialType.Windows;
                    if (server.Attribute("HttpClientCredentialType") != null
                        && server.Attribute("HttpClientCredentialType").Value != null
                        && server.Attribute("HttpClientCredentialType").Value.ToLower() == "ntlm")
                    {
                        reportHttpClientCredentialType = HttpClientCredentialType.Ntlm;
                    }

                    //Console.WriteLine(string.Format("ReportServer: {0}; userName:{1}; userPassword:{2}", reportServerPath, userName, userPassword));
                    string ParameterLanguage = (server.Attribute("ParameterLanguage") != null ? server.Attribute("ParameterLanguage").Value : "en-us").ToLower();
                    ReportServerMode reportServerMode = (server.Attribute("Mode").Value == "Native" ? ReportServerMode.Native : ReportServerMode.SharePoint);

                    foreach (XElement folder in server.Descendants("Folder"))
                    {
                        string reportFolder = folder.Attribute("Name").Value;
                        //Console.WriteLine(string.Format("reportFolder: {0};", reportFolder));
                        foreach (XElement report in folder.Descendants("Report"))
                        {
                            string reportName = report.Attribute("Name").Value;
                            List<KeyValuePair<string, string>> reportParams = new List<KeyValuePair<string, string>>();
                            if (report.Element("Params") != null)
                            {
                                foreach (XElement param in report.Element("Params").Descendants("Param"))
                                {
                                    if (!string.IsNullOrEmpty(param.Attribute("Value").Value))
                                    {
                                        reportParams.Add(new KeyValuePair<string, string>(param.Attribute("Name").Value, param.Attribute("Value").Value));
                                    }
                                }
                            }
                            #region Render Report
                            if (report.Attribute("RenderFormat") != null) //&& report.Attribute("RenderPath") != null
                            {
                                //XML|CSV|IMAGE|PDF|EXCEL|WORD|HTML 4.0|MHTML|NULL http://msdn.microsoft.com/en-us/library/ms154606.aspx
                                string renderFormat = report.Attribute("RenderFormat").Value;
                                string renderPath = (report.Attribute("RenderPath") != null ? report.Attribute("RenderPath").Value : string.Empty);

                                Render(reportServerPath, userName, userPassword, reportServerMode, reportHttpClientCredentialType, reportFolder, reportName, reportParams, ParameterLanguage, renderFormat, renderPath);
                            }
                            #endregion

                            #region linked reports
                            if (report.Element("LinkedReports") != null)
                            {
                                foreach (XElement linkedReport in report.Element("LinkedReports").Descendants("LinkedReport"))
                                {
                                    string linkedReportPath = linkedReport.Attribute("Path").Value;
                                    ReportingService2010.Property prop = new ReportingService2010.Property();
                                    prop.Name = "Description";
                                    string linkedReportDescription = string.Empty;
                                    if (linkedReport.Attribute("Description") != null)
                                    {
                                        linkedReportDescription = linkedReport.Attribute("Description").Value;
                                    }
                                    prop.Value = linkedReportDescription;

                                    ReportingService2010.Property[] linkedReportProperties = new ReportingService2010.Property[1];
                                    linkedReportProperties[0] = prop;

                                    CreateLinkedReport(reportServerPath, userName, userPassword, reportServerMode, reportHttpClientCredentialType, reportFolder, reportName
                                                , linkedReportPath
                                                , linkedReportProperties
                                                , linkedReport.Element("Params")
                                        //, ParameterLanguage
                                    );
                                }
                            }
                            #endregion

                            #region test cases
                            if (report.Element("TestCases") != null)
                            {
                                XElement xDocument = null;
                                Render(reportServerPath, userName, userPassword, reportServerMode, reportHttpClientCredentialType, reportFolder, reportName, reportParams, ParameterLanguage, out xDocument);
                                foreach (XElement test in report.Element("TestCases").Descendants("TestCase"))
                                {
                                    if (test.Attribute("Assert").Value == "IsNotNull")
                                    {
                                        string value = GetValue(xDocument, reportName, test.Attribute("Path").Value);
                                        XAttribute attribute = new XAttribute("Passed", ((!string.IsNullOrEmpty(value)).ToString()));
                                        test.Add(attribute);
                                    }
                                    else if (test.Attribute("Assert").Value == "AreEqual")
                                    {
                                        string parentValue = GetValue(xDocument, reportName, test.Attribute("Path").Value);
                                        string childValue = string.Empty;
                                        XAttribute attributeValue = test.Attribute("Value");
                                        if (attributeValue != null)
                                        {
                                            childValue = attributeValue.Value;
                                        }
                                        else
                                        {
                                            XElement childReport = test.Element("DrillDownReport");
                                            if (childReport != null)
                                            {
                                                List<KeyValuePair<string, string>> childReportParams = new List<KeyValuePair<string, string>>();
                                                foreach (XElement param in childReport.Element("Params").Descendants("Param"))
                                                {
                                                    childReportParams.Add(new KeyValuePair<string, string>(param.Attribute("Name").Value, param.Attribute("Value").Value));
                                                }
                                                XElement xChildDocument = null;
                                                Render(reportServerPath, userName, userPassword, reportServerMode, reportHttpClientCredentialType, reportFolder, childReport.Attribute("Name").Value, childReportParams, ParameterLanguage, out xChildDocument);
                                                childValue = GetValue(xChildDocument, childReport.Attribute("Name").Value, childReport.Attribute("Path").Value);
                                            }
                                        }
                                        XAttribute attribute = new XAttribute("Passed", (parentValue == childValue).ToString());
                                        test.Add(attribute);
                                    }
                                }
                                if (string.IsNullOrEmpty(ResultFilePath))
                                {
                                    ResultFilePath = AppDomain.CurrentDomain.BaseDirectory + "TestSuite " + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ".xml";
                                }
                            }
                            #endregion
                        }
                    }
                }
                if (!string.IsNullOrEmpty(ResultFilePath))
                {
                    try
                    {
                        settings.Save(ResultFilePath);
                    }
                    catch (IOException ex)
                    {
                        LogMessage("File " + ResultFilePath + " can not be saved. Error: " + ex.Message);
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex.Message + "  \n" + ex.StackTrace);
                throw;
            }
        }