【问题标题】:Using Python + Selenium for Electron-based app testing使用 Python + Selenium 进行基于 Electron 的应用程序测试
【发布时间】:2019-05-24 00:58:44
【问题描述】:

有很多关于使用 Spectron 测试使用 Electron 构建的应用程序的文档。

由于我有很多用 Python 编写的实用程序,我想知道是否有任何方法可以使用 Python-Selenium 来测试在 Electron 中构建的应用程序。

从一些在线资源中,我发现有几个人能够做到这一点(虽然不是我目前使用的最新版本)。 我能够使用下面的代码启动应用程序,但调用 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)

谢谢。

【问题讨论】:

  • stackoverflow.com/a/51186740/8903949。这应该对您有所帮助。我已经尝试使用 java 使用 selenium。
  • 我在 python 中使用过类似的代码,但是 webdriver.Chrome() 是一个阻塞调用
  • 我认为问题在于“--app=/home/username/electron_test/”行。您能否使用 options.add_argument("--app=","/home/username/electron_test/") 更新它

标签: python selenium electron selenium-chromedriver spectron


【解决方案1】:
    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)

首先您需要为 webdriver 创建一个服务实例。之后,使用服务 url 打开电子应用程序,这样它们就可以相互连接了。

请务必使用与您的电子版本相匹配的正确 Web 驱动程序版本。

仅供参考:当您在应用中使用 webviews 之类的东西时,您会喜欢“windowTypes”这一行。我花了几个小时才弄清楚。

【讨论】:

  • 嗨 Dirk,“我收到错误:selenium.common.exceptions.WebDriverException:消息:未知错误:DevToolsActivePort 文件不存在”我的电子版本是:7.3.0,chromedriver 版本是:78.0.3904.105。经过所有的谷歌搜索后,我找到了添加选项的解决方案,如 --headless、--no-sandbox 等,但没有任何效果。你能帮忙吗?
【解决方案2】:

@dirk 的回答是对的! goog:chromeOptions 是诀窍。谢谢!

这是我最终得到的结果:

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/')

【讨论】:

  • 嗨丹尼尔,“我收到错误:selenium.common.exceptions.WebDriverException:消息:未知错误:DevToolsActivePort 文件不存在”我的电子版本是:7.3.0,chromedriver 版本是:78.0.3904.105。经过所有的谷歌搜索后,我找到了添加选项的解决方案,如 --headless、--no-sandbox 等,但没有任何效果。你能帮忙吗?
猜你喜欢
  • 2023-01-29
  • 1970-01-01
  • 2021-06-16
  • 2016-07-20
  • 2017-04-01
  • 2018-02-16
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多