Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
04_HttpRunner通用_01_設(shè)置變量
在編寫用例的時候,如果一項硬編碼的數(shù)據(jù),反復多次出現(xiàn),可以將其提取出來設(shè)置為變量,然后分別進行調(diào)用,提高用例的靈活度。HttpRunner 支持變量申明(variables)和引用($var)的機制。
如某系統(tǒng)登錄頁面,其表單代碼如下
登錄頁面及其對應的表單代碼
編寫 YAML 格式的測試用例如下:
- config:
name: 用例 - 測試進銷存系統(tǒng)登錄
- test:
name: 步驟 - 完成登錄操作
request:
url: http://localhost/myweb/jxc/index.asp?action=login
method: POST
data:
username: admin
pwd: admin
問題:以上用例中,用戶名和密碼直接寫入請求實體 data 下,類似硬編碼,如果以其他用戶身份登錄系統(tǒng),或者其他補助也用到該用戶名的時候,則每次都要修改用例文件,明顯缺乏靈活性,所以我們將用戶名和密碼提取出來,設(shè)置為變量,做到一次修改多次使用。而且后面我們可以將其再存儲到環(huán)境變量文件(.env)中,可以多個測試用例共用一套變量。
解決:將用戶名和密碼的輸入存入變量中:
變量的定義和使用:
案例1: 變量定義于測試步驟(test)中
- config:
name: 用例 - 測試進銷存系統(tǒng)登錄
- test:
name: 步驟 - 完成登錄操作
# 相當于定義局部變量,只能在當前步驟中被使用
variables:
p_username: admin
p_password: admin
request:
url: http://localhost/myweb/jxc/index.asp?action=login
method: POST
data:
# 引用變量:$<變量名>
username: $p_username
pwd: $p_password
validate:
# 斷言,如果登錄成功,登錄后頁面的HTML中會出現(xiàn) “topFrame” 字符串
# contains: 包含判斷,判斷預期結(jié)果是否被包含在實際結(jié)果中
# content:實際結(jié)果,請求后返回的響應實體內(nèi)容,即登錄后打開的頁面 HTML代碼
# topFrame:預期結(jié)果,如果返回正確的頁面,其 HTML 中會包含此字符串
- contains: [content, topFrame]
案例2:變量定義于 config 中
- config:
name: 用例 - 測試進銷存系統(tǒng)登錄
# 相當于全局變量,在當前測試用例的所有步驟中都可以被使用
variables:
p_username: admin
p_password: admin
- test:
name: 步驟 - 完成登錄操作
request:
url: http://localhost/myweb/jxc/index.asp?action=login
method: POST
data:
# 在用例文件的任何位置都可以引用變量:$<變量名>
username: $p_username
pwd: $p_password
validate:
- contains: [content, topFrame]
如果 config 區(qū)域里面的變量和 test 區(qū)域里面的變量有沖突時會怎樣呢?
實驗1:變量同時定義于 config 和 測試步驟 中
- config:
name: 用例 - 測試進銷存系統(tǒng)登錄
variables:
# 在 config 區(qū)域,變量中使用 “正確” 的用戶名和密碼
p_username: admin
p_password: admin
- test:
name: 步驟 - 完成登錄操作
variables:
# 在步驟區(qū)域, 變量中使用 “錯誤” 的用戶名和密碼
p_username: admin2
p_password: admin2
request:
url: http://localhost/myweb/jxc/index.asp?action=login
method: POST
data:
username: $p_username
pwd: $p_password
validate:
- contains: [content, topFrame]
實驗2:變量同時定義于 config 和 測試步驟 中
html轉(zhuǎn)為pdf的組件有很多,但是還沒有哪一款能達到這個效果,其只要原因是wkhtmltopdf使用webkit網(wǎng)頁渲染引擎開發(fā)的用來將 html轉(zhuǎn)成 pdf的工具,可以跟多種腳本語言進行集成來轉(zhuǎn)換文檔。但是就使用簡便性來說還是itext等組件占據(jù)優(yōu)勢,如果你要轉(zhuǎn)換格式有比較高的要求,那么wkhtmltopdf絕對是不二之選!
下載路徑
官網(wǎng)地址 wkhtmltopdf.org/
github地址 github.com/wkhtmltopdf…
使用方法
java調(diào)用demo
public class HtmlToPdfInterceptor extends Thread { private InputStream is; public HtmlToPdfInterceptor(InputStream is){ this.is = is; } public void run(){ try{ InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { System.out.println(line.toString()); //輸出內(nèi)容 } }catch (IOException e){ e.printStackTrace(); } }}public class HtmlToPdf { //wkhtmltopdf在系統(tǒng)中的路徑 private static final String toPdfTool = "D:\wkhtmltopdf\bin\wkhtmltopdf.exe"; /** * html轉(zhuǎn)pdf * @param srcPath html路徑,可以是硬盤上的路徑,也可以是網(wǎng)絡路徑 * @param destPath pdf保存路徑 * @return 轉(zhuǎn)換成功返回true */ public static boolean convert(String srcPath, String destPath){ File file = new File(destPath); File parent = file.getParentFile(); //如果pdf保存路徑不存在,則創(chuàng)建路徑 if(!parent.exists()){ parent.mkdirs(); } StringBuilder cmd = new StringBuilder(); cmd.append(toPdfTool); cmd.append(" "); cmd.append(" --header-line");//頁眉下面的線 cmd.append(" --header-center 這里是頁眉這里是頁眉這里是頁眉這里是頁眉 ");//頁眉中間內(nèi)容 //cmd.append(" --margin-top 30mm ");//設(shè)置頁面上邊距 (default 10mm) cmd.append(" --header-spacing 10 ");//(設(shè)置頁眉和內(nèi)容的距離,默認0) cmd.append(srcPath); cmd.append(" "); cmd.append(destPath); boolean result = true; try{ Process proc = Runtime.getRuntime().exec(cmd.toString()); HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream()); HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream()); error.start(); output.start(); proc.waitFor(); }catch(Exception e){ result = false; e.printStackTrace(); } return result; } public static void main(String[] args) { HtmlToPdf.convert("https://my.oschina.net/papio/blog/835645", "d:/wkhtmltopdf.pdf"); }}復制代碼
wkhtmltopdf 參數(shù)詳解
wkhtmltopdf [OPTIONS]... <input file> [More input files] <output file>常規(guī)選項 --allow <path> 允許加載從指定的文件夾中的文件或文件(可重復) --book* 設(shè)置一會打印一本書的時候,通常設(shè)置的選項 --collate 打印多份副本時整理 --cookie <name> <value> 設(shè)置一個額外的cookie(可重復) --cookie-jar <path> 讀取和寫入的Cookie,并在提供的cookie jar文件 --copies <number> 復印打印成pdf文件數(shù)(默認為1) --cover* <url> 使用HTML文件作為封面。它會帶頁眉和頁腳的TOC之前插入 --custom-header <name> <value> 設(shè)置一個附加的HTTP頭(可重復) --debug-javascript 顯示的javascript調(diào)試輸出 --default-header* 添加一個缺省的頭部,與頁面的左邊的名稱,頁面數(shù)到右邊,例如: --header-left '[webpage]' --header-right '[page]/[toPage]' --header-line --disable-external-links* 禁止生成鏈接到遠程網(wǎng)頁 --disable-internal-links* 禁止使用本地鏈接 --disable-javascript 禁止讓網(wǎng)頁執(zhí)行JavaScript --disable-pdf-compression* 禁止在PDF對象使用無損壓縮 --disable-smart-shrinking* 禁止使用WebKit的智能戰(zhàn)略收縮,使像素/ DPI比沒有不變 --disallow-local-file-access 禁止允許轉(zhuǎn)換的本地文件讀取其他本地文件,除非explecitily允許用 --allow --dpi <dpi> 顯式更改DPI(這對基于X11的系統(tǒng)沒有任何影響) --enable-plugins 啟用已安裝的插件(如Flash --encoding <encoding> 設(shè)置默認的文字編碼 --extended-help 顯示更廣泛的幫助,詳細介紹了不常見的命令開關(guān) --forms* 打開HTML表單字段轉(zhuǎn)換為PDF表單域 --grayscale PDF格式將在灰階產(chǎn)生 --help Display help --htmldoc 輸出程序HTML幫助 --ignore-load-errors 忽略claimes加載過程中已經(jīng)遇到了一個錯誤頁面 --lowquality 產(chǎn)生低品質(zhì)的PDF/ PS。有用縮小結(jié)果文檔的空間 --manpage 輸出程序手冊頁 --margin-bottom <unitreal> 設(shè)置頁面下邊距 (default 10mm) --margin-left <unitreal> 將左邊頁邊距 (default 10mm) --margin-right <unitreal> 設(shè)置頁面右邊距 (default 10mm) --margin-top <unitreal> 設(shè)置頁面上邊距 (default 10mm) --minimum-font-size <int> 最小字體大小 (default 5) --no-background 不打印背景 --orientation <orientation> 設(shè)置方向為橫向或縱向 --page-height <unitreal> 頁面高度 (default unit millimeter) --page-offset* <offset> 設(shè)置起始頁碼 (default 1) --page-size <size> 設(shè)置紙張大小: A4, Letter, etc. --page-width <unitreal> 頁面寬度 (default unit millimeter) --password <password> HTTP驗證密碼 --post <name> <value> Add an additional post field (repeatable) --post-file <name> <path> Post an aditional file (repeatable) --print-media-type* 使用的打印介質(zhì)類型,而不是屏幕 --proxy <proxy> 使用代理 --quiet Be less verbose --read-args-from-stdin 讀取標準輸入的命令行參數(shù) --readme 輸出程序自述 --redirect-delay <msec> 等待幾毫秒為JS-重定向(default 200) --replace* <name> <value> 替換名稱,值的頁眉和頁腳(可重復) --stop-slow-scripts 停止運行緩慢的JavaScripts --title <text> 生成的PDF文件的標題(第一個文檔的標題使用,如果沒有指定) --toc* 插入的內(nèi)容的表中的文件的開頭 --use-xserver* 使用X服務器(一些插件和其他的東西沒有X11可能無法正常工作) --user-style-sheet <url> 指定用戶的樣式表,加載在每一頁中 --username <username> HTTP認證的用戶名 --version 輸出版本信息退出 --zoom <float> 使用這個縮放因子 (default 1) 頁眉和頁腳選項--header-center* <text> (設(shè)置在中心位置的頁眉內(nèi)容) --header-font-name* <name> (default Arial) (設(shè)置頁眉的字體名稱)--header-font-size* <size> (設(shè)置頁眉的字體大小)--header-html* <url> (添加一個HTML頁眉,后面是網(wǎng)址)--header-left* <text> (左對齊的頁眉文本)--header-line* (顯示一條線在頁眉下)--header-right* <text> (右對齊頁眉文本)--header-spacing* <real> (設(shè)置頁眉和內(nèi)容的距離,默認0)--footer-center* <text> (設(shè)置在中心位置的頁腳內(nèi)容) --footer-font-name* <name> (設(shè)置頁腳的字體名稱) --footer-font-size* <size> (設(shè)置頁腳的字體大小default 11)--footer-html* <url> (添加一個HTML頁腳,后面是網(wǎng)址)--footer-left* <text> (左對齊的頁腳文本)--footer-line* 顯示一條線在頁腳內(nèi)容上)--footer-right* <text> (右對齊頁腳文本)--footer-spacing* <real> (設(shè)置頁腳和內(nèi)容的距離)./wkhtmltopdf --footer-right '[page]/[topage]' http://www.baidu.com baidu.pdf./wkhtmltopdf --header-center '報表' --header-line --margin-top 2cm --header-line http://192.168.212.139/oma/ oma.pdf表內(nèi)容選項中 --toc-depth* <level> Set the depth of the toc (default 3) --toc-disable-back-links* Do not link from section header to toc --toc-disable-links* Do not link from toc to sections --toc-font-name* <name> Set the font used for the toc (default Arial) --toc-header-font-name* <name> The font of the toc header (if unset use --toc-font-name) --toc-header-font-size* <size> The font size of the toc header (default 15) --toc-header-text* <text> The header text of the toc (default Table Of Contents) --toc-l1-font-size* <size> Set the font size on level 1 of the toc (default 12) --toc-l1-indentation* <num> Set indentation on level 1 of the toc (default 0) --toc-l2-font-size* <size> Set the font size on level 2 of the toc (default 10) --toc-l2-indentation* <num> Set indentation on level 2 of the toc (default 20) --toc-l3-font-size* <size> Set the font size on level 3 of the toc (default 8) --toc-l3-indentation* <num> Set indentation on level 3 of the toc (default 40) --toc-l4-font-size* <size> Set the font size on level 4 of the toc (default 6) --toc-l4-indentation* <num> Set indentation on level 4 of the toc (default 60) --toc-l5-font-size* <size> Set the font size on level 5 of the toc (default 4) --toc-l5-indentation* <num> Set indentation on level 5 of the toc (default 80) --toc-l6-font-size* <size> Set the font size on level 6 of the toc (default 2) --toc-l6-indentation* <num> Set indentation on level 6 of the toc (default 100) --toc-l7-font-size* <size> Set the font size on level 7 of the toc (default 0) --toc-l7-indentation* <num> Set indentation on level 7 of the toc (default 120) --toc-no-dots* Do not use dots, in the toc輪廓選項 --dump-outline <file> 轉(zhuǎn)儲目錄到一個文件 --outline 顯示目錄(文章中h1,h2來定) --outline-depth <level> 設(shè)置目錄的深度(默認為4)頁腳和頁眉 * [page] 由當前正在打印的頁的數(shù)目代替 * [frompage] 由要打印的第一頁的數(shù)量取代 * [topage] 由最后一頁要打印的數(shù)量取代 * [webpage] 通過正在打印的頁面的URL替換 * [section] 由當前節(jié)的名稱替換 * [subsection] 由當前小節(jié)的名稱替換 * [date] 由當前日期系統(tǒng)的本地格式取代 * [time] 由當前時間,系統(tǒng)的本地格式取代
作者:曹元
鏈接:https://juejin.im/post/6856547881873047559
來源:掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
些在線圖文編輯器不支持直接插入代碼塊,但可以直接粘貼 HTML 格式的高亮代碼塊。
花了一點時間研究了一下各家的編輯器,規(guī)則卻各不相同。有的要求代碼塊被包含于 <code> ... </code> 或者 <pre> <code> ... </code> </pre> , 有些要求 class 屬性里包含 "code" 關(guān)鍵詞,或者要求代碼塊里必須包含至少一個 <br> 。如果不符合這些要求,不是變成普通文本,就是丟失換行縮進,或者丟失顏色樣式。
所以,這就難了。先得找個支持代碼高亮的編輯器,仔細地選擇并復制代碼塊,復制完還得編輯剪貼板里的 HTML 。這就不如干脆寫個轉(zhuǎn)換工具了。
因為瀏覽器操作系統(tǒng)剪貼板可能不太方便,下面用 aardio 寫一個工具軟件。
先看軟件成品演示:
軟件用法:
1、輸入編程語言名稱(支持自動完成)。
2、然后在輸入框中粘貼要轉(zhuǎn)換的編程代碼。
3、點擊「復制高亮代碼塊」按鈕。
然后我們就可以打開在線圖文編輯器直接粘貼生成的高亮代碼塊了。
下面是這個軟件的 aardio 源代碼:
import win.ui;
/*DSG{{*/
var winform = win.form(text="HTML 代碼塊生成工具 - 本工具使用 aardio 語言編寫";right=1055;bottom=674;bgcolor=16777215)
winform.add(
button={cls="button";text="復制高亮代碼塊";left=633;top=609;right=1000;bottom=665;bgcolor=16777215;color=14120960;db=1;dr=1;font=LOGFONT(h=-14);note="可在網(wǎng)頁編輯器直接粘貼";z=4};
cmbLangs={cls="combobox";left=262;top=625;right=446;bottom=651;db=1;dl=1;edge=1;items={"javascript"};mode="dropdown";z=2};
editCode={cls="edit";left=1;top=4;right=1052;bottom=599;db=1;dl=1;dr=1;dt=1;edge=1;hscroll=1;multiline=1;vscroll=1;z=5};
static={cls="static";text="請選擇語言:";left=70;top=629;right=248;bottom=649;align="right";db=1;dl=1;transparent=1;z=3};
webCtrl={cls="custom";text="自定義控件";left=8;top=10;right=1048;bottom=604;db=1;dl=1;dr=1;dt=1;hide=1;z=1}
)
/*}}*/
import web.view;
var wb = web.view(winform.webCtrl);
import win.clip.html;
wb.export({
onHighlight = function(html,background,foreground){
html = `<pre class="code" style="overflow-x:auto;text-align:left;box-shadow: rgba(216, 216, 216, 0.5) 0px 0px 0px 1px inset;padding:10px;border-radius:3px;background-color:`+background+`;color:`+foreground+`;white-space:pre;word-break:break-all;display:block;font-size:14px;font-style:normal;font-variant-ligatures:normal;font-variant-caps: normal;font-family: "Consolas", Consolas, "Liberation Mono", Menlo, Courier, monospace"><code>`
+ html + `</code></pre>`;
html,count = string.replace(html,'\n',"<br>");
if(!count){
html = string.replace(html,`\</code\>\</pre\>$`,`<br></code></pre>`);
}
var cb = win.clip.html();
cb.write(html);
winform.setTimeout(
function(){
winform.editCode.show(true);
winform.webCtrl.show(false);
winform.text = "HTML 代碼塊生成工具 - 已復制高亮代碼塊到剪貼板,可在網(wǎng)頁直接粘貼";
},1000);
};
setLanguages = function(langs){
winform.languages = langs;
}
})
winform.cmbLangs.onEditChange = function(){
var text = string.lower(winform.cmbLangs.text);
var items = table.filter( winform.languages : {}, lambda(v) string.startWith(v,text) );
winform.cmbLangs.autoComplete(items);
}
winform.cmbLangs.editBox.disableInputMethod();
import web.prism;
import wsock.tcp.asynHttpServer;
var httpServer = wsock.tcp.asynHttpServer();
httpServer.run(web.prism,{
["/index.html"] = /*****
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link href="prism.css" rel="stylesheet" />
</head>
<body>
<pre id="code-pre"><code id="code" class="lang-javascript"></code></pre>
<script src="prism.js"></script>
<script>
function computedColorStyle(element, options = {}) {
Array.prototype.forEach.call(element.children,child => {
computedColorStyle(child, options);
});
const computedStyle = getComputedStyle(element);
element.style["color"] = computedStyle.getPropertyValue("color");
}
highlight = function(code,language){
var html = Prism.highlight(code, Prism.languages[language], language);
var codeEle = document.getElementById("code");
codeEle.innerHTML = html;
computedColorStyle(codeEle);
const computedStyle = getComputedStyle(codeEle);
onHighlight(codeEle.innerHTML
,getComputedStyle(document.getElementById("code-pre")).getPropertyValue("background-color")
,computedStyle.getPropertyValue("color"));
}
setLanguages( Object.keys(Prism.languages) );
</script>
</body>
</html>
*****/
});
wb.go( httpServer.getUrl("/index.html"));
winform.button.oncommand = function(id,event){
winform.text = "HTML 代碼塊生成工具 - 本工具使用 aardio 語言編寫"
winform.editCode.show(false);
winform.webCtrl.show(true);
wb.xcall("highlight",winform.editCode.text,winform.cmbLangs.text);
}
winform.show();
win.loopMessage();
打開 aardio 創(chuàng)建工程,然后復制粘貼上面的代碼到 main.aardio 里面就可以直接運行,或生成獨立 EXE 文件:
這個軟件的原理:
1、首先通過 WebView2 調(diào)用 Prism.js 高亮代碼。為了可以內(nèi)存加載 Prism.js ( 支持生成獨立 EXE ),我寫了一個 aardio 擴展庫 web.prism 。關(guān)于 WebView2 請參考:放棄 Electron,擁抱 WebView2!JavaScript 快速開發(fā)獨立 EXE 程序
2、因為 Prism.js 生成的 HTML 代碼塊都是使用 class 屬性指定樣式,所以我們需要調(diào)用 getComputedStyle 獲取最終渲染的字體顏色屬性。
3、最后在 JavaScript 里調(diào)用 aardio 函數(shù)處理生成的 HTML 代碼塊,aardio 的任務是將 HTML 修改為更合適直接粘貼的格式,并盡可能地處理各圖文編輯器的兼容問題。然后調(diào)用 win.clip.html 將處理好的 HTML 復制到系統(tǒng)剪貼板:
import win.clip.html;
var cb = win.clip.html();
cb.write(html);
然后只要愉快地粘貼代碼塊就可以。
如果是 aardio 代碼不需要用這個工具,在 aardio 編輯器里右鍵直接點『 復制全部到 HTML 代碼塊 』就可以了:
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。