Wednesday, 17 June 2020

key value pair

private static KeyValuePair<ArrayList, ArrayList> GetXmlKeyValuePairForRiva(string XmlFile)
        {
            ArrayList node = new ArrayList();
            ArrayList value = new ArrayList();
            try
            {
                Int32 count = 500;

                String[] spearator = { "</InputXml>" };
                String[] nodeValueSpearator = { "<CosemObject Name=" };
                String[] nodeSpearator = { "\" " };
                String[] valuesSeparator = { ">" };
                String[] valueSeparator = { "</" };
                XmlFile = File.ReadAllText(@"C:\Users\asamantr\OneDrive - Itron\Desktop\Automation\DeviceCommandExport.txt");

                String[] strList = XmlFile.Split(spearator, count, StringSplitOptions.RemoveEmptyEntries);
                String[] strNodeValueDetails = strList[1].Split(nodeValueSpearator, count, StringSplitOptions.RemoveEmptyEntries);
                node.Clear();
                value.Clear();
                for (int i = 1; i < strNodeValueDetails.Length; i++)
                {
                    string xmlNode = (strNodeValueDetails[i].Split(nodeSpearator, count, StringSplitOptions.RemoveEmptyEntries))[0].Replace("\"", string.Empty);

                    string stringValueDetails = (strNodeValueDetails[i].Split(nodeSpearator, count, StringSplitOptions.RemoveEmptyEntries))[3];
                    string stringValue = (stringValueDetails.Split(valuesSeparator, count, StringSplitOptions.RemoveEmptyEntries)).Length <= 3 ? "" : stringValueDetails.Split(valuesSeparator, count, StringSplitOptions.RemoveEmptyEntries)[3];

                    string xmlNodeValue = xmlNode == string.Empty || (stringValue.StartsWith("\r\n") || stringValue == string.Empty) ? "" : stringValue.Split(valueSeparator, count, StringSplitOptions.RemoveEmptyEntries)[0];

                    node.Add(xmlNode);
                    value.Add(xmlNodeValue);
                }
            }
            catch (Exception ex)
            {

            }
            return new KeyValuePair<ArrayList, ArrayList>(node, value);
        }

xml merge file

private static void MergeTwoXmlFile(string xmlFile1, string xmlFile2, string mergeFileNameLocation)
        {
            try
            {
                IList<string> duplicateNodes = new List<string>();

                xmlFile1 = @"C:\Ashutosh\Automation\xml files\CellularCheck_BaseLine.xml";
                xmlFile2 = @"C:\Ashutosh\Automation\xml files\CellularCheck_BaseLine_File.xml";
                mergeFileNameLocation = @"C:\Users\asamantr\Source\Repos\ConsoleApp4\ConsoleApp4\Temp";

                XmlDocument doc1 = new XmlDocument();
                XmlDocument doc2 = new XmlDocument();

                XElement rootFile1 = XElement.Load(xmlFile1);
                XElement rootFile2 = XElement.Load(xmlFile2);

                if (File.Exists("" + mergeFileNameLocation + "\\" + "Test_CellularCheck_BaseLine.xml"))
                    File.Delete("" + mergeFileNameLocation + "\\" + "Test_CellularCheck_BaseLine.xml");
                if (!Directory.Exists(mergeFileNameLocation))
                    Directory.CreateDirectory(mergeFileNameLocation);


                doc1.LoadXml(rootFile1.ToString());
                doc2.LoadXml(rootFile2.ToString());
                XmlNodeList list1 = doc1.GetElementsByTagName("root");
                XmlNodeList list2 = doc2.GetElementsByTagName("root");

                foreach (XmlNode node1 in list1)
                {
                    foreach (XmlNode childNode1 in node1.ChildNodes)
                    {
                        foreach (XmlNode node2 in list2)
                        {
                            foreach (XmlNode childNode2 in node2.ChildNodes)
                            {
                                if (childNode1.Name == childNode2.Name)
                                {
                                    duplicateNodes.Add(childNode1.Name);
                                }
                            }
                        }
                    }
                }

                for (int i = 0; i < duplicateNodes.Count(); i++)
                    rootFile2.Element(duplicateNodes[i]).Remove();

                rootFile1.Add(rootFile2.Elements());
                rootFile1.Save(@"C:\Users\asamantr\Source\Repos\ConsoleApp4\ConsoleApp4\Temp\Test_CellularCheck_BaseLine.xml");
            }
            catch (Exception ex)
            {
                //return false;
                //throw;
            }
        }

xml compare file

public bool CompareXml(string SourceFile, string DestinationFile, string[] SkipValues = null, string DiffFileNameWithPath = null)
        {
            try
            {
                bool bIdentical = false;
                //SourceFile = @"C:\FDMXml\NewFile\100W_CheckEndpointOperation_13_27_38.xml";
                //DestinationFile = @"C:\FDMXml\OldFile\100W_CheckEndpointOperation_13_27_38.xml";
                //SkipValues = XmlSkipValues.XmlSkipValues100W;

                string tempFile1 = @"C:\temp\tempfile1.xml";
                string tempFile2 = @"C:\temp\tempfile2.xml";

                XElement rootFile1 = XElement.Load(SourceFile);
                XElement rootFile2 = XElement.Load(DestinationFile);

                for (int i = 0; i < SkipValues.Length; i++)//only hot fix operations
                {
                    rootFile1.Element(SkipValues[i]).Remove();
                    rootFile2.Element(SkipValues[i]).Remove();
                }
                if (File.Exists(@"C:\temp\tempfile1.xml"))
                    File.Delete(tempFile1);
                if (File.Exists(@"C:\temp\tempfile2.xml"))
                    File.Delete(tempFile2);

                rootFile1.Save(tempFile1);
                rootFile2.Save(tempFile2);

                if (String.IsNullOrEmpty(DiffFileNameWithPath))
                    DiffFileNameWithPath = "Diff";
                string diffPath = @"C:\FDMXml\" + DiffFileNameWithPath + ".xml";
                if (!Directory.Exists(DiffFileNameWithPath))
                    Directory.CreateDirectory(DiffFileNameWithPath);

                StringBuilder differenceStringBuilder = new StringBuilder();

                using (FileStream fs = new FileStream(diffPath, FileMode.Create))
                {
                    XmlWriter diffGramWriter = XmlWriter.Create(fs);
                    //XmlDiff diff = new XmlDiff();
                    XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
                                            XmlDiffOptions.IgnoreNamespaces |
                                            XmlDiffOptions.IgnorePrefixes);
                    bIdentical = xmldiff.Compare(tempFile1, tempFile2, false, diffGramWriter);

                    diffGramWriter.Close();
                }

                File.Delete(tempFile1);
                File.Delete(tempFile2);

                StaticTestData.xmlSourceFileName = string.Empty;
                return bIdentical;
            }
            catch (Exception ex)
            {
                return false;
                //throw;
            }
        }

Friday, 17 April 2020

string split

Int32 count = 500;
            String[] spearator = { "=","\""};
            String[] strlist = strMyXml.Split(spearator, count,StringSplitOptions.RemoveEmptyEntries);

Thursday, 16 April 2020

xml Create xml file


public static void CreateXmlFile(string FolderName, string FileName, string strMyXml)
        {
            FolderName = "100G";
            FileName = "GridData";
            strMyXml = "<Employee>" +
                                   "<firstName>" + "Santosh" + "</firstName>" +
                                   "<lastName>" + "Sahoo" + "</lastName>" +
                        "</Employee>";

            string path = @"C:\FDMXml\" + FolderName + "";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(strMyXml);
            doc.Save(path + "\\" + "" + FileName + "" + ".xml");
        }

Friday, 23 August 2019

Automation Selenium Take a Screen Shot

 public void TakeScreenshot(string fileName, string screenshotPath = null, IWebDriver driver = null)
        {
            Screenshot s = ((ITakesScreenshot)driver).GetScreenshot();//driver = new ChromeDriver();
            var path = @"C:\Ashutosh";

            if (!string.IsNullOrEmpty(screenshotPath))
            {
                path = screenshotPath;
            }

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            s.SaveAsFile(Path.Combine(screenshotPath, fileName + DateTime.Now.ToString("MM-dd-yyyy-HHmmss")), ScreenshotImageFormat.Png);
        }

Wednesday, 14 August 2019

Automation Selenium

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace AshuPractice
{
    [TestClass]
    public class FirstTestClass
    {
        IWebDriver driver;

        [TestMethod]
        public void ChromeMethod()
        {
            string actualResult;
            string expectedResult = "Google";
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://google.co.in/");
            driver.Manage().Window.Maximize();
            actualResult = driver.Title;
            if (actualResult.Contains(expectedResult))
            {
                Debug.WriteLine("Test Case Passed");
                Assert.IsTrue(true, "Test Case Passed");
            }
            else
            {
                Debug.WriteLine("Test Case Failed");
            }
            Thread.Sleep(2000);
            driver.Close();
            driver.Quit();
        }
        [TestMethod]
        public void WikiSearch()
        {
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.wikipedia.org/");
            driver.Manage().Window.Maximize();
            driver.FindElement(By.XPath("//*[@id='searchInput']")).SendKeys("selenuim");
            driver.FindElement(By.XPath("//*[@id='search-form']/fieldset/button/i")).Click();
            Thread.Sleep(2000);
            driver.Close();
            driver.Quit();
        }
        [TestMethod]
        public void ReadOnlyCollection()
        {
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.wikipedia.org/");
            driver.Manage().Window.Maximize();

            //ReadOnlyCollection<IWebElement> anchorLists = driver.FindElements(By.TagName("a"));
            IList<IWebElement> anchorLists = driver.FindElements(By.TagName("a"));
            foreach (var item in anchorLists)
            {
                if (item.Text != "")
                    if (item.Text.Contains("English"))
                    {
                        item.Click();
                        break;
                    }
            }

            Thread.Sleep(2000);
            driver.Close();
            driver.Quit();
        }
        [TestMethod]
        public void GetDropDownValue()
        {
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.wikipedia.org/");
            driver.Manage().Window.Maximize();

            IList<IWebElement> languages = new List<IWebElement>();
            languages = (driver.FindElement(By.XPath("//*[@id='searchLanguage']"))).FindElements(By.TagName("option"));
            foreach (var language in languages)
            {
                if (language.Text == "Dansk")
                {
                    SelectElement selectElement = new SelectElement(driver.FindElement(By.XPath("//*[@id='searchLanguage']")));
                    selectElement.SelectByText(language.Text);
                    break;
                }
            }
            Thread.Sleep(2000);
            driver.Close();
            driver.Quit();
        }
        [TestMethod]
        public void TwitterLogin()
        {

        }
    }   
}