2016-08-16 15 views
0

我有一個機器人框架測試套件,它使用「username」參數調用Powershell腳本。這是正確的,直到用戶名在這種情況下包含非ASCII(非英文)字符。機器人正在正確輸出字符(我通過記錄數值檢查並寫入了一個文本文件,它們總是正確的)。從Robot Framework傳遞到Powershell的非ASCII字符以問號形式顯示

但是當它到達Powershell腳本時,用戶名只會顯示一行問號。

我知道這是與文本編碼有關,但無法弄清楚/有什麼辦法來處理它。

我有一個解決方法選項是將Robot的用戶名寫入臨時文本文件,然後讓Powershell腳本從文本文件中讀取該值。這可以成功運行,但在多臺機器上運行這些腳本時效率非常低。

測試套件我有如下(只突出線拉出):

*** Variables *** 
${user_name} ÀÀÀ 

*** Test Cases *** 
Write foreign username 
    Write Username ${user_name} 

*** Keywords *** 
Write Username 
    [Arguments] ${user_name} 
    Run powershell.exe -file ".\\..\\Powershell\Write-Username.ps1" -UserName "${user_name}" 

PowerShell腳本則包含以下代碼:

param 
(
    [parameter(Mandatory=$True)] 
    [string]$UserName 
) 

$username | Out-File C:\Temp\Test.txt -Encoding UTF8 
+0

,而無需腳本很難猜測。首先記住'$ OutputEncoding = [Console] :: OutputEncoding' – Deptor

回答

0

Robot Framework User Guide

輸出編碼

所有輸出文件都使用UTF-8編碼進行編寫。輸出寫入 到控制檯使用當前控制檯編碼

Robot Framework是基於Python的;因此,下一個Python示例可以舉例說明的情況下:

import sys 
username = 'ÀÁÂÃÄÅ' 
print(sys.stdout.encoding, "username=" + username) 

輸出(示出了用於各種特定控制檯編碼的?問號)導致下一個推論:設定有源控制檯正確的編碼開始之前機器人框架;最好的選擇可能是utf-8;參見Change the active console Code Page,使用CHCP 65001Windows cmd)。

==> D:\test\Python\38975403.py 
utf-8 username=ÀÁÂÃÄÅ 

==> chcp 1252 >NUL 

==> python D:\test\Python\38975403.py 
cp1252 username=ÀÁÂÃÄÅ 

==> chcp 850 >NUL 

==> python D:\test\Python\38975403.py 
cp850 username=ÀÁÂÃÄÅ 

==> chcp 437 >NUL 

==> python D:\test\Python\38975403.py 
cp437 username=????ÄÅ 

==> chcp 852 >NUL 

==> python D:\test\Python\38975403.py 
cp852 username=?ÁÂ?Ä? 

僅供參考,旁邊還有使用外的ASCII字母上面的例子:

Ch UniCode  Dec UTF-8 Comment 

À U+00C0  192 0xC380 Latin Capital Letter A With Grave 
Á U+00C1  193 0xC381 Latin Capital Letter A With Acute 
 U+00C2  194 0xC382 Latin Capital Letter A With Circumflex 
à U+00C3  195 0xC383 Latin Capital Letter A With Tilde 
Ä U+00C4  196 0xC384 Latin Capital Letter A With Diaeresis 
Å U+00C5  197 0xC385 Latin Capital Letter A With Ring Above