阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

手机号码,快捷登录

老司机
查看: 1023|回复: 0

VA单点登录脚步实施实例之用友U8_11.0

[复制链接]
  • TA的每日心情
    郁闷
    2016-4-21 20:05
  • 签到天数: 16 天

    [LV.4]偶尔看看III

    发表于 2013-11-12 16:18:32 | 显示全部楼层 |阅读模式
    于被实施程序分为多个不同的版本,自动登录脚本会有小范围修正.

    程序: 用友U8
    版本:11.0

    uses uApp,Sysutils,Windows, Dialogs;

    function GetLang(const InValue:string):string;
    begin
      Result := '';
      if InValue = 'CHT' then Result := '{DOWN}'
      else if InValue = 'ENU' then Result := '{DOWN}{DOWN}';
    end;

    function GetAccount(const InValue:string):string;
    var i :integer;
    begin
      Result := '';
      for i := 1 to StrToInt(InValue) - 1 do begin
        Result := Result + '{DOWN}';
      end;
    end;

    procedure ErrorLog(const ErrorMsg:string);
    begin
    App.DebugLog(ErrorMsg);
    end;


    function InputText(const aMode: Integer; const aTargetWin: DWORD; const aText: string): Boolean;
    var
      s: string;
    begin
      Result := True;
      s := aText;
      Case aMode of
        1 : begin
         uApp.SetTextByWM_SetText(aTargetWin, s);
        end;
        2 : begin
          uApp.SelectAllByEM_SETSEL(aTargetWin); //Select All
          uApp.SetTextByClipBoard(aTargetWin, s);   
        end;
        3 : begin
          uApp.ClearTextByWM_CharAndKey(aTargetWin, 20);
          uApp.SetTextByWM_Char(aTargetWin, s);   
        end;
        4 : begin
          s := uApp.Str2Macrostr(s);
          uApp.SetFocusByAttachThread(aTargetWin, 5000);
          uApp.SetTextByKeyEvent(aTargetWin, s);   
        end;
       else
         Result := false;
       end;
    end;

    procedure SetComboboxIndexByKB(const aHandle : DWORD;const aIndex : Integer);
    var
      i,j,curIndex : Integer;
    begin
      for i := 1 to 3 Do begin

        curIndex := Windows.SendMessage(aHandle,messages.CB_GETCURSEL,0,0);
        if aIndex > curIndex then begin

          for j := 1 to (aIndex-curIndex) Do begin
            Windows.PostMessage(aHandle,Messages.WM_KEYDOWN,windows.VK_DOWN,0);
            Windows.PostMessage(aHandle,Messages.WM_KEYUP,windows.VK_DOWN,0);
            Windows.Sleep(10) ;

          end;
        end else if aIndex < curIndex then begin
          for j := 1 to (curIndex-aIndex) Do begin
            Windows.PostMessage(aHandle,Messages.WM_KEYDOWN,windows.VK_UP,0);
            Windows.PostMessage(aHandle,Messages.WM_KEYUP,windows.VK_UP,0);
            Windows.Sleep(10) ;

          end;
        end
        else begin
          Exit;
        end;
        Windows.Sleep(666);
      end;
    end;

    function ChildWindowCount : Integer;
    var iCount : Integer;
    begin
      Result := 0; iCount := 0;
      while true Do begin
        if iCount > 5 then Exit;
        Result := uApp.GetChildWinCount(Alp.TopWinInfo.WinHandle,true);
        if (Result < Alp.TopWinInfo.ChildWinCount) then begin
          iCount := iCount + 1;
          Windows.Sleep(50);
          continue;
        end
        else begin
          Exit;
        end;
      end;
    end;

    function ChildWinCountIs19(const WaiteTimeOut:DWORD):Boolean;
    var
      iWinCount,i : Integer;
      FirstTick: LongWord;  
    begin
      Result := False;
      FirstTick := windows.GetTickCount;
      iWinCount := ChildWindowCount;
      while True do begin
        if ((windows.GetTickCount - FirstTick) > WaiteTimeOut) then Break;
        if iWinCount < Alp.TopWinInfo.ChildWinCount then Sleep(200)
        else begin  Result := True;    Break;   end;
        iWinCount := ChildWindowCount;  
      end;
    end;

    function InitEditorParams : Boolean;
    begin
      Result := false;  
      //----------Username Window
      ALP.Editor_Username.WinIndex := 17;
      ALP.Editor_Username.InputMode := 1;
      ALP.Editor_Username.WinHandle := uApp.FindWinByThCi(ALP.TopWinInfo.WinHandle, ALP.Editor_Username.WinIndex, True);  
    //----------Password Window
      ALP.Editor_Password.WinIndex := 7;
      ALP.Editor_Password.InputMode := 1;
      ALP.Editor_Password.WinHandle := uApp.FindWinByThCi(ALP.TopWinInfo.WinHandle, ALP.Editor_Password.WinIndex, True);
    //--------Custom Widnow
      ALP.Editor[0].WinIndex := 35;  //Login Server Name
      ALP.Editor[0].InputMode := 1;
      ALP.Editor[0].WinHandle := uApp.FindWinByThCi(ALP.TopWinInfo.WinHandle, ALP.Editor[0].WinIndex, True);
      ALP.Editor[1].WinIndex := 20; //Account
      ALP.Editor[1].WinHandle := uApp.FindWinByThCi(ALP.TopWinInfo.WinHandle, ALP.Editor[1].WinIndex, True);
      ALP.Editor[2].WinIndex := 1; //Language
      ALP.Editor[2].WinHandle := uApp.FindWinByThCi(ALP.TopWinInfo.WinHandle, ALP.Editor[2].WinIndex, True);
      ALP.Editor[3].WinIndex := 12; //OpDate
      ALP.Editor[3].InputMode := 1;
      ALP.Editor[3].WinHandle := uApp.FindWinByThCi(ALP.TopWinInfo.WinHandle, ALP.Editor[3].WinIndex, True);
    //----------Click LoginButton
      ALP.LoginButton.WinIndex := 42;
      if ALP.LoginButton.WinIndex = 0 then
        ALP.LoginButton.WinHandle := ALP.TopWinInfo.WinHandle
      else if ALP.LoginButton.WinIndex > 0 then
        ALP.LoginButton.WinHandle := uApp.FindWinByThCi(ALP.TopWinInfo.WinHandle, ALP.LoginButton.WinIndex, True);
      ALP.LoginButton.ClickMode := 1;

      Result := True;  
    end;

    procedure AutoLogin;
    type TMouseButton = (mbLeft, mbRight, mbMiddle);
    var
      htop, hchild: DWORD;
      b: boolean;
      i,j,k: Integer;
      s, s1,s2,s3,s4,s5, s6, sDel, sEnter : string;
    begin
      if uApp.CAppHelperVersion < 110126 then begin
        ErrorLog('Current Script Engine Is Low!');
        Exit;
      end;
      if ALP = nil then begin
        ErrorLog('Current Script Engine Is Low!' );
        Exit;
      end;
    //--------- Set Autologin Params
    //----------Find Top Window
      ALP.TopWinInfo.WinClass := 'WindowsForms10.Window.8.app*';//'WindowsForms10.Window.8.app.0.2e0c681';
      ALP.TopWinInfo.WinTitle := '登 錄|登录|Login'; //*AB*,*AB,AB*,AB,AB|cd*|*Ef*
      Alp.TopWinInfo.FindTimeout := 5000;
      ALP.TopWinInfo.ChildWinCount := 43;

    //----------1 Wait Target Application Idle
      ALP.TopWinInfo.IdleTimeout :=  5000;

    //-----------Find Top Window
      htop := uApp.FindMatchWinWithTimeout(ALP.ProcessID, ALP.TopWinInfo.WinClass,ALP.TopWinInfo.WinTitle,Alp.TopWinInfo.FindTimeout);
      if (htop <= 0) then begin
        ErrorLog('Not Find Top Window!');
        Exit;
      end;
      ALP.TopWinInfo.WinHandle := htop;

      //-----------1 Wait Target Application Idle
      b := uApp.CheckWindowProcessIsIdle(htop, ALP.TopWinInfo.IdleTimeout);
      if b = false then begin
        s := Sysutils.IntToStr(ALP.TopWinInfo.IdleTimeout);
        ErrorLog('Target Application Wait '+s+'s,Still Not Idle!');
        Exit;
      end;

    //------Login ServerName
      ALP.Editor[0].WinIndex := 35;
      hchild := uApp.FindWinByThCi(ALP.TopWinInfo.WinHandle, ALP.Editor[0].WinIndex, True);
      if hchild <= 0 then begin
        s := Sysutils.IntToStr(ALP.Editor[0].WinIndex);
        ErrorLog('Not Find Login ServerName Window Index:'+s+' !');
        Exit;
      end;

    //-----输入信息

      uApp.SetFocusByAttachThread(htop, 5000);
      Sleep(200);
      uApp.SetFocusByAttachThread(htop, 5000);
      Sleep(200);

      s1 := ALP.Editor[0].Value + '{TAB}';

      s2 := ALP.Editor_Username.Value + '{TAB}';
      s3 := ALP.Editor_Password.Value + '{TAB}';
      s4 := '{UP}{UP}{UP}' + GetAccount(ALP.Editor[1].Value) + '{TAB}';  
      s5 := '{UP}{UP}{UP}' + GetLang(ALP.Editor[2].Value) + '{TAB}';
      s6 := ALP.Editor[3].Value + '{ENTER}';

      uApp.SetTextByKeyEvent(htop, '{TAB}');  Sleep(100);
      uApp.SetTextByKeyEvent(htop, '{TAB}');  Sleep(100);  
      uApp.SetTextByKeyEvent(htop, '{TAB}');  Sleep(100);  
      uApp.SetTextByKeyEvent(htop, '{TAB}');  Sleep(100);  
      uApp.SetTextByKeyEvent(htop, '{TAB}');  Sleep(100);  

      //ServerName
      uApp.SetTextByKeyEvent(htop, s1);
      Sleep(500);

      //UserName
      uApp.SetTextByKeyEvent(htop, s2);
      Sleep(500);

      //Password
      uApp.SetTextByKeyEvent(htop, s3);
      Sleep(500);

      //Account
      uApp.SetTextByKeyEvent(htop, s4);
      Sleep(500);

      //Language
      uApp.SetTextByKeyEvent(htop, s5);
      Sleep(500);

      //OpDate
      uApp.SetTextByKeyEvent(htop, s6);

      ErrorLog('Do Autologin OK') ;  
    end;

    begin

      try
        AutoLogin;
      except
        ErrorLog('Do Autologin Except!') ;   
      end;
    end.
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表