本文主要是介绍Selenium测试NW.js python实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
NW.js
nw是在Chrome的内核上封装了一层外壳,进而把Web应用封装成桌面应用程序。既然NW是基于Chrome内核的,就可以用ChromeDriver驱动NW进行自动化测试。
NW封装搜狗主页
- 下载NW binary 文件: https://nwjs.io/downloads/
- 解压到指定文件下,例如:D:\SelfDevelop\Python\nwjs-sdk-v0.45.1-win-x64
- 将NW的主目录追加到Path环境变量里面
- 创建HelloWorld的APP:D:\SelfDevelop\Python\nwjs-sdk-v0.45.1-win-x64\Hello World
- 在app文件夹下创建一个名为package.json的文件内容如下:
{
“name”: “helloworld”,
“main”: “https://www.sogou.com”
}
Selenium启动NW
- 安装Python Selenium模块
pip install Selenium
- 启动NW
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Optionsdef start_driver():chrome_options = Options()chrome_options.add_argument(r"nwapp=D:\SelfDevelop\Python\nwjs-sdk-v0.45.1-win-x64\Hello World")driver = webdriver.Chrome(executable_path=r'D:\SelfDevelop\Python\nwjs-sdk-v0.45.1-win-x64\chromedriver.exe',options=chrome_options)return driver
- 搜索Chrome Driver
time.sleep(5) # Wait 5s to see the web page
driver = start_driver();
search_box = driver.find_element_by_class_name('sec-input')
search_box.send_keys('ChromeDriver')
submit_button = driver.find_element_by_id('stb')
submit_button.click();
time.sleep(5) # Wait 5s to see the search result
driver.quit()
以后会持续更新自动化测试相关的内容,喜欢的童鞋记得点赞加关注哦!
这篇关于Selenium测试NW.js python实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!