小编jac*_*118的帖子

如何在python中将数据模拟为request.Response类型

我想写一些测试用例来练习isinstance(obj, requests.Response) 逻辑中的object_check。在我创建 Mock 数据作为 requests.post 的返回值之后。模拟数据的类型始终是 Mock 类。这样,我如何重写模拟数据,以便模拟数据可以是 requests.Response 类型?所以我可以锻炼线d = obj.json()

from unittest.mock import patch, Mock
import unittest
import requests
from requests.exceptions import HTTPError
import pytest
def object_check(obj):
    if isinstance(obj, bytes):
        d = ujson.decode(obj.decode())
    elif isinstance(obj, requests.Response):
        d = obj.json()
    else:
        raise ValueError('invalid type')
    return d

def service_post(params):
    """
    trivial function that does a GET request
    against google, checks the status of the
    result and returns the raw content
    """
    url = "https://www.iamdomain.com"
    params …
Run Code Online (Sandbox Code Playgroud)

python pytest python-3.x python-unittest python-unittest.mock

5
推荐指数
1
解决办法
1万
查看次数

在python中使用Selenium Webdriver从父xpath找出所有子元素xpath

我可以通过的xpath找到该元素 driver.find_element_by_xpath('//*[@id="app"]/table/tbody/tr[1]/td[1]')。但是以任何方式我可以返回所有子元素,例如tag和tag xpath?

<tr>
    <td class="">
        <div>
            <a href="/user/1">
                <!-- react-text: 6011 -->user first name |
                <!-- /react-text -->
                <!-- react-text: 6012 -->
                <!-- /react-text -->
                <!-- react-text: 6013 -->user last name
                <!-- /react-text -->
            </a>
            <div>
                <span>
            <!-- react-text: 6014 -->town<!-- /react-text -->
            <!-- react-text: 6015 -->  | <!-- /react-text -->
            <!-- react-text: 6015 -->month<!-- /react-text -->
            <!-- react-text: 6081 --> | date<!-- /react-text -->
            <!-- react-text: 6082 -->year<!-- /react-text -->
            </span>
            </div>
        </div>
    </td>
    <td class=""><a href="/address/1">1</a> …
Run Code Online (Sandbox Code Playgroud)

python selenium python-3.x selenium-chromedriver selenium-webdriver

4
推荐指数
1
解决办法
6532
查看次数

使用Selenium WebDriver等待元素的属性更改值

我有一个属性为“ aria-busy”的元素,当搜索和完成数据时,该元素从true变为false。如果达到20秒并且属性未从true更改为false,则如何使用硒预期条件和显式等待来等待默认时间(例如20秒)。抛出异常。我有以下内容,但它实际上没有用

import selenium.webdriver.support.ui as ui
from selenium.webdriver.support import expected_conditions as EC

<div id="xxx" role="combobox" aria-busy="false" /div>
class Ele:
    def __init__(self, driver, locator)
        self.wait = ui.WebDriverWait(driver, timeout=20)

    def waitEle(self):
        try:
            e = self.driver.find_element_by_xpath('//div[@id='xxxx']')
            self.wait.until(EC.element_selection_state_to_be((e.get_attribute('aria-busy'), 'true')))
        expect:
            raise Exception('wait timeout')
Run Code Online (Sandbox Code Playgroud)

python selenium python-3.x selenium-webdriver

4
推荐指数
1
解决办法
3328
查看次数

如何以相反的顺序循环列表并跳过最后一个元素

我知道我可能必须for i in x[::-1]:以相反的顺序循环遍历列表。但是如果我想跳过最后一个元素然后从高到低遍历怎么办?for i in x[-1:0:-1]不起作用

x = [1,2,3,4,5]
=> 4,3,2,1         #  desired

for i in x[-1:-1]:
    print(i)

# it does not work
Run Code Online (Sandbox Code Playgroud)

python indexing reverse list python-3.x

4
推荐指数
1
解决办法
4727
查看次数

在python3中使用没有继承类的super

有人知道下面的代码意味着什么super(xxx, self).__init__()吗?它有什么作用?我虽然它应该是 ABC 类继承自其他类,但我没有看到继承自哪个类。

class ABC:

    def __init__(self):
        super(ABC, self).__init__()
Run Code Online (Sandbox Code Playgroud)

python python-3.x

4
推荐指数
1
解决办法
381
查看次数

如何在打字稿中的 forEach 下定义类型?

我有一个PersonTypes对象数组,并且只想在 forEach 循环中使用部分键。什么是更精确、更正确的打字稿编码来提供类型?我可以做一些类似的事情 people.forEach((person: Pick<PersonTypes, 'name' | 'gender'>people.forEach((person: PersonTypes) =>{ 或者 people.forEach((person: any) =>{ 什么是在打字稿中编码的正确方法

export type PersonTypes = {
  name: string;
  value: string;
  gender: boolean;
};
const people: PersonTypes[] = [
  {name: 'apl', value: 'apple', gender: true},
  {name: 'gal', value: 'google', gender: false},
]
people.forEach((person: Pick<PersonTypes, 'name' | 'gender'>) =>{
//people.forEach((person: PersonTypes) =>{
//people.forEach((person: any) =>{
  console.log(person.name);
  console.log(person.gender);
} )
Run Code Online (Sandbox Code Playgroud)

typescript typescript1.8 typescript-typings typescript2.0

4
推荐指数
1
解决办法
7389
查看次数

如何在打字稿中使用带有变量的可选链运算符

我有以下代码,我想从变量对象的值键传递数字,如何使用变量作为可选链运算符来解决错误元素隐式具有any类型?

    function fun(i?: number) {
        console.log(i)
    }

    const variable = { min: { value: 1, name: 'google' }, max: {value: 2, name: 'apple'} }
    const variable2 = { min: { value: 1, name: 'google' } }
    const variable3 = { max: {value: 2, name: 'apple'} }

    fun(variable?.min.value) // working => 1
    fun(variable?.max.value) // working => 2
    fun(variable2?.min.value) // working => 1
    fun(variable2?.max.value) // working => undefined
    fun(variable3?.min.value) // working => undefined
    fun(variable3?.max.value) // working => 2

    Object.keys(variable).forEach((key) => …
Run Code Online (Sandbox Code Playgroud)

javascript typescript typescript1.5 typescript1.9 typescript2.0

4
推荐指数
1
解决办法
1402
查看次数

测试库验证 aria-expanded 值是否为 false

HTML打开弹出窗口后,我有一个组件按照以下代码渲染弹出窗口。我想使用测试库编写测试来验证单击弹出项目后aria-expanded的值。false有没有更好的方法让我验证 aria-expanded?

// component
    <body>
      <div>
        <div >
          <button aria-expanded="true" class="btn">
            Flyout button
          </button>
          <div class="flyoutContainer>
            <ul class="flyoutItem" role="listbox" >
              <li aria-selected="false" class="item" role="option" >
                <div class="option" >
                    <span >Data is here</span>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </body>

// test

render(<flyout />);

const item = screen.getByText(/Data is here/, {selector: 'span'});
fireEvent.click(item);

const flyoutButton = screen.getByRole('button', {name: 'Flyout button'});
expect(flyoutButton).toHaveClass('aria-expanded').;
expect(flyoutButton).toHaveAttribute('aria-expanded', 'false')
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing typescript react-testing-library

4
推荐指数
1
解决办法
5114
查看次数

当值为"None"时如何使用理解来查找dict键?

如何在值为时使用理解来查找dict键None?是否有一个班轮而不是以下代码?

def Func():
    for k, v in some_dict.items()
        if not v:
            return k
Run Code Online (Sandbox Code Playgroud)

python dictionary

3
推荐指数
1
解决办法
71
查看次数

使用其他属性名称的值创建枚举

如何创建枚举类,使其属性使用其他成员的值?喜欢我下面的代码

from enum import Enum
class ProjectPath(Enum):
    home = '~/home'
    app = '~/home/app'
    prefix = '~/home/app/prefix'
    postfix = '~/home/app/postfix'

'''
try to do something like
from enum import Enum
class ProjectPath(Enum):
    home = '~/home'
    app = f'{self.home.value}/app'
    prefix = f'{self.app.value}/prefix'
    postfix = f'{self.app.value}/postfix'
'''
Run Code Online (Sandbox Code Playgroud)

python enums python-3.x python-3.7

3
推荐指数
1
解决办法
43
查看次数