python - 在带有 unittest 的自动 Selenium 测试中使用多个 WebSocket 客户端

标签 python unit-testing selenium websocket

我正在为特定设备编写自动化 UI 测试,其中 UI 配置通过 websocket 连接作为多个 JSON 文件从后端服务器传输到设备。首先,我编写了一个单独的小脚本来接收 websocket 消息,将它们保存到 JSON 文件并关闭 websocket 连接。然后当我在另一个文件中运行我的测试时,我可以访问 JSON 文件并在我的测试中处理配置信息。

然而,我想做的是将这个 websocket 连接集成到测试中,所以我只会运行一个测试,它会处理所有事情。我尝试这样做,但测试在监听 websocket 时卡住了,无法在 UI 中导航,这是测试的下一步,它会触发消息传输。

我正在使用 Python 2.7.14 的 Anaconda 虚拟环境中的 Windows 10 上运行测试,并使用 ChromeDriver for Selenium 3.11.0。 websocket库是0.2.1版本。

这是我使用的代码:

#!/usr/bin/python
#-*- encoding: utf-8 -*-

import json
from lib import *
import sys
import unittest
import websocket
import xmlrunner


class TestCases(TestObject):

    def test_service_recharge(self):

        # Step 1: Login and initialize websocket client
        credential = self.get_data()["credentials"][0]
        login_page = LoginPage(self.get_driver(), url=self.get_url())
        login_page.load_page()
        login_page.try_login(credential["username"], credential["password"])

        ws = websocket.WebSocket()
        ws.connect("ws://192.168.1.105:8888/gui/services/menudata")
        print "Receiving websocket messages..."
        menu_data = ws.recv()

        # Step 2: Check successful login 
        services_page = ServicesPage(self.get_driver())
        services_page.check_opened()

        # Step 3: Save websocket message to a .json file
        menu_data_json = json.loads(menu_data)

        with open('PATH_TO_FILE\\Services_Menu_Recharge_JSON.json', 'w') as outfile:
            json.dump(menu_data_json, outfile)

        ws.close()

        # Step 4: Start a new websocket client to get a second, different message
        ws = websocket.WebSocket()
        ws.connect("ws://192.168.1.105:8888/gui/services/servicesdata")
        print "Receiving websocket messages..."
        menu_data = ws.recv()

        # Step 5: Navigate to another page and save the obtained message to a local file
        services_page.go_to_recharge()
        recharge_page = RechargePage(self.get_driver())
        recharge_page.check_opened()

        menu_data_json = json.loads(menu_data)

        with open('PATH_TO_FILE\\Services_Components_Recharge_JSON.json', 'w') as outfile:
            json.dump(menu_data_json, outfile)

        ws.close()

        # Step 6: Work with the locally saved json files
        parser = JsonParser()
        service_components_list = parser.read_all_nodes()

        # Rest of the test #

if __name__ == '__main__':
    unittest.main(testRunner=xmlrunner.XMLTestRunner(
        output='test-reports'), failfast=False, buffer=False, catchbreak=False)

这里是卡住的地方:

Running tests...
----------------------------------------------------------------------
[17364:19032:0705/163445.321:ERROR:install_util.cc(597)] Unable to read registry value HKLM\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmentToken for writing result=2

DevTools listening on ws://127.0.0.1:12069/devtools/browser/a91244ef-8daf-422e-a24a-ca0e19218aa1
Receiving websocket messages...

我猜测试脚本正在监听来 self 提供的地址的 websocket 消息,并且在收到消息之前不会继续。

我该如何处理这个问题,以便客户端监听具有超时限制的 websocket 消息并保存/共享接收到的消息,以便可以从测试脚本同时访问它作为测试步骤继续运行?

预先感谢您的关注和回复。

最佳答案

显然,我需要的是使用线程。我为 websocket 客户端和自动化测试使用了不同的线程。

我使用了threading 库来实现这一点,并使运行 websocket 客户端守护线程的线程成为可能。这解决了我的问题。

关于python - 在带有 unittest 的自动 Selenium 测试中使用多个 WebSocket 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51208401/

相关文章:

python - 如何在本地测试 aws lambda 函数

java - Easymock 不兼容的返回值类型错误

ruby - 单元测试错误1个:Fixnum的未定义方法 `rank'

unit-testing - 在没有 ExpectedException 属性的 nUnit 中预期异常

java - 如何获取 Espresso 中的元素列表?

javascript - 如何使用 selenium javascript 单击推文的收藏夹按钮

Python。如何从 JSON 获取目录路径?

python - 将 pandas DataFrame 的索引增加一个

python - 如何将以天、小时、分钟和秒为单位的时间转换为仅以秒为单位

javascript - 如何使用 Selenium 和 Python 更改 DOM 中的属性值