Test recorder的源码分析(4)

    技术2022-05-20  37

    9、鼠标事件 HookMouseUp

    cEXWB1控件中打开的网页进行处理,调用wscript.AddClick对鼠标事件进行判断,在AddClick中调用AddAction将动作加入列表的操作

    void HookMouseUp(object sender,  Microsoft.Win32.MouseHookEventArgs e)

            {

                if (e.Control != null && e.Control.Name == "cEXWB1" && e.Button == MouseButtons.Right)

                {

                    IHTMLElement pelem = cEXWB1.GetActiveElement();

                    if (pelem != null && pelem.tagName.ToLower(CultureInfo.InvariantCulture) == "select")

                    {

                        //ShowPropertyWindow(pelem,  new Point(e.X,  e.Y));

                    }

                    return;

                }

     

                if (e.Button == MouseButtons.Right)

                {

                    return;

                }

     

                IHTMLElement activeElement;

                try

                {

                    activeElement = cEXWB1.GetActiveElement();

                }

                catch (Exception)

                {

                    return;

                }

     

     

                if (e.Control != null

                    && e.Control.Name == "cEXWB1"

                    && e.Control.Parent != null

                    && e.Control.Parent.Name == "frmMain"

                    && activeElement != null)

                {

                    // Freddy Guime

                    // We are doing this differently now.

                    // The issue being that clicks capture will not relate to the correct

                    // activeelement if a "change" on the activeelement has been made.

     

                    // 1) Figure out if there was an "active" element before.

     

                    // 2) Make Click record the "TextActiveElement"

                    if (activeElement != textActiveElement) WriteKeys();

                    textActiveElement = activeElement;

     

                    if (textActiveElement.tagName.ToLower(CultureInfo.InvariantCulture) == "body")

                    {

                        Point clientPoint = cEXWB1.PointToClient(new Point(e.X,  e.Y));

                        textActiveElement = cEXWB1.ElementFromPoint(true,  clientPoint.X,  clientPoint.Y);

                    }

                        /*

                    else if (textActiveElement.tagName.ToLower(System.Globalization.CultureInfo.InvariantCulture) == "object")

                    {

                        Point clientPoint = cEXWB1.PointToClient(new Point(e.X,  e.Y));

                        wscript.AddObjectClick(Watinie,  textActiveElement,  clientPoint);

                        return;

                    }

                     */

                    else

                    {

                        Point clientPoint = cEXWB1.PointToClient(new Point(e.X,  e.Y));

                        IHTMLElement eItem = cEXWB1.ElementFromPoint(true,  clientPoint.X,  clientPoint.Y);

                        if (eItem != textActiveElement

                            && eItem.GetType() != typeof (mshtml.HTMLFrameElementClass)

                            && eItem.GetType() != typeof (mshtml.HTMLIFrameClass)) textActiveElement = eItem;

                    }

     

                    wscript.AddClick(watinie,  textActiveElement);

                }

                else if (e.Control != null

                         && e.Control.Name == "cEXWB1"

                         && e.Control.Parent != null

                         && e.Control.Parent.Name == "frmPopup")

                {

                    if (m_frmPopup.GetActiveElement() != null)

                    {

                        wscript.AddClick(((frmPopup) e.Control.Parent).Watinie,  m_frmPopup.GetActiveElement());

                    }

                }

            }

    public virtual void AddClick(BrowserWindow browser,  IHTMLElement activeElement)

            {

                // don't add the click if we're just highlighting elements

                if (MainBrowser.highlightingElements) return;

     

                var action = new ActionClick(this);

                action.SetFindMethod(browser,  activeElement);

     

                if (action.ElementType == ElementTypes.FileUpload)

                {

                    WatchFileUploadBox(browser,  activeElement);

                }

                else if (action.ElementType == ElementTypes.RadioButton)

                {

                    var radio = new ActionRadio

                                    {

                                        ScriptCaller = this,

                                        FindMechanism = action.FindMechanism,

                                        Checked = (activeElement.outerHTML.ToLower().Contains("checked") == false)

                                    };

                    radio.SetFindMethod(browser,  activeElement);

                    AddAction(radio);

                }

                else if (action.ElementType == ElementTypes.CheckBox)

                {

                    var checkbox = new ActionCheckbox

                                       {

                                           ScriptCaller = this,

                                           FindMechanism = action.FindMechanism,

                                           Checked = (activeElement.outerHTML.ToLower().Contains("checked") == false)

                                       };

                    checkbox.SetFindMethod(browser,  activeElement);

                    AddAction(checkbox);

                }

                else if (action.ElementType == ElementTypes.SelectList

                    || action.ElementType == ElementTypes.Frame)

                {

                    // do nothing with the click

                }

                else

                {

                    AddAction(action);

                }           

            }

     

    10、保存 接受的事件

    接受鼠标click事件,调用方法tsbSave_Click(),调用save_code(),将列表中的动作,保存为xml文件

    private void tsbSave_Click(object sender,  EventArgs e)

            {

                if (wscript.Recording)

                {

                    tsbStop_Click(null,  e);

                }

     

                saveCodeDialog.Filter = "Native XML Format (*.xml)|*.xml|" + templatesAvailable.GetSaveFilter();

     

                if (saveCodeDialog.ShowDialog() != DialogResult.OK

                    || wscript.RecordedTests.Count == 0) return;

               

                // get the selected template

                if (saveCodeDialog.FilterIndex == 1) wscript.SaveXML(saveCodeDialog.FileName);

                else

                {

                    Template selectedTemplate = templatesAvailable.GetTemplateByIndex(saveCodeDialog.FilterIndex - 2);

                    wscript.SaveCode(saveCodeDialog.FileName,  selectedTemplate);

                }

            }

     

    public void SaveCode(string filename,  Template selectedTemplate)

            {

                string code = GetCodeFile(selectedTemplate);

                try

                {

                    File.WriteAllText(filename,  code);

                }

                catch (Exception ex)

                {

                    MessageBox.Show("Could not save code:" + Environment.NewLine + ex.Message,  "Save Failed",

                                    MessageBoxButtons.OK,  MessageBoxIcon.Error);

                }

               

            }

     

    11、打开 接受的事件

    鼠标click事件 调用方法tsbLoad_Click(),打开一个保存在本地xml文件,将里面的动作,显示在gridSource

     

    12Tsbrun 接受的事件

    鼠标click事件 调用方法tsbRun_Click(),步骤同运行按钮

     

    编辑

    接受鼠标click事件,调用方法tsbEditAction_Click(),打开编辑界面,对选定的动作进行编辑

     

    Clickelement

    调用方法tsbClickElement_CheckedChanged(),对选择的元素高亮显示

     

    EditDataSet

    调用方法tsbEditDataset_Click(),可以生成,编辑csv文件

     

    WATINELEMENTS

    接受鼠标的右键弹起事件调用方法treeWatin_MouseUp(),和左键的afterselect事件调用方法treeWatin_AfterSelect();通过HighlightElement(element)使选中的部分变亮

    private void HighlightElement(IHTMLElement element)

            {

                try

                {

                    if (lastelement != null)

                    {

                        lastelement.style.setAttribute("backgroundColor",  OriginalColor,  0);

                    }

     

                    if (element == null)

                    {

                        return;

                    }

     

                    lastelement = element;

                    Object objColor = lastelement.style.getAttribute("backgroundColor",  0);

                    OriginalColor = objColor != null ? objColor.ToString() : "";

                    lastelement.style.setAttribute("backgroundColor",  wscript.Settings.DOMHighlightColor.ToKnownColor(),  0);

                }

                catch (Exception)

                {

                    MessageBox.Show("Cannot highlight this element.",  "Highlight Error",  MessageBoxButtons.OK,  MessageBoxIcon.Error);

                }

            }

     

    HTMLDOM

    接受nodemouseclick事件,调用方法treeDOM_NodeMouseClick(),将选中的元素高亮显示


    最新回复(0)