GVKun编程网logo

带有selenium测试的Django通道2失败(selenium ddt)

9

本文将带您了解关于带有selenium测试的Django通道2失败的新内容,同时我们还将为您解释seleniumddt的相关知识,另外,我们还将为您提供关于AngularJs使用Java编写的现有Se

本文将带您了解关于带有selenium测试的Django通道2失败的新内容,同时我们还将为您解释selenium ddt的相关知识,另外,我们还将为您提供关于AngularJs使用Java编写的现有Selenium测试进行端到端测试、appium+python+selenium测试配置、Django学习系列2:django环境中安装selenium并查看selenium版本号、Django测试用例和Selenium服务器如何使用相同的数据库?的实用信息。

本文目录一览:

带有selenium测试的Django通道2失败(selenium ddt)

带有selenium测试的Django通道2失败(selenium ddt)

我正在尝试遵循Django频道教程。我能够实现此处所述的聊天功能。但是从此页面粘贴的单元测试完全复制失败,并出现以下错误AttributeError:Can''t pickle local object ''DaphneProcess.__init__.<locals>.<lambda>''

完整回溯:

Traceback (most recent call last):  File "C:\Users\user\PycharmProjects\django_channels_test\venv35\lib\site-packages\django\test\testcases.py", line 202, in __call__    self._pre_setup()  File "C:\Users\user\PycharmProjects\django_channels_test\venv35\lib\site-packages\channels\testing\live.py", line 42, in _pre_setup    self._server_process.start()  File "C:\Users\user\AppData\Local\Programs\Python\Python35\Lib\multiprocessing\process.py", line 105, in start    self._popen = self._Popen(self)  File "C:\Users\user\AppData\Local\Programs\Python\Python35\Lib\multiprocessing\context.py", line 212, in _Popen    return _default_context.get_context().Process._Popen(process_obj)  File "C:\Users\user\AppData\Local\Programs\Python\Python35\Lib\multiprocessing\context.py", line 313, in _Popen    return Popen(process_obj)  File "C:\Users\user\AppData\Local\Programs\Python\Python35\Lib\multiprocessing\popen_spawn_win32.py", line 66, in __init__    reduction.dump(process_obj, to_child)  File "C:\Users\user\AppData\Local\Programs\Python\Python35\Lib\multiprocessing\reduction.py", line 59, in dump    ForkingPickler(file, protocol).dump(obj)AttributeError: Can''t pickle local object ''DaphneProcess.__init__.<locals>.<lambda>''

我的消费阶层:

class ChatConsumer(AsyncWebsocketConsumer):    async def connect(self):        self.room_name = self.scope[''url_route''][''kwargs''][''room_name'']        self.room_group_name = ''chat_%s'' % self.room_name        # Join room group        await self.channel_layer.group_add(            self.room_group_name,            self.channel_name        )        await self.accept()    async def disconnect(self, close_code):        # Leave room group        await self.channel_layer.group_discard(            self.room_group_name,            self.channel_name        )    # Receive message from WebSocket    async def receive(self, text_data):        text_data_json = json.loads(text_data)        message = text_data_json[''message'']        # Send message to room group        await self.channel_layer.group_send(            self.room_group_name,            {                ''type'': ''chat_message'',                ''message'': message            }        )    # Receive message from room group    async def chat_message(self, event):        message = event[''message'']        # Send message to WebSocket        await self.send(text_data=json.dumps({            ''message'': message        }))

我的测试模块:

from channels.testing import ChannelsLiveServerTestCasefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.support.wait import WebDriverWaitclass ChatTests(ChannelsLiveServerTestCase):    serve_static = True  # emulate StaticLiveServerTestCase    @classmethod    def setUpClass(cls):        super().setUpClass()        try:            # NOTE: Requires "chromedriver" binary to be installed in $PATH            cls.driver = webdriver.Chrome()        except:            super().tearDownClass()            raise    @classmethod    def tearDownClass(cls):        cls.driver.quit()        super().tearDownClass()    def test_when_chat_message_posted_then_seen_by_everyone_in_same_room(self):        try:            self._enter_chat_room(''room_1'')            self._open_new_window()            self._enter_chat_room(''room_1'')            self._switch_to_window(0)            self._post_message(''hello'')            WebDriverWait(self.driver, 2).until(lambda _:                ''hello'' in self._chat_log_value,                ''Message was not received by window 1 from window 1'')            self._switch_to_window(1)            WebDriverWait(self.driver, 2).until(lambda _:                ''hello'' in self._chat_log_value,                ''Message was not received by window 2 from window 1'')        finally:            self._close_all_new_windows()    def test_when_chat_message_posted_then_not_seen_by_anyone_in_different_room(self):        try:            self._enter_chat_room(''room_1'')            self._open_new_window()            self._enter_chat_room(''room_2'')            self._switch_to_window(0)            self._post_message(''hello'')            WebDriverWait(self.driver, 2).until(lambda _:                ''hello'' in self._chat_log_value,                ''Message was not received by window 1 from window 1'')            self._switch_to_window(1)            self._post_message(''world'')            WebDriverWait(self.driver, 2).until(lambda _:                ''world'' in self._chat_log_value,                ''Message was not received by window 2 from window 2'')            self.assertTrue(''hello'' not in self._chat_log_value,                ''Message was improperly received by window 2 from window 1'')        finally:            self._close_all_new_windows()    # === Utility ===    def _enter_chat_room(self, room_name):        self.driver.get(self.live_server_url + ''/chat/'')        ActionChains(self.driver).send_keys(room_name + ''\n'').perform()        WebDriverWait(self.driver, 2).until(lambda _:            room_name in self.driver.current_url)    def _open_new_window(self):        self.driver.execute_script(''window.open("about:blank", "_blank");'')        self.driver.switch_to.window(self.driver.window_handles[-1])    def _close_all_new_windows(self):        while len(self.driver.window_handles) > 1:            self.driver.switch_to.window(self.driver.window_handles[-1])            self.driver.execute_script(''window.close();'')        if len(self.driver.window_handles) == 1:            self.driver.switch_to.window(self.driver.window_handles[0])    def _switch_to_window(self, window_index):        self.driver.switch_to.window(self.driver.window_handles[window_index])    def _post_message(self, message):        ActionChains(self.driver).send_keys(message + ''\n'').perform()    @property    def _chat_log_value(self):        return self.driver.find_element_by_css_selector(''#chat-log'').get_property(''value'')

我正在使用Python 3.5和Django 2.0。

答案1

小编典典

reduction.py无法序列化包含lambda的对象。经过一些研究,这似乎与Windows环境中的多处理问题有关(并且不仅限于此示例)。

解决此问题的一种方法是 reduction.py

替换import pickleimport dill as pickle

莳萝包可以在腌制失败的地方序列化这些对象。但是,在不深入研究以确保此更改不会破坏其他任何内容的情况下,我不建议在生产环境中使用此功能。

AngularJs使用Java编写的现有Selenium测试进行端到端测试

AngularJs使用Java编写的现有Selenium测试进行端到端测试

鉴于一个网站现有大量使用Selenium为 Java编写的 Java测试用例,并且该网站正在AngularJs中重写,如何将Selenium测试集成到新应用程序中?

我已经研究了Protractor,这是一个使用Selenium的新推荐的端到端测试运行器,但是,这似乎建议用Javascript编写测试. This article详细介绍了如何使这个设置正常工作,但同样,测试是用Javascript编写的.

尽管我想在Javascript中编写测试,但我想避免重写,因此将现有的集成方法集成起来会很好. Protractor中是否有配置可以实现这一点?

解决方法

我担心这里没有好的解决方案.你的方式有很多障碍.

> Protractor是用JavaScript编写的,是Selenium的WebDriverJS实现的包装器.鉴于WebDriverJS使用与Java WebDriver非常不同的API(由于节点的异步性质),即使技术上可以获得包装Java库的节点库,在这种情况下它也不起作用.
>量角器和Selenium不支持相同的API,所以即使有基于Java的量角器,你也会发现自己做了一些重组工作.
>如果您正在利用Angular,那么虽然您的页面的整体行为可能与底层DOM结构相同,但不会如此.无论是这个还是你都没有充分利用Angular.因此,除非你有一个非常好的页面对象抽象层,否则你将不得不重写你的测试.如果确实有一个好的页面对象层,则需要在任何情况下重写该层.

在我看来,你最好的最好的如下:

如果您的规范本身是用更高级别的规范语言(如Cucumber)编写的,那么理论上至少可以使用cucumber-js将其移植到javascript中,您可以简单地重新实现底层定义(不是小任务).量角器.

你可以真正雄心勃勃地将端口量角器转换为Java,如果它似乎比在javascript中重写你的测试更省力.然后,您可以自由地将问题#2最小化,但我仍然认为#3会让您得出结论,将您的测试移植到javascript具有较低的LOE.

appium+python+selenium测试配置

appium+python+selenium测试配置

前提是配置好了adb环境变量,安装了python

1. 安装appium server

分享图片

下载地址  :    http://appium.io/

2. 安装appium client和selenium

在cmd中输入     pip install selenium   

                          pip install Appium-Python-Client

如果出现retrying问题, 使用带pip源的命令,如   

pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple/

pip install Appium-Python-Client -i https://pypi.mirrors.ustc.edu.cn/simple/

3. 编写脚本,代码中需要包含对appium server的设置, 可以根据实际需要增/删设置项, 如

 
 
# -*- coding: utf-8 -*-
from appium import webdriver
from time import sleep

CAPS = {
    "deviceName": " MEIZU_E3","platformName": "Android","platformVersion": "7.1.1",#‘app‘ = ‘E:/autotestingPro/app/UCliulanqi_701.apk‘  #指向.apk文件,如果设置appPackage和appActivity,那么这项会被忽略
    "appPackage": " com.meizu.flyme.flymebbs","appActivity": ".ui.LoadingActivity",#"noreset": True,
}

driver = webdriver.Remote(http://localhost:4723/wd/hub,CAPS)
sleep(3)

4. 打开appium server, 设置主机为 127.0.0.1,设置端口为 4723, 启动server

5. 连接手机,安装应用,运行脚本。完整测试脚本如下例(用Unittest):

# coding: utf-8
import
unittest from appium import webdriver from selenium.webdriver.support.ui import webdriverwait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By desired_caps = {platformName: Android,platformVersion: 5.1.1,deviceName: MEIZU_E3,"appPackage": " com.meizu.flyme.flymebbs",} appium_server = http://localhost:4723/wd/hub class LearnAppiumTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Remote(appium_server,desired_caps) def tearDown(self): self.driver.quit() def test_01(self): text_view = self.driver.find_element_by_id("text_view") assert text_view.text == Hello World! Hello World! # 测试应该不通过 def test_02(self): wait = webdriverwait(self.driver,6) wait.until(EC.element_to_be_clickable((By.ID,button))) button = self.driver.find_element_by_id("button") button.click() wait = webdriverwait(self.driver,6) wait.until(EC.presence_of_element_located((By.ID,text_view))) text_view = self.driver.find_element_by_id("text_view") assert text_view.text == 3 # 测试应该通过 if __name__ == __main__: unittest.main()

Django学习系列2:django环境中安装selenium并查看selenium版本号

Django学习系列2:django环境中安装selenium并查看selenium版本号

在Django环境中安装selenium

(django) root@ranxf-TEST:/studydisk/Python_web_TDD/superlists# conda install selenium

查看版本selenium版本号

(django) root@ranxf-TEST:/studydisk/Python_web_TDD/superlists# pip show selenium
Name: selenium Version:
3.141.0

或者:

(django) root@ranxf-TEST:/studydisk/Python_web_TDD/superlists# python
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
>>> print(selenium.__version__)
3.141.0

 

Django测试用例和Selenium服务器如何使用相同的数据库?

Django测试用例和Selenium服务器如何使用相同的数据库?

我有一个Django(v1.4,使用Postgresql)项目,为此我编写了许多工作单元测试。它们使用FactoryBoy生成大多数数据。

我现在开始使用LiveServerTestCase和Selenium
编写一些集成测试。我刚刚意识到我的测试和实时测试服务器使用不同的数据库。这意味着Selenium无法使用工厂在我的测试中创建的数据。

我不确定最好的进步方式。我 认为 我可以使用固定装置来提供可以正常工作的数据,尽管现在使用工厂来解决这个问题实在是很痛苦。

有没有办法我可以继续使用工厂来生成适用于我的Selenium测试的数据?确实,我希望我的测试和LiveServerTestCase使用相同的数据库。

今天关于带有selenium测试的Django通道2失败selenium ddt的讲解已经结束,谢谢您的阅读,如果想了解更多关于AngularJs使用Java编写的现有Selenium测试进行端到端测试、appium+python+selenium测试配置、Django学习系列2:django环境中安装selenium并查看selenium版本号、Django测试用例和Selenium服务器如何使用相同的数据库?的相关知识,请在本站搜索。

本文标签: