# 遠程執行指南
桌面GUI測試通常需要活動桌面來移動鼠標光標并將某些鍵鍵入焦點窗口。 這完全阻止了本地機器的正常使用。
但是在遠程計算機上運行測試是一項挑戰。 本指南收集了使用GUI測試控制遠程計算機的已知問題和解決方案。
## Windows遠程桌面功能
遠程桌面(RDP)通過Windows操作系統向遠程計算機提供虛擬活動桌面。 有兩個潛在問題:
> * 如果RDP窗口最小化,則默認情況下遠程PC上沒有活動桌面。
> * 如果RDP斷開連接,桌面將被鎖定。
在這兩種情況下,任何GUI自動化作業都將失敗(如果您不使用下面描述的一些技巧)。 TestComplete文檔中詳細描述了變通方法:
> * [在最小化的遠程桌面Windows中運行測試](https://support.smartbear.com/testcomplete/docs/testing-with/running/via-rdp/in-minimized-window.html)
> * [在運行自動測試時斷開與遠程桌面的連接](https://support.smartbear.com/testcomplete/docs/testing-with/running/via-rdp/keeping-computer-unlocked.html)
## VNC服務器軟件
有更簡單的方法可以避免上述問題:使用VNC服務器軟件(例如,Tight VNC)。 它作為一對客戶端和服務器工作。 VNC服務器還在遠程計算機上提供活動桌面。
> * 這是一個非虛擬桌面,因此在遠程PC上使用本機屏幕分辨率可能需要更新視頻驅動程序。
> * 最小化或斷開VNC客戶端不會破壞活動桌面(默認情況下!)。
> * 這是一個跨平臺的解決方案(VNC服務器是macOS的本機部分,可在Linux上使用)。
您可能面臨的唯一問題:
> * 使用遠程桌面(RDP)可能會破壞VNC服務器的優勢,您必須重新啟動遠程PC或應用上述RDP解決方法。
## 其他遠程訪問軟件
如果有人試圖使用Teamviewer,Power BI桌面或任何其他虛擬桌面軟件遠程運行GUI測試/自動化,請隨意在本指南中添加更多詳細信息。
## 在鎖定的機器上運行自動化的技巧
對于某些應用程序,可以在鎖定的計算機上運行GUI自動化,但它需要使用特殊方法。 首先,值得列出在鎖定機器上不起作用的方法:
> * [click\_input](code/pywinauto.base_wrapper.html#pywinauto.base_wrapper.BaseWrapper.click_input)和所有其他鼠標單擊并按下以`_input`結尾的方法.
> * [set\_focus](code/pywinauto.controls.hwndwrapper.html#pywinauto.controls.hwndwrapper.HwndWrapper.set_focus) as it uses `SetCursorPos` and `SetForegroundWindow`.
> * [type\_keys](code/pywinauto.base_wrapper.html#pywinauto.base_wrapper.BaseWrapper.type_keys) for native keyboard input.
> * Direct usage of modules [mouse](code/pywinauto.mouse.html) and [keyboard](code/pywinauto.keyboard.html).
其他一些方法也可能不起作用,但這取決于應用程序。 在`backend="win32"`中輸入靜默文本的方法很少:
> * [send\_chars](code/pywinauto.controls.hwndwrapper.html#pywinauto.controls.hwndwrapper.HwndWrapper.send_chars) (僅符號; 特殊鍵組合不起作用)
> * [send\_keystrokes](code/pywinauto.controls.hwndwrapper.html#pywinauto.controls.hwndwrapper.HwndWrapper.send_keystrokes) (一些特殊的組合鍵可能起作用)
另一種有用的方法是一次輸入字符串(兩個后端都可用,通常只用于編輯框):
> * [“uia”: set\_edit\_text](code/pywinauto.controls.uia_controls.html#pywinauto.controls.uia_controls.EditWrapper.set_edit_text) (按原樣輸入文本,不支持修飾符)
> * [“win32”: set\_edit\_text](code/pywinauto.controls.win32_controls.html#pywinauto.controls.win32_controls.EditWrapper.set_edit_text)
## 使用基于代理的CI啟動遠程腳本
當您的腳本準備好并且您可以手動在遠程計算機上運行它時,是時候自動執行最后一步:從本地計算機或CI服務器觸發運行腳本。
如果您有內部托管CI(例如,Jenkins),則可能目標計算機已使用代理連接到Jenkins master。 連接代理有三種方法:
> * 將代理程序作為服務運行:在這種情況下,GUI測試將不起作用,因為在作為服務運行時甚至無法創建GUI。
> * 通過SSH運行代理:GUI測試不起作用。
> * 將代理作為普通應用程序運行。 這是唯一的工作案例!
## 直接啟動遠程腳本
本章的靈感來自于 [issue #401](https://github.com/pywinauto/pywinauto/issues/401) (特別感謝[yangliang003](https://github.com/yangliang003)).
第一個選項是PsExec。 感謝 [這篇文章在StackExchange上](https://serverfault.com/a/852877/368634).
> 1. 下載[PsTools](https://docs.microsoft.com/en-us/sysinternals/downloads/psexec).
> 2. 使用`tasklist`命令獲取RDP會話的進程ID。 PowerShell腳本:
>
> > ~~~
> > $session = tasklist /fo CSV | findstr RDP ; $session = $session.Split(",")[3] ; $session.Split('"')[1]
> > ~~~
>
> 3. Start process: `PsExec.exe -s -i 123 python my_script.py`.
[Ansible](https://github.com/ansible/ansible) 有PsExec插件,簡化了它。 手冊示例:
~~~
---
- name: test ra module
hosts: *****
tasks:
- name: run GUI automation
win_psexec:
command: python pywinauto_example.py
executable: C:\Windows\PSTools\psexec.exe
interactive: yes
username: admin
password: ******
hostnames: ******
~~~
Windows Scheduler還能夠通過GUI交互支持啟動作業。 有一種簡單的方法可以從`cmd.exe`安排一次任務:
~~~
Schtasks /Create /tn my_task /tr c:\temp\my_task.bat /sc ONCE /st hh:mi:ss /sd yyyy/mm/dd
~~~
### How To`s
* [How To’s](HowTo.html)
- 什么是Pywinauto
- 入門指南
- 如何
- 等待長時間操作
- 遠程執行指南
- 每種不同控制類型可用的方法
- 貢獻者
- 開發筆記
- 待辦項目
- 更新日志
- 基本用戶輸入模塊
- pywinauto.mouse
- pywinauto.keyboard
- 主要用戶模塊
- pywinauto.application
- pywinauto.findbestmatch
- pywinauto.findwindows
- pywinauto.timings
- 特定功能
- pywinauto.clipboard
- pywinauto.win32_hooks
- 控件參考
- pywinauto.base_wrapper
- pywinauto.controls.hwndwrapper
- pywinauto.controls.menuwrapper
- pywinauto.controls.common_controls
- pywinauto.controls.win32_controls
- pywinauto.controls.uiawrapper
- pywinauto.controls.uia_controls
- Pre-supplied Tests
- pywinauto.tests.allcontrols
- pywinauto.tests.asianhotkey
- pywinauto.tests.comboboxdroppedheight
- pywinauto.tests.comparetoreffont
- pywinauto.tests.leadtrailspaces
- pywinauto.tests.miscvalues
- pywinauto.tests.missalignment
- pywinauto.tests.missingextrastring
- pywinauto.tests.overlapping
- pywinauto.tests.repeatedhotkey
- pywinauto.tests.translation
- pywinauto.tests.truncation
- 后端內部實施模塊
- pywinauto.backend
- pywinauto.element_info
- pywinauto.win32_element_info
- pywinauto.uia_element_info
- pywinauto.uia_defines
- 內部模塊
- pywinauto.controlproperties
- pywinauto.handleprops
- pywinauto.xml_helpers
- pywinauto.fuzzydict
- pywinauto.actionlogger
- pywinauto.sysinfo
- pywinauto.remote_memory_block