⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webreplaydlg.cpp

📁 an automated software testing tool for Web applications
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 
// $Id: WebReplayDlg.cpp 27 2005-06-16 09:27:37Z wpc0756\Emmanuel $
//
// Copyright 2005 Emmanuel KARTMANN (emmanuel@kartmann.org)
//

#include "stdafx.h"
#include "WebReplay.h"
#include "WebReplayDlg.h"
#include "DlgProxy.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Timer identifier while waiting for HTML document be generated dynamically
#define CWEB_HTML_ELEMENT_TIMER 1


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CWebReplayDlg dialog




IMPLEMENT_DYNAMIC(CWebReplayDlg, CDialog);
CWebReplayDlg::CWebReplayDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWebReplayDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pAutoProxy = NULL;
  m_intTimeoutMilliseconds = 500;
  m_intMaxNumberOfTimeouts = 10;
}
CWebReplayDlg::~CWebReplayDlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
}

void CWebReplayDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_EXPLORER1, m_objExplorer);
  DDX_Control(pDX, IDC_SCENARIO_FILE_NAME, m_objScenarioFileName);
  DDX_Control(pDX, IDC_STATUSBAR, m_objStatusBar);
  DDX_Control(pDX, IDC_BUTTON_REPLAY, m_objReplayButton);
  DDX_Control(pDX, IDC_REPLAY_STATUS, m_objReplayStatus);
}

BEGIN_MESSAGE_MAP(CWebReplayDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_CLOSE()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
  ON_WM_TIMER()
  ON_BN_CLICKED(IDOK, OnBnClickedOk)
  ON_BN_CLICKED(IDC_BUTTON_REPLAY, OnBnClickedButtonReplay)
  ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
END_MESSAGE_MAP()


// CWebReplayDlg message handlers

BOOL CWebReplayDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon


  // Create XML DOM Document for Scenario File
  HRESULT hCreateResult = m_objXMLDoc.CreateInstance("msxml2.domdocument");
  if (FAILED(hCreateResult))
  {
    throw hCreateResult;
  }

  // Get configuration file name (WebReplay.ini)
  _TCHAR strConfigFileName[MAX_PATH];
  DWORD dwModuleFileName = GetModuleFileName(NULL, strConfigFileName, sizeof(strConfigFileName));
  if (dwModuleFileName == 0)
  {
    strcpy(strConfigFileName, _T(".\\WebReplay.ini"));
  }
  else
  {
    _TCHAR *pFileName = strrchr(strConfigFileName, '\\');
    if (pFileName)
    {
      strcpy(pFileName, _T("\\WebReplay.ini"));
    }
    else
    {
      strcpy(strConfigFileName, _T(".\\WebReplay.ini"));
    }
  }
  m_strConfigFile = strConfigFileName;


  // Build XML Scenario File Name
  _TCHAR strXMLFileName[MAX_PATH];
  DWORD dwLoaded = GetPrivateProfileString("WebReplay", "ScenarioFileName", NULL, strXMLFileName, sizeof(strXMLFileName), m_strConfigFile);

  if (dwLoaded == 0)
  {
    // Compute a default value (local directory plus xml file)
    DWORD dwCurrentDir = GetCurrentDirectory(sizeof(strXMLFileName), strXMLFileName);
    if (dwCurrentDir == 0)
    {
      strcpy(strXMLFileName, _T(".\\WebReplayScenario1.xml"));
    }
    else
    {
      // Concat file name
      strcat(strXMLFileName, _T("\\WebReplayScenario1.xml"));
    }
  }
  m_objScenarioFileName.SetWindowText(strXMLFileName);

  // Reset state/step/loop
  m_objExplorer.m_nScenarioLoops = 1;
  m_objExplorer.m_nScenarioStep = 0;
  m_objExplorer.m_nScenarioAction = 0;
	
  CWnd * pFocus = m_objScenarioFileName.SetFocus();
  m_objScenarioFileName.SetSel(0, -1);

  m_objReplayStatus.SetWindowText("Press ENTER or click on REPLAY button to start scenario.");

  return FALSE;  // return TRUE  unless you set the focus to a control
}

void CWebReplayDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWebReplayDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWebReplayDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CWebReplayDlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CWebReplayDlg::OnOK() 
{
	if (CanExit())
		CDialog::OnOK();
}

void CWebReplayDlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CWebReplayDlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}
BEGIN_EVENTSINK_MAP(CWebReplayDlg, CDialog)
  ON_EVENT(CWebReplayDlg, IDC_EXPLORER1, 259, DocumentCompleteExplorer1, VTS_DISPATCH VTS_PVARIANT)
  ON_EVENT(CWebReplayDlg, IDC_EXPLORER1, 102, StatusTextChangeExplorer1, VTS_BSTR)
END_EVENTSINK_MAP()

void CWebReplayDlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL)
{
  // Replay Scenario
  ReplayScenario();

  // TODO Implement recording of events to generate an XML scenario file
}

void CWebReplayDlg::StatusTextChangeExplorer1(LPCTSTR Text)
{
  if (Text != NULL)
  {
    m_objStatusBar.SetWindowText(Text);
    m_bstrStatusBarText = Text;
  }
}

void CWebReplayDlg::OnBnClickedOk()
{
  ResetScenario();

  ReplayScenario();
}

void CWebReplayDlg::OnTimer(UINT nIDEvent)
{
  if (nIDEvent == CWEB_HTML_ELEMENT_TIMER)
  {
    // Immediatly stop timer
    this->KillTimer(nIDEvent);

    if (m_intMaxNumberOfTimeouts-- > 0)
    {
      // Try to continue to replay scenario
      ReplayScenario();
    }
    else
    {
      // Error: too many timeouts - can't find requested control
      MessageBox("Error: too many timeouts - can't find requested HTML Element");
      m_objReplayStatus.SetWindowText("Error: too many timeouts - can't find requested HTML Element");

      // Enable "Replay" button
      m_objReplayButton.EnableWindow(TRUE);
      m_objScenarioFileName.EnableWindow(TRUE);

      // Put back focus on scenario file name (so that you can REPLAY again by pressing the ENTER key)
      CWnd * pFocus = m_objScenarioFileName.SetFocus();
      m_objScenarioFileName.SetSel(0, -1);

    }
  }

  CDialog::OnTimer(nIDEvent);
}


void CWebReplayDlg::ResetScenario()
{
  // Reset state - restart scenario from the beginning
  this->m_objExplorer.m_nScenarioLoops++;
  this->m_objExplorer.m_nScenarioStep = 0;
  this->m_objExplorer.m_nScenarioAction = 0;
  this->m_intMaxNumberOfTimeouts = 10;

  // UnLoad XML file
  if (m_objXMLDoc->documentElement)
  {
    m_objXMLDoc->removeChild(m_objXMLDoc->documentElement);
  }

}

void CWebReplayDlg::ReplayScenario()
{
  _TCHAR strReplayMessage[4096];
  if (this->m_objExplorer.m_nScenarioStep == -1)
  {
    EndScenario();
    return;
  }
  CString strScenarioFileName;
  m_objScenarioFileName.GetWindowText(strScenarioFileName);

  _variant_t varOut((bool)TRUE);
  if (m_objXMLDoc->documentElement)
  {
    // Already loaded - do nothing
  }
  else
  {
    // Load XML Scenario File
    _variant_t varXml(strScenarioFileName);
    varOut = m_objXMLDoc->load(varXml);
    if ((bool)varOut == FALSE)
    {
      MSXML2::IXMLDOMParseErrorPtr parseErrorPtr = m_objXMLDoc->parseError;
      _bstr_t bstrReason = parseErrorPtr->reason;
      ::MessageBox(NULL, bstrReason, _T("CAN'T LOAD XML FILE"), MB_OK);
    }
  }
  
  if ((bool)varOut == TRUE)
  {
    // DEBUG ::MessageBox(NULL, _bstr_t(m_objXMLDoc->xml), _T("Original Document"), MB_OK);

    // Check if loops are requested
    bool blnInfiniteLoops = false;
    int intLoops = 0;
    MSXML2::IXMLDOMNodePtr objXMLLoop = m_objXMLDoc->documentElement->selectSingleNode(_T("/web_replay/scenario/@loop"));
    if (objXMLLoop)
    {
      _bstr_t bstrLoop = objXMLLoop->text;
      if (bstrLoop == _bstr_t(_T("infinite")))
      {
        blnInfiniteLoops = true;
        intLoops = 0;
      }
      else
      {
        _TCHAR *strLoop = bstrLoop;
        intLoops = atol(strLoop);
      }
    }

    m_objReplayStatus.SetWindowText("Replaying scenario...");

    MSXML2::IXMLDOMNodeListPtr objXMLSteps = m_objXMLDoc->documentElement->selectNodes(_T("/web_replay/scenario/step"));
    for (long intStep = 0; intStep < objXMLSteps->length; intStep++)
    { 
      // Do not replay previous steps
      if (this->m_objExplorer.m_nScenarioStep <= intStep)
      {

        sprintf(strReplayMessage, "Replaying scenario step %d...", intStep);
        m_objReplayStatus.SetWindowText(strReplayMessage);

        MSXML2::IXMLDOMNodePtr objXMLStep = objXMLSteps->item[intStep];
        // DEBUG ::MessageBox(NULL, _bstr_t(objXMLStep->xml), _T("Current Step"), MB_OK);
        MSXML2::IXMLDOMNodeListPtr objXMLActions;
        objXMLActions = objXMLStep->selectNodes(_T("action"));

        if (objXMLActions->length > 0)
        {
          bool blnNextStep = false;
          for (long intAction = 0; intAction < objXMLActions->length; intAction++)
          {
            // If a specific action number was saved, restart at that action
            if (this->m_objExplorer.m_nScenarioAction <= intAction)
            {
              sprintf(strReplayMessage, "Replaying scenario step %d action %d...", intStep, intAction);
              m_objReplayStatus.SetWindowText(strReplayMessage);

              MSXML2::IXMLDOMNodePtr objXMLAction = objXMLActions->item[intAction];
              MSXML2::IXMLDOMNodePtr objXMLType = objXMLAction->selectSingleNode(_T("@type"));
              _bstr_t bstrActionType = objXMLType->text;
              if (bstrActionType == _bstr_t(_T("navigate")))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -