private int forceFields(TempStep step)
        {
            int returnValue = 10;

            switch (step.extension)
            {
            case "Navigate": returnValue = 0; break;

            case "Find":
            case "Wait": if (step.action == "SendKeys")
                {
                    returnValue = 1;
                }
                else if (step.action == "Click")
                {
                    returnValue = 2;
                }
                break;
            }
            return(returnValue);
        }
        private void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to remove this element?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                int      x  = -1;
                TempStep ts = new TempStep();
                Case     cs = new Case();
                foreach (Case c in testCases)
                {
                    if (c.selected)
                    {
                        cs = c;
                        foreach (TempStep s in c.steps)
                        {
                            if (s.isActive)
                            {
                                ts = s;
                                int start = listBox1.Items.IndexOf("Case: " + c.name);
                                int end   = listBox1.Items.IndexOf("Case: " + testCases.ElementAt(testCases.IndexOf(c) + 1).name);
                                for (int o = start; o < end; o++)
                                {
                                    int u = listBox1.Items[o].ToString().IndexOf(':');

                                    if (listBox1.Items[o].ToString().Substring(u + 1).Trim() == s.name.Trim())
                                    {
                                        x = o;
                                    }
                                }
                            }
                        }
                    }
                }
                cs.steps.Remove(ts);
                listBox1.Items.RemoveAt(x);
                listBox1.Refresh();
            }
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.GetItemText(listBox1.SelectedItem).Contains("Case:"))
            {
                NewStepPanel.Visible = false;
                ParamPanel.Visible   = false;
                TextBoxCaseName.Text = testCases.Find(t => t.name.Trim() == (listBox1.GetItemText(listBox1.SelectedItem).Split(':').LastOrDefault().Trim())).name;
            }
            else if (listBox1.GetItemText(listBox1.SelectedItem).Contains("Param:"))
            {
                NewStepPanel.Visible = false;
                ParamPanel.Visible   = true;
                Case  c = new Case();
                param p = new param();

                foreach (Case y in testCases)
                {
                    foreach (param o in y.parameters)
                    {
                        if (o.name.Trim() == listBox1.GetItemText(listBox1.SelectedItem).Split(':').LastOrDefault().Trim())
                        {
                            o.isActive = true;
                            p          = o;
                            c          = y;
                        }
                    }
                }
                TextBoxCaseName.Text = c.name;
                textBox2.Text        = p.name;
                textBox3.Text        = p.file;
                textBox1.Text        = p.value;
                checkedListBox1.SetItemChecked(checkedListBox1.Items.IndexOf(p.type), true);
            }
            else if (listBox1.GetItemText(listBox1.SelectedItem).Contains("Step:"))
            {
                NewStepPanel.Visible = true;
                ParamPanel.Visible   = false;
                Case     c = new Case();
                TempStep s = new TempStep();
                foreach (Case y in testCases)
                {
                    if (y.selected == true)
                    {
                        //y.selected = false;
                        foreach (TempStep o in y.steps)
                        {
                            int commaIn = listBox1.GetItemText(listBox1.SelectedItem).IndexOf(':');
                            o.isActive = false;
                            if (o.name.Trim() == listBox1.GetItemText(listBox1.SelectedItem).Substring(commaIn + 1).Trim())
                            {
                                //y.selected = true;
                                o.isActive = true;
                                s          = o;
                                c          = y;
                            }
                        }
                    }
                }

                ListBoxExtension.SetItemChecked(ListBoxExtension.Items.IndexOf(s.extension), true);
                TextBoxCaseName.Text = c.name;
                TextBoxValue.Text    = "";
                TextBoxElement.Text  = "";
                switch (forceFields(s))
                {
                case 0: TextBoxValue.Text = s.value;
                    fieldManager(2, 10);
                    break;

                case 1:
                case 2: TextBoxElement.Text = s.element;
                    TextBoxElement.Text     = s.element;
                    ListBoxAction.SetItemChecked(ListBoxAction.Items.IndexOf(s.action), true);
                    if (ListBoxAction.Items.IndexOf(s.action) != 0)
                    {
                        fieldManager(1, 1);
                        TextBoxValue.Text = s.value;
                    }
                    else
                    {
                        fieldManager(1);
                    }
                    break;

                case 10:
                    TextBoxElement.Text = s.element;
                    fieldManager(0, 2);
                    if (s.action != "")
                    {
                        ListBoxAction.SetItemChecked(ListBoxAction.Items.IndexOf(s.action), true);
                    }
                    ;
                    break;

                default:
                    TextBoxElement.Text = s.element;
                    fieldManager(1, 1);
                    ListBoxAction.SetItemChecked(ListBoxAction.Items.IndexOf(s.action), true);
                    //TextBoxValue.Text = s.value;
                    break;
                }
            }
        }
        private void GenerateFromFile()
        {
            string filePath = TextBoxFilePath.Text;

            BL.FunctionClass  fc       = new BL.FunctionClass();
            XPathNavigator    nav      = fc.XpathNavigator(filePath);
            XPathExpression   expr     = nav.Compile("//cases/case");
            XPathNodeIterator iterator = nav.Select(expr);

            testCases.Clear();
            while (iterator.MoveNext())
            {
                Case c = new Case
                {
                    name       = iterator.Current.SelectSingleNode("@name").Value,
                    selected   = false,
                    parameters = new List <param>(),
                    steps      = new List <TempStep>()
                };
                XPathExpression   paramExpr = iterator.Current.Compile("//case[@name='" + c.name + "']/param");
                XPathNodeIterator paramIt   = iterator.Current.Select(paramExpr);

                int k = 0;
                while (paramIt.MoveNext())
                {
                    param p = new param
                    {
                        name     = paramIt.Current.SelectSingleNode("@name").Value,
                        isActive = false,
                        type     = paramIt.Current.SelectSingleNode("@type").Value,
                        value    = paramIt.Current.SelectSingleNode("@value").Value
                    };
                    if (p.type == "xPath")
                    {
                        p.file = paramIt.Current.SelectSingleNode("@file").Value;
                    }
                    c.parameters.Add(p);
                    k++;
                }

                XPathExpression   stepExpr = iterator.Current.Compile("//case[@name='" + c.name + "']/step");
                XPathNodeIterator stepIt   = iterator.Current.Select(stepExpr);

                k = 0;
                while (stepIt.MoveNext())
                {
                    TempStep s = new TempStep()
                    {
                        isActive  = false,
                        extension = stepIt.Current.SelectSingleNode("@extension").Value,
                        value     = "",
                        element   = ""
                    };
                    s.name = s.extension;

                    if (s.extension != "Navigate")
                    {
                        s.element = stepIt.Current.SelectSingleNode("@element").Value;
                        s.action  = stepIt.Current.SelectSingleNode("@action").Value;

                        s.name += (" | " + s.element + " | " + s.action);
                        if (s.action == "SendKeys")
                        {
                            s.value = stepIt.Current.SelectSingleNode("@value").Value;
                        }
                    }
                    else
                    {
                        s.value = stepIt.Current.SelectSingleNode("@value").Value;
                        s.name += (" | " + s.value);
                    }

                    k++;
                    c.steps.Add(s);
                }
                testCases.Add(c);
            }
            showListboxItems();
        }
        private void SaveStep_Click(object sender, EventArgs e)
        {
            if (label2.Text.ToLower().Contains("attribute"))
            {
                label2.Text = "Value:";
                label1.Text = "Element:";
                Check ch = new Check(TextBoxElement.Text, TextBoxElement.Text + TextBoxValue.Text, TextBoxValue.Text);
                CheckList.Add(ch);
            }
            else
            {
                if (ListBoxExtension.CheckedItems.Count == 0)
                {
                    MessageBox.Show("Extension not filled in!");
                }
                TempStep t = new TempStep()
                {
                    extension = ListBoxExtension.GetItemText(ListBoxExtension.CheckedItems[0]),
                    action    = (ListBoxAction.CheckedItems.Count == 0)? "" : ListBoxAction.GetItemText(ListBoxAction.CheckedItems[0]),
                };
                switch (forceFields(t))
                {
                case 0:
                    t.value = TextBoxValue.Text;
                    break;

                case 1:
                    t.element = TextBoxElement.Text;
                    t.value   = TextBoxValue.Text;
                    break;

                case 2:
                    t.element = TextBoxElement.Text;
                    t.value   = TextBoxValue.Text;
                    break;
                }
                t.name = t.extension;
                if (t.extension != "Navigate")
                {
                    t.name += (" | " + t.element + " | " + t.action);
                }
                else
                {
                    t.name += (" | " + t.value);
                }

                foreach (Case c in testCases)
                {
                    if (c.selected)
                    {
                        if (c.steps.Exists(p => p.name == t.name))
                        {
                            int i = c.steps.IndexOf(c.steps.Find(p => p.name == t.name));
                            c.steps[i] = t;
                        }
                        else
                        {
                            c.steps.Add(t);
                        }
                    }
                }
            }
            showListboxItems();
        }