Selenium python 在 nwjs 应用客户端做 UI 自动化

leayn for 贝壳找房 · 2018年12月08日 · 最后由 long 回复于 2019年06月18日 · 2218 次阅读

nwjs 简介

NW.js 是基于 Chromium 和 node.js 运行的,跨平台应用客户端。目前已经有很多应用在使用,可查看官网统计https://github.com/nwjs/nw.js/wiki/List-of-apps-and-companies-using-nw.js

搭建步骤

下载 nw+chromedriver 压缩包

地址:http://dl.nwjs.io/
需要根据被测应用的版本匹配下载,放在固定的位置调用。

编写程序

python 包:selenium ,Options

import os
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

#设置chromedriver路径
path = "/path/to/nwjssdk/nwjs-sdk-v0.30.5-osx-x64/chromedriver"
chrome_options = Options()

#添加nwjs.app的option
chrome_options.add_argument( "nwapp=/Applications/xx.app/Contents/Resources/app.nw")
print chrome_options.__dict__

#启动应用
driver = webdriver.Chrome( executable_path=path, chrome_options=chrome_options)
time.sleep(5)
#查找元素
print driver.find_element_by_xpath('//*[@id="root"]/div/div/section/section/article/div/ul/li[2]/div').click()
driver.quit()

实践中采坑

  • 引用包:Options
  • 客户端启动比较慢,并且没有 localStorage,会遇到应用本身的判断逻辑失败。
  • nwjs 客户端做自动化成本较高,需要权衡
共收到 3 条回复 时间 点赞

先关注,先支持, 加油 楼主

尚酷米 回复

谢谢

提示如下错误:
Session ID: e5e688a264451f17c879ef6ab88ef3b5
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.maximize(RemoteWebDriver.java:833)
at utility.Assertion.main(Assertion.java:872)

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册