期望一个元素的类属性中包含特定字符串。

huangapple go评论82阅读模式
英文:

How to expect an element to have a specific string it the class attribute

问题

I would like check if an element has a specific string in the class attribute.
我想检查一个元素的class属性中是否包含特定的字符串。

The toHaveClass assertion looks promising but does not seem to allow to check for one specific class string.
toHaveClass断言看起来很有前途,但似乎不允许检查一个特定的类字符串。

Assuming the following element <div class="a b">, I would like to check for a using await expect(myLocator).toHaveClass('a') but this does not work because the api would expect class to be "a b".
假设以下元素 <div class="a b">,我想使用 await expect(myLocator).toHaveClass('a') 来检查是否包含 a,但这不起作用,因为 API 期望 class 为 "a b"。

Is there is a simple way to exist an element to have one specific string in its class list?
是否有一种简单的方法来检查元素是否存在其类列表中的一个特定字符串?

英文:

I would like check if an element has a specific string it the class attribute.
The toHaveClass assertion looks promising but does not seem to allow to check for one specific class string.

Assuming the following element &lt;div class=&quot;a b&quot;&gt;, I would like to check for a using await expect(myLocator).toHaveClass(&#39;a&#39;) but this does not work because the api would expect class to be "a b".

Is there is simple way to exist an element to have one specific string in its class list?

答案1

得分: 2

你可以使用正则表达式进行部分匹配,参见:https://playwright.dev/docs/api/class-locatorassertions#locator-assertions-to-have-class

英文:

You can use a Regex if you want to do a partial match, see: https://playwright.dev/docs/api/class-locatorassertions#locator-assertions-to-have-class

答案2

得分: 1

如果你正在按照[这个答案](https://stackoverflow.com/a/76034150/6243352)中描述的方式使用正则表达式进行测试,请确保在单词边界上使用 `\b`:

```js
import {expect, test} from &quot;@playwright/test&quot;; // ^1.30.0

test(&quot;has class &#39;a&#39;&quot;, async ({page}) =&gt; {
  await page.setContent(&#39;&lt;div class=&quot;a b&quot;&gt;&lt;/div&gt;&#39;);
  await expect(page.locator(&quot;div&quot;)).toHaveClass(/\ba\b/);
});

test(&quot;does not have class &#39;a&#39;&quot;, async ({page}) =&gt; {
  await page.setContent(&#39;&lt;div class=&quot;aaa b&quot;&gt;&lt;/div&gt;&#39;);
  await expect(page.locator(&quot;div&quot;)).not.toHaveClass(/\ba\b/);
});

没有单词边界,测试 2 会失败,将类 a 检测为 aaa 的子字符串。

另请参阅如何在playwright中获取选择器的 classList使用playwright检查元素类是否包含字符串,这两者展示了如何测试多个类,无论顺序如何。

英文:

If you're testing with regex as described in this answer, make sure to use \b for word boundaries:

import {expect, test} from &quot;@playwright/test&quot;; // ^1.30.0

test(&quot;has class &#39;a&#39;&quot;, async ({page}) =&gt; {
  await page.setContent(&#39;&lt;div class=&quot;a b&quot;&gt;&lt;/div&gt;&#39;);
  await expect(page.locator(&quot;div&quot;)).toHaveClass(/\ba\b/);
});

test(&quot;does not have class &#39;a&#39;&quot;, async ({page}) =&gt; {
  await page.setContent(&#39;&lt;div class=&quot;aaa b&quot;&gt;&lt;/div&gt;&#39;);
  await expect(page.locator(&quot;div&quot;)).not.toHaveClass(/\ba\b/);
});

Without the word boundaries, test 2 would fail, detecting the class a as a substring of aaa.

See also How to get classList on a selector in playwright? and Check if element class contains string using playwright which show how to test multiple classes in any order.

答案3

得分: 0

是的,您仍然可以使用 toHaveClass:

await expect(locator).toHaveClass('a b');

toHaveClass 也允许在其函数参数中使用字符串。

期望一个元素的类属性中包含特定字符串。

英文:

Yes , you can still use toHaveClass:

await expect(locator).toHaveClass(&#39;a b&#39;);

toHaveClass allows string as well in its function arguments.

期望一个元素的类属性中包含特定字符串。

答案4

得分: 0

Sure, here's the translated code snippet:

(Tested!!) 验证在定位器的类属性中是否存在字符串

let className = await this.page.$eval(locator, element => element.className);
console.log("className--> " + className);
await expect(className).toContain('SOMETHING');

Please note that I've only translated the code part as per your request.

英文:

(Tested!!) To verify presence of an string inside class property of locator:

let className= await this.page.$eval(locator, element =&gt; element.className)
  console.log(&quot;className--&gt; &quot;+className)
  await expect(className).toContain(&#39;SOMETHING&#39;)

huangapple
  • 本文由 发表于 2023年4月17日 18:12:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定