2014年9月29日 星期一

健保雲端藥歷系統-1(WatiN)

衛生福利部的送的"大禮" ~ 健保雲端藥歷系統
經過各同仁一番努力之後,看來不得不將這套系統整合到自己的HIS內,所以參考了WatiN這套網頁測試工具,效果不錯但仍有些非同步的網頁的狀態判斷仍不夠好(只能拉長等待秒數,避免判斷錯誤)!

public partial class Form1 : Form
{
    private WatiN.Core.IE windowIE = null;
    private string entryURL = "https://10.253.253.245/imme0000/IMME0002S01.aspx";
    private string targetURL = "https://10.253.253.245/imme0000/IMME0002S02.aspx";
    private DateTime baseTime;
    private TimeSpan runTime;
    private TimeSpan timeOut = TimeSpan.FromSeconds(90);

    private void btnShow健保雲端藥歷系統_Click(object sender, EventArgs e)
    {
        try
        {
            StartCounting();

            baseTime = DateTime.Now;

            if (windowIE != null)
            {
                WatiN.Core.Button ContentPlaceHolder1_btnReSrc = windowIE.Button("ContentPlaceHolder1_btnReSrc");
                if (ContentPlaceHolder1_btnReSrc.Exists)
                {
                    ContentPlaceHolder1_btnReSrc.Click();
                }
            }
            else
            {
                windowIE = new WatiN.Core.IE(entryURL);
                windowIE.WaitForComplete((int)timeOut.TotalSeconds);
            }

            #region busy and timeout control
            while (((SHDocVw.InternetExplorer)(windowIE.InternetExplorer)).Busy)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(3000);

                runTime = DateTime.Now - baseTime;
                if (runTime > timeOut) break;
            }
            #endregion

            while (true)
            {
                #region https://10.253.253.245/imme0000/IMME0002S01.aspx
                if (windowIE.Url == entryURL)
                {
                    WatiN.Core.Span ContentPlaceHolder1_lblmsg = windowIE.Span("ContentPlaceHolder1_lblmsg");
                    if (ContentPlaceHolder1_lblmsg.Exists)
                    {
                        MessageBox.Show(ContentPlaceHolder1_lblmsg.OuterText);
                        break;
                    }
                }
                #endregion

                #region https://10.253.253.245/imme0000/IMME0002S02.aspx
                if (windowIE.Url == targetURL)
                {
                    WatiN.Core.Table ContentPlaceHolder1_gvList = windowIE.Table("ContentPlaceHolder1_gvList");
                    if (ContentPlaceHolder1_gvList.Exists)
                    {
                        if (dataGridView1.DataSource != null)
                        {
                            dataGridView1.DataSource = null;
                        }
                        else
                        {
                            dataGridView1.Rows.Clear();
                        }
                        dataGridView1.Refresh();
                        dataGridView1.DataSource = gvListHtml2DT(ContentPlaceHolder1_gvList);
                        break;
                    }

                    WatiN.Core.Span ContentPlaceHolder1_lblmsg = windowIE.Span("ContentPlaceHolder1_lblmsg");
                    if (ContentPlaceHolder1_lblmsg.Exists && ContentPlaceHolder1_lblmsg.OuterText.Contains("查無資料"))
                    {
                        if (dataGridView1.DataSource != null)
                        {
                            dataGridView1.DataSource = null;
                        }
                        else
                        {
                            dataGridView1.Rows.Clear();
                        }
                        dataGridView1.Refresh();
                        MessageBox.Show(ContentPlaceHolder1_lblmsg.OuterText);
                        break;
                    }
                }
                #endregion

                Application.DoEvents();
                System.Threading.Thread.Sleep(1000);

                runTime = DateTime.Now - baseTime;
                if (runTime > timeOut) break;
            }
        }
        catch (Exception ex)
        {
            Debug.Print(ex.Message);
            if (windowIE != null)
            {
                windowIE.Dispose();
            }
        }
        finally
        {
            StopCounting();
        }
    }

    private DataTable gvListHtml2DT(WatiN.Core.Table table)
    {
        DataTable dt = new DataTable();

        WatiN.Core.TableRowCollection rows = table.TableRows;
        WatiN.Core.TableRow row = rows[0];
        WatiN.Core.ElementCollection cols = row.Elements;

        StringCollection RowValues = new StringCollection();
        foreach (WatiN.Core.Element col in cols)
        {
            if (col.TagName == "TH") RowValues.Add(col.OuterText);
        }

        for (int j = 0; j < RowValues.Count; j++)
        {
            dt.Columns.Add(RowValues[j], Type.GetType("System.String"));
        }

        if (rows.Count > 1)
        {
            for (int i = 1; i < rows.Count; i++)
            {
                row = rows[i];
                cols = row.Elements;

                dt.Rows.Add();
                for (int j = 0; j < RowValues.Count; j++)
                {
                    dt.Rows[i - 1][j] = cols[j].OuterText;
                }
            }
        }

        return dt;
    }

    private void StartCounting()
    {
        ...
    }

    private void StopCounting()
    {
        ...
    }

}

沒有留言:

張貼留言