简体   繁体   English

使用 Python + Selenium 进行基于 Electron 的应用程序测试

[英]Using Python + Selenium for Electron-based app testing

There is a lot of documentation on using Spectron for testing applications built using Electron.有很多关于使用 Spectron 测试使用 Electron 构建的应用程序的文档。

As I have a lot of utilities written in Python, I would like to know if there is any way to use Python-Selenium for testing apps built in Electron.由于我有很多用 Python 编写的实用程序,我想知道是否有任何方法可以使用 Python-Selenium 来测试使用 Electron 构建的应用程序。

From a few online resources, I found a few people were able to do it (though not the latest version, which I am using currently).从一些在线资源中,我发现一些人能够做到(虽然不是我目前使用的最新版本)。 I was able to launch the app using below code, but the call webdriver.Chrome() is a blocking call, and I never get the driver instance:我能够使用下面的代码启动应用程序,但调用 webdriver.Chrome() 是一个阻塞调用,我从来没有得到驱动程序实例:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = "/home/username/electron_test/node_modules/electron/dist/electron"

options.add_argument("--app=/home/username/electron_test/")
driver = webdriver.Chrome(chrome_options=options)

Thanks.谢谢。

    from selenium import webdriver

    # Start the web driver
    web_driver_path = os.path.join(
        os.environ["ProgramFiles(x86)"],
        "chromedriver-v3.1.9-win32-x64",
        "chromedriver.exe")
    service = webdriver.chrome.service.Service(web_driver_path)
    service.start()

    # start the app
    self.web_driver = webdriver.remote.webdriver.WebDriver(
        command_executor=service.service_url,
        desired_capabilities={
            'browserName': 'chrome',
            'goog:chromeOptions': {
                'args': [],
                'binary': PATH_TO_BINARY_APP,
                'extensions': [],
                'windowTypes': ['webview']},
            'platform': 'ANY',
            'version': ''},
        browser_profile=None,
        proxy=None,
        keep_alive=False)

First you need to create a service instance for the webdriver.首先,您需要为 webdriver 创建一个服务实例。 After that, open the electron app with the service url, so they can connect to each other.之后,使用服务 url 打开电子应用程序,以便它们可以相互连接。

Be sure to use the right web driver version matching your electron version.请务必使用与您的电子版本匹配的正确 Web 驱动程序版本。

FYI: When you use something like webviews in your app, you'll love the "windowTypes" line.仅供参考:当你在你的应用程序中使用类似 webviews 的东西时,你会喜欢“windowTypes”这一行。 Took me some hours to figure out.我花了几个小时才弄清楚。

@dirk answer is right! @dirk 答案是对的! the goog:chromeOptions is the trick. goog:chromeOptions 是诀窍。 Thanks!谢谢!

Here what I ended up with:这是我最终的结果:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

cap = DesiredCapabilities.CHROME.copy()
cap['goog:chromeOptions'] = {'binary': PATH_TO_BINARY_APP}
driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4444/wd/hub',
    desired_capabilities=cap)

driver.get('http://google.com/')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM