예제 #1
0
        /// <summary>
        /// Method parses the Object Repo and fetches the Locator Type and Value based on search parameters
        /// </summary>
        /// <param name="pageNodeName"></param>
        /// <param name="locatorName"></param>
        /// <returns></returns>
        public static string[] GetObjectRepo(string pageNodeName, string locatorName)
        {
            string[] objRepo    = new string[] { "", "" };
            string   pth        = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            string   actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
            string   repo_path  = new Uri(actualPath).LocalPath;
            //log.Info("Repo Path : "+ repo_path);
            XmlDocument doc = new XmlDocument();

            if (ConfigurationManager.AppSettings["platform"].ToLower() == "win")
            {
                doc.Load(repo_path.ToString() + "resources/or/Or_Web.xml");
            }
            else if (ConfigurationManager.AppSettings["platform"].ToLower() == "mobile")
            {
                if (ConfigurationManager.AppSettings["os"].ToLower().Equals("ios"))
                {
                    doc.Load(repo_path.ToString() + "resources/or/Or_iOS.xml");
                }
                else if (ConfigurationManager.AppSettings["os"].ToLower().Equals("android"))
                {
                    doc.Load(repo_path.ToString() + "resources/or/Or_Android.xml");
                }
            }
            XmlNodeList obj = doc.SelectNodes("//PageNode[@name='" + pageNodeName + "']//Object[@locatorname='" + locatorName + "']");

            int totalmatchingnodes = obj.Count;

            //log.Info("Total Matching Nodes : "+totalmatchingnodes);

            if (totalmatchingnodes == 1)
            {
                XmlElement objElement = obj.Item(0) as XmlElement;
                objRepo[0] = objElement.GetAttribute("locatortype");
                objRepo[1] = objElement.GetAttribute("locatorvalue");
                log.Info("locatortype : " + objRepo[0] + " , locatorvalue : " + objRepo[1]);
            }
            else if (totalmatchingnodes == 0)
            {
                Report.Fail("No matching node with PageNode : " + pageNodeName + " and LocatorName : " + locatorName);
                log.Info("No matching node with PageNode : " + pageNodeName + " and LocatorName : " + locatorName);
            }
            else
            {
                Report.Fail("There are multiple matching nodes with PageNode : " + pageNodeName + " and LocatorName : " + locatorName);
                log.Info("There are multiple matching nodes with PageNode : " + pageNodeName + " and LocatorName : " + locatorName);
            }
            return(objRepo);
        }
        /// <summary>
        /// Method reads data from CSV and adds it to a custom DataTable
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static DataTable ReadCsvFile(string filename)
        {
            Console.WriteLine("filename : " + filename);
            DataTable dt = new DataTable();
            string    lines;
            int       i          = 0;
            string    pth        = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            string    actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
            string    proj_path  = new Uri(actualPath).LocalPath;

            Console.WriteLine("Project Path : " + proj_path);
            try
            {
                using (StreamReader reader = new StreamReader(proj_path + "Resources\\Testdata\\" + filename + ".csv"))
                {
                    while (!reader.EndOfStream)
                    {
                        lines = reader.ReadLine();
                        //Console.WriteLine("FullText : " + lines);
                        //string[] rows = lines.Split(',');

                        //fulltext = reader.ReadToEnd().ToString();
                        //Console.WriteLine("FullText : "+fulltext);
                        //string[] rows = fulltext.Split('\n');
                        //Console.WriteLine("Rows len : "+rows.Length);
                        //for (int i = 0; i < rows.Count() - 1; i++)
                        //{
                        string[] rowValues = lines.Split(',');
                        if (i == 0)
                        {
                            for (int j = 0; j < rowValues.Count(); j++)
                            {
                                dt.Columns.Add(rowValues[j]);
                            }
                        }
                        else
                        {
                            DataRow dr = dt.NewRow();
                            for (int k = 0; k < rowValues.Count(); k++)
                            {
                                dr[k] = rowValues[k].ToString();
                            }
                            dt.Rows.Add(dr);
                        }
                        //}
                        i++;
                    }
                    //Console.WriteLine("Value of i : "+i);
                }
            }
            catch (FileNotFoundException e)
            {
                Report.Fail(e.Message);
            }
            return(dt);
        }
        /// <summary>
        /// Instantiates a browser based on the browser name
        /// </summary>
        /// <param name="platform"></param>
        /// <param name="browsername"></param>
        /// <param name="hostname"></param>
        /// <param name="portname"></param>
        /// <returns></returns>
        public static IWebDriver InitDriver(String platform, String browsername, string hostname, string portname)
        {
            try
            {
                string pth         = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
                string actualPath  = pth.Substring(0, pth.LastIndexOf("bin"));
                string projectPath = new Uri(actualPath).LocalPath;
                DesiredCapabilities capabilities = new DesiredCapabilities();
                if (platform.ToLower().Equals("win"))
                {
                    //KillDriverProcess();
                    if (ConfigurationManager.AppSettings["useseleniumgrid"].ToLower().Equals("true"))
                    {
                        switch (browsername.ToLower())
                        {
                        case "firefox":
                            capabilities.SetCapability("browserName", "firefox");
                            break;

                        case "chrome":
                            capabilities.SetCapability("browserName", "chrome");
                            break;

                        case "ie":
                            capabilities.SetCapability("browserName", "internet explorer");
                            break;
                        }
                        driver = new RemoteWebDriver(new Uri("http://" + hostname + ":" + portname + "/wd/hub"), capabilities);
                    }
                    else
                    {
                        switch (browsername.ToLower())
                        {
                        case "firefox":
                            processname = "geckodriver";
                            driver      = new FirefoxDriver();
                            break;

                        case "chrome":
                            ChromeOptions chromeOptions = new ChromeOptions();
                            //chromeOptions.AddArgument("-no-sandbox");
                            //chromeOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.Accept;
                            //processname = "chromedriver";
                            driver = new ChromeDriver();
                            break;

                        case "ie":
                            driver = new InternetExplorerDriver();
                            break;
                        }
                    }
                }
                else if (platform.ToLower() == "mobile" || platform.ToLower() == "mob")
                {
                    //process.StartInfo.FileName = "cmd";
                    //process.StartInfo.Arguments = "/c appium -a " + ConfigurationManager.AppSettings["address"] + " -p " + ConfigurationManager.AppSettings["port"];
                    //process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                    //process.Start();
                    //Thread.Sleep(6000);
                    //log.Info("Appium Server Started at URI: " + ConfigurationManager.AppSettings["appiumhub"]);

                    // Setting Capabilities for Appium
                    capabilities.SetCapability("platformName", GlobalVariables.os);
                    capabilities.SetCapability("platformVersion", ConfigurationManager.AppSettings["version"]);
                    capabilities.SetCapability("deviceName", ConfigurationManager.AppSettings["devicename"]);

                    if (ConfigurationManager.AppSettings["os"].ToLower() == "android")
                    {
                        //If commented, will use specified App Package and Activity else will install the apk present in the configured location
                        //capabilities.SetCapability("app", ConfigurationManager.AppSettings["apploc"]);
                        capabilities.SetCapability("appPackage", ConfigurationManager.AppSettings["apppackage"]);
                        capabilities.SetCapability("appActivity", ConfigurationManager.AppSettings["appactivity"]);
                        capabilities.SetCapability("autoGrantPermissions", true);
                        capabilities.SetCapability("automationName", "UIAutomator2");
                        log.Info("Android driver capability set");

                        driver = new AndroidDriver <AndroidElement>(new Uri("http://" + ConfigurationManager.AppSettings["address"] + ":" + ConfigurationManager.AppSettings["port"] + "/wd/hub"), capabilities, TimeSpan.FromMinutes(3));
                        log.Info("Android driver initiated");
                    }
                    else if (ConfigurationManager.AppSettings["os"].ToLower() == "ios")
                    {
                        capabilities.SetCapability("automationName", "XCUITest");
                        capabilities.SetCapability("app", ConfigurationManager.AppSettings["apploc"]);
                        capabilities.SetCapability("udid", ConfigurationManager.AppSettings["udid"]);
                        //capabilities.SetCapability("bundleId", ConfigurationManager.AppSettings["bundleid"]);
                        //capabilities.SetCapability("xcodeOrgId", ConfigurationManager.AppSettings["xcodeOrgId"]);

                        driver = new IOSDriver <IOSElement>(new Uri("http://" + ConfigurationManager.AppSettings["address"] + ":" + ConfigurationManager.AppSettings["port"] + "/wd/hub"), capabilities, TimeSpan.FromMinutes(3));
                        log.Info("Android driver initiated");
                    }
                    Console.WriteLine(driver);
                }
            }
            catch (WebDriverException wd)
            {
                Report.Fail(wd.Message);
                driver.Quit();
            }
            return(driver);
        }