c/s端測試——nw.js篇(selenium工具)


最近在為兄弟部門開發自動化測試工具。
然后才知道現在竟然有JS工具可以把JS打包成cs端程序了,太牛了,js發展是真快。並且還是跨平台的,mac、windows、linux都支持。
當然,今天不是說nw.js是怎樣去打包EXE程序,今天是來說怎么去對這樣的nw.js打包的C/S程序進行自動化測試。
百度了很久,沒找到對應的資料,還是打開vps谷歌了一把,以下基本都是官網的內容

一、工具ChromeDriver

ChromeDriver , 一款開源工具 , 提供跨瀏覽器自動測試頁面應用 . 提供引導頁面 , 用戶輸入 , 腳本執行等功能 . 實現Chromium協議的獨立服務 . 安卓系統以及PC系統(Mac, Linux, Windows, ChromeOS)的Chorme瀏覽器中可以運行

NW.js提供自定義ChromeDriver完成自動測試基於NW.js開發的應用 , 可以通過類似於selenium工具進行使用

二、安裝

1.從官網下載SDK構建方式的NW.js , 其中包含ChromeDriver工具 .

解壓下載的文件,chromedriver在解壓的NW.js目錄中

Linux系統中名為nw

Windows系統中名為nw.exe

Mac系統中名為node-webkit.app

將開發的dist文件替換到sdk中,若有相應的配置文件同樣放在其根目錄下

然后打開nw.exe,點擊主頁面,右鍵點擊,可選擇“檢查”、F12,效果與打開瀏覽器一致,如下:

001

002

2.安裝selenium-python到python環境中 :

   pip install selenium

三、運行

假設你的應用遠程查詢內容 . 頁面代碼大致如下:

<form action="http://mysearch.com/search" method="GET">
	    <input type="text" name="q"><input type="submit" value="Submit">
</form>

編寫一個python腳本自動填充搜索框並提交表單:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
path = 'D:\\項目\\測試平台\\nw\\nw_sdk\\'
chrome_options.add_argument("nwapp="+path)

driver = webdriver.Chrome(executable_path=path+'chromedriver', chrome_options=chrome_options)

time.sleep(5) # 等待5秒 , 查看頁面
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # 等待5秒 , 查看搜索結果
driver.quit()

參考http://selenium-python.readthedocs.org/詳細文檔 .

四、修改參數

被修改的chromedriver工具默認在NW可執行相同目錄下 .

如果需要傳遞參數給命令行 , 需要使用選項nwargs:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("nwapp=/path/to/your/app")
chrome_options.add_experimental_option("nwargs", ["arg1", "arg2"])

driver = webdriver.Chrome(executable_path='/path/to/nwjs/chromedriver', chrome_options=chrome_options)

四、最后

忘記放官網鏈接了

https://nwjs-cn.readthedocs.io/zh_CN/latest/Base/Advanced/Test%20with%20ChromeDriver/index.html#_1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM