gpt4 book ai didi

How to expect count bigger or smaller than?(如何期望计数大于或小于?)

转载 作者:bug小助手 更新时间:2023-10-22 15:47:01 30 4
gpt4 key购买 nike



Using Playwright and Python, how can I expect for count bigger or smaller than?

使用Playwright和Python,我怎么能期望计数大于或小于?


For example, this code expect for count of 2.

例如,此代码期望计数为2。


How do I achieve count >= 2 (Only bigger)

如何实现计数>=2(仅更大)


expect(self.page.locator('MyLocator')).to_have_count(2, timeout=20 * 1000)

更多回答

Exactly what are you testing where you need this type of unusual assertion?

在需要这种不寻常断言的地方,你到底在测试什么?

As per the conditions provided, any value will be true ?

根据提供的条件,任何值都是真的?

There are items in the list or table. Their count can be 2 or above, not just count equals to 2.

列表或表中有个项目。他们的计数可以是2或以上,而不仅仅是计数等于2。

"How do I achieve count > 2 or count <=2" as mentioned in the question- this includes any number.

问题中提到的“我如何实现计数>2或计数<=2”-这包括任何数字。

The request isn't clear but I assume you want .to_be_greater_than() and .to_be_less_than_or_equal_to() matchers separately, not at the same time. I suggest editing the post to clarify this, since there is a good deal of confusion in the answers and comments here. That said, I don't think this is possible, so you'll probably need to use a plain non-web-first assertion like assert page.locator('MyLocator').count() > 2 unless someone comes up with something better using expect, or a new matcher is added to the API.

请求不清楚,但我认为您需要分别匹配.to_begreater_than()和.to_be_liss_than_or_equal_to(),而不是同时匹配。我建议编辑这篇文章来澄清这一点,因为这里的答案和评论有很多混乱。也就是说,我认为这是不可能的,所以你可能需要使用一个普通的非web-first断言,比如assert page.locator('MyLocator').count()>2,除非有人用expect想出了更好的东西,或者在API中添加了一个新的匹配器。

优秀答案推荐

Playwright doesn't have options to use bigger/smaller in toHaveCount/to_have_count.

Playwright没有在toHaveCount/to_have_count中使用更大/更小的选项。


I guess you can rewrite it to Python easily.

我想你可以很容易地把它重写成Python。


const count = await page.locator("MyLocator").count();
expect(count).toBeGreaterThan(2);
expect(count).toBeLessThanOrEqual(2);

The only limitation of this approach is that you won't have expect retry feature because you check the int value, not a Locator. So you might wanna check for the visibility of the Locator first.

这种方法的唯一限制是,您不会期望有重试功能,因为您检查的是int值,而不是Locator。因此,您可能需要先检查定位器的可见性。


const locator = page.locator("MyLocator");
await expect(locator.last()).toBeVisible(); // or first()
const count = await locator.count();
expect(count).toBeGreaterThan(2);
expect(count).toBeLessThanOrEqual(2);

Also, PW for NodeJS has the ability to create custom matches, but I'm not sure it's an option for Python.

此外,PW for NodeJS能够创建自定义匹配,但我不确定这是否是Python的选项。



This doesn't seem to be possible in the current Python Playwright API, but you could use wait_for_function as a workaround:

在当前的Python Playwright API中,这似乎是不可能的,但您可以使用wait_for_formation作为解决方法:


page.wait_for_function("document.querySelectorAll('.foo').length > 2")

This is web-first and will wait for the predicate, but the error message once it throws won't be as clear as the expect failure.

这是web优先的,将等待谓词,但一旦抛出错误消息,就不会像预期的失败那样清晰。


If the count is immediately available and you don't need to wait for the predicate to be true, assert is useful to mention as another possibility:

如果计数立即可用,并且您不需要等待谓词为true,那么断言作为另一种可能性是有用的:


assert page.locator(".foo").count() > 2

If you're using unittest, you can replace assert with, for example

如果您使用的是unittest,您可以用替换assert,例如


self.assertGreaterEqual(page.locator(".foo").count(), 2)
self.assertGreater(page.locator(".foo").count(), 2) # or

Yet another workaround if you're dealing only with small numbers of elements:

如果您只处理少量元素,还有另一种解决方法:


page.locator(".foo")
expect(loc).not_to_have_count(0)
expect(loc).not_to_have_count(1)
expect(loc).not_to_have_count(2)

Here, we ensure the count is greater than 2 by process of elimination. It's impossible for count to be less than 0, so we need not include that.

在这里,我们通过消除过程来确保计数大于2。计数不可能小于0,所以我们不需要包括它。


You could do this for less than as well, but not as easily, and using a reasonable assumption that there'll never be more than, say, 20 or 50 elements in any conceivable run:

你可以用不到的时间,但不那么容易,并使用一个合理的假设,即在任何可能的运行中,永远不会有超过20或50个元素:


loc = page.locator(".foo")
for i in range(2, 20):
expect(loc).not_to_have_count(i)

This ensures the count is 0 or 1, or greater than some reasonably high upper-bound.

这样可以确保计数为0或1,或者大于某个合理的上限。



Use not_to_have_count

使用not_to_have_count


expect(self.page.locator('MyLocator')).not_to_have_count(2, timeout=20 * 1000)


Just found this workaround and it worked like a charm:

刚刚找到了这个变通方法,它就像一个魅力:


"You can do expect(await locator.count()).toBeGreatedThen() as a workaround for now. It won't be waiting until the number is greater than, it will just assert it immediately, but it looks like it might work for you."

“你可以期待(wait-locator.count()).toBeGreatedThen()作为目前的解决方法。它不会等到数字大于,它只会立即断言,但看起来它可能对你有用。”


in https://github.com/microsoft/playwright/issues/8725

在里面https://github.com/microsoft/playwright/issues/8725



from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto('https://example.com') # Replace with your URL
locator = page.locator('MyLocator')

# Wait for the element to appear within a timeout
locator.wait_for_selector(timeout=20000)

# Get the count of elements matching the locator
count = locator.count()

# Check if count is greater than 2 or less than or equal to 2
if count > 2:
print(f"Count is greater than 2: {count}")
elif count <= 2:
print(f"Count is less than or equal to 2: {count}")

browser.close()

use this and let me have your respect if it works good.
'''ERFAN MOGHIS'''

用这个,如果效果好的话,请尊重我ERFAN MOGHIS


更多回答

I don't think there's an easy way to rewrite this in Python Playwright. If there is, can you add that to the answer? As for the JS code here, it's best to use web-first assertions.

我不认为有一个简单的方法可以在Python Playwright中重写这一点。如果有,你能把它加到答案中吗?至于这里的JS代码,最好使用web优先断言。

self.assertGreaterEqual() does NOT compile in Python. Please update your answer.

self.assertGreaterEqual()不在Python中编译。请更新您的答案。

It's part of unittest: assertGreaterEqual, which is built into the standard library.

它是单元测试的一部分:assertGreaterEqual,它被构建在标准库中。

And If you use Pytest?

如果你使用Pytest?

Pytest just uses normal assert I think. I don't use the library much. My point wasn't to give a comprehensive overview of greater than assertions in different libraries, just to let you know that you can use an assertion library of your choice and a non-web-first assertion as a workaround, assuming the predicate is immediately available.

Pytest只是使用了正常的断言。我不怎么使用图书馆。我的观点并不是全面概述不同库中的大于断言,只是想让您知道,假设谓词立即可用,您可以使用自己选择的断言库和非web优先断言作为变通方法。

And if count is 0 or 4 - both cases will pass. I need greater or smaller than...

如果计数是0或4,两种情况都会通过。我需要大于或小于。。。

@TalAngel both 0 and 4 are greater or smaller than 2...

@TalAngel 0和4都大于或小于2。。。

But I want only greater than 2 Or only smaller than 2. Using your code is guessing.

但我只想要大于2或小于2。使用您的代码就是猜测。

As far as I know you will have to implement your own function.

据我所知,你将不得不执行你自己的职能。

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

您的回答可以通过其他支持信息得到改进。请编辑以添加更多详细信息,如引文或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com