Test recorder的源码分析(2)

    技术2022-05-20  34

    WBEvaluteNewWindow

    调用方法cEXWB1_WBEvaluteNewWindow()

    点击在控件中打开一个新窗口,调用 方法wscript.AddPopup(),调用AddAction,将动作加入到列表中,调用m_frmPopup.SetURL(this, wscript, e.url),通过cEXWB1.Navigate(url)方法,在新窗口显示网页

     

    private void cEXWB1_WBEvaluteNewWindow(object sender,  csExWB.EvaluateNewWindowEventArgs e)

            {

                // modeless/modal flags=38

                if ((e.flags & NWMF.HTMLDIALOG)==NWMF.HTMLDIALOG)

                {

                    if (MessageBox.Show(Properties.Resources.showDialogBug,  Properties.Resources.KnownBug, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                    {

                        e.Cancel = true;

                    }

                }

                else

                {

                    m_frmPopup.Show();

                    //m_frmPopup.PopupName = wscript.AddPopup(wscript.settings.PopupIEName,  e.url);

                    wscript.AddPopup(wscript.Settings.PopupIEName, e.url,  m_frmPopup);

                    m_frmPopup.SetURL(this,  wscript,  e.url);

                }

            }

    public virtual void AddPopup(string ieName,  string url,  frmPopup popup)

            {

                var actpopup = new ActionOpenPopup(ieName,  url) {PopupForm = popup};

                AddAction(actpopup);

            }

    public void SetURL(frmMain form,  WatinScript script,  string url)

            {

                MainScript = script;

                MainForm = form;

                cEXWB1.Navigate(url);

            }

     

     

    WBKeyDown 调用方法cEXWB1_WBKeyDown

    处理控件中网页,输入框,除了删除,方向键以外的键盘事件, 先调用WriteKeys()方法,然后调用wscript.AddTyping(watinie,  textActiveElement)方法,做好通过AddAction方法,将动作加入列表

     

     

    WBKeyUp 调用方法cEXWB1_WBKeyUp

    处理方向键,tabenter,键的键盘事件, 先调用WriteKeys()方法,然后调用wscript.AddTyping(watinie, textActiveElement)方法,做好通过AddAction方法,将动作加入列表

     

    WBLButtonDown 调用方法cEXWB1_WBLButtonDown

    处理控件中的WBLButtonDown事件

     

    WBLButtonUp 调用方法cEXWB1_WBLButtonUp

    处理控件中的WBLButtonUp事件

     

    WBMouseMove 调用方法cEXWB1_WBMouseMove

    处理控件中的鼠标移动事件

     

    WBSecurityProblem 调用方法cEXWB1_WBSecurityProblem

    处理控件中的WBSecurityProblem事件

     

    WindowClosing调用方法cEXWB1_WindowClosing

    处理控件中的WindowClosing事件

     

    4、录制 接受的事件

    接受鼠标click事件调用方法tsbRecord_Click,设置录制标识(wscript.ShouldInsertActions = true, wscript.Recording=true),开始将所有的动作加入activeTest

    private void tsbRecord_Click(object sender,  EventArgs e)

            {

                if (txtTestName.Text.Trim() == "")

                {

                    MessageBox.Show(Properties.Resources.TestsMustHaveAName,  Properties.Resources.TestName,  MessageBoxButtons.OK,  MessageBoxIcon.Information);

                    return;

                }

                if (!System.Text.RegularExpressions.Regex.IsMatch(txtTestName.Text,  @"/A[a-zA-Z0-9_]+/z"))

                {

                    MessageBox.Show(Properties.Resources.TestNameCanContainOnlyAZAnd09,  Properties.Resources.TestName,  MessageBoxButtons.OK,  MessageBoxIcon.Information);

                    return;

                }

               

                tsbRecord.Enabled = false;

                tsbStop.Enabled = true;

                txtTestName.Enabled = false;

                wscript.ActiveTest.TestName = txtTestName.Text;

                wscript.Recording = true;

                wscript.ClearTimer();

     

                // inserting items into the current position

                if (wscript.ActiveTest.Count > 0 && gridSource.Selection.ActivePosition.Row+1<gridSource.RowsCount)

                {

                    wscript.InsertPosition = gridSource.Selection.ActivePosition.Row;

                    if (wscript.InsertPosition == -1) wscript.InsertPosition = 0;

                    wscript.ShouldInsertActions = true;

                }

                else wscript.ShouldInsertActions = false;

            }

     

     

    5、停止录制 接受的事件

    接受鼠标click事件,调用方法tsbStop_Click,设置录制标识为false

    private void tsbStop_Click(object sender,  EventArgs e)

            {

                tsbRecord.Enabled = true;

                tsbStop.Enabled = false;

                txtTestName.Enabled = true;

                wscript.Recording = false;

                for (int i = 0; i < wscript.ActiveTest.Count; i++)

                {

                    String strTmp = wscript.ActiveTest[i].ParentPage.Content;

                }

               

            }

     

    6、运行 接受的事件

    接受鼠标click事件,调用方法tsbRunCurrent_Click,调用wscript.RunTest(),调用ActiveTest.RunTest()

    在线程中执行,列表(ActiveTest)中的动作,action.Perform(),根据具体操作调用对应子类中的Perform()执行操作,比如回车事件调用ActionNavigate.csPerform(),鼠标点击事件, 通过ActionElementBase.cs中的GetElement()获取点击的是什么元素,然后调用ActionClick.cs中的Perform(),打开新窗口,调用ActionOpenWindow子类中的Perform();鼠标移动调用ActionMouse中的Perform()执行该动作

    public class RunThreaded

        {

            public Thread ThisThread;

            public ActionList Actions;

            public bool BreakpointSleep = true;

            public bool SingleStep = false;

            public ActionBase CurrentAction;

            public DataRow ReplacementRow;

     

            public delegate void ActionCounterIncrementedEvent(int index);

            public ActionCounterIncrementedEvent OnActionCounterIncremented;

     

            public delegate void RunCompleteEvent();

            public RunCompleteEvent OnRunCompleted;

     

            public delegate void BreakpointEvent(ActionBase action);

            public BreakpointEvent OnBreakpoint;

     

            public delegate void ActionStatusEvent(ActionBase action);

            public ActionStatusEvent OnActionStatus;

     

            public void RunTest()

            {

                if (Actions.ReplacementTable.Rows.Count == 0)

                {

                    ReplacementRow = null;

                    RunTestInstance(null);

                }

                else

                {

                    foreach (DataRow row in Actions.ReplacementTable.Rows)

                    {

                        ReplacementRow = row;

                        RunTestInstance(row);

                    }

                }

     

                if (OnRunCompleted != null) OnRunCompleted();

            }

     

            private void RunTestInstance(DataRow row)

            {

                int rowcounter = 1;

                foreach (ActionBase action in Actions)

                {

                    CurrentAction = action;

                    RunStep(action,  row);

                    rowcounter++;

                    if (OnActionCounterIncremented != null) OnActionCounterIncremented(rowcounter);

                }

            }

     

            private void RunStep(ActionBase action,  DataRow row)

            {

                if (SingleStep)

                {

                    BreakpointSleep = true;

                    while (BreakpointSleep)

                    {

                        Thread.Sleep(500);

                        Application.DoEvents();

                    }

                }

                else if (action.Breakpoint == ActionBase.BreakpointIndicators.ActiveBreakpoint)

                {

                    if (OnBreakpoint != null) OnBreakpoint(action);

                       

                    BreakpointSleep = true;

                    while (BreakpointSleep)

                    {

                        Thread.Sleep(500);

                        Application.DoEvents();

                    }

                }

     

                action.ReplacementRow = row;

                action.Perform();

                if (OnActionStatus != null) OnActionStatus(action);           

            }

    }


    最新回复(0)