本文介绍了Protractorjs 测试 - 如何使用 :contains('someText') - 可能错误地转义字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要测试的角度形式,我想通过其文本节点内容选择一个没有身份的项目.在其他平台上的 jquery 和 selenium 中,我能够使用一个名为 :contains() 的特殊 css 选择器,它允许我找到东西

I have an angular form that I need to test and I want to select an item without an identity, via its textnode contents. In jquery and selenium on other platforms, I was able to use a special css selector called :contains() which allows me to find stuff

ptor.findElement(protractor.By.css('label:contains(\'some text\') > input')).getAttribute('value').then(function (value) {
    expect(value).toContain('myExpectedValue');
});

当我运行这个时,我得到一个关于无效字符串的词法错误.我尝试了多种方法来转义字符串中的引号.我还尝试了一个 xpath 表达式,它在引入引号后做了同样的事情.它看起来像这样:

When I run this I get a lexical error about invalid string. I've tried a variety of ways to escape the quotes in the string. I have also tried an xpath expression, which did the same thing after introducing quotes. It looked something like this:

ptor.findElement(protractor.By.xpath('//label[text()="some text"]/descendant::input[1])')).getAttribute('value').then(function (value) {
    expect(value).toContain('myExpectedValue');
});

同样失败了.

1: :contains selenium 函数在量角器中可用吗?
2:我的字符串转义是错误的吗?

1: Is the :contains selenium function available in protractor?
2: Am I escaping my strings wrong?

请不要告诉我将身份附加到对象上.我不允许修改标记.

Please don't tell me to attach an identity to the object. I am not allowed to modify the markup.

推荐答案

  1. 您不能将 :contains 与 protractor.By.css 定位器一起使用,因为 :contains 不是 CSS3 规范的一部分(请参阅 这里)

我不知道你的 xpath 有什么问题,但这不是逃避.我在量角器中使用过这样的 xpath 字符串.

I don't know what's wrong with your xpath, but it's not the escaping. I've used xpath strings like that in protractor.

更新:

为了解决‘:contains’选择器的缺失,为此创建了一个新的定位器:

In response to the lack of ‘:contains’ selector a new locator was created for this purpose:

示例:By.cssContainingText('ul .pet', 'Dog') 将查找所有 ul 子类,类为 'pet' 包含文本'Dog'.在此处

Example: By.cssContainingText(‘ul .pet’, ‘Dog’) will find all ul children with class ‘pet’ containing the text ‘Dog’. Read more about it here

注意:您可能需要更新,因为旧版本的量角器没有这个选择器.

Note: you might need to update since older versions of protractor don't have this selector.

这篇关于Protractorjs 测试 - 如何使用 :contains('someText') - 可能错误地转义字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:43