Protractorjs testing - how to use :contains('s

2019-07-02 00:15发布

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');
});

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');
});

That failed the same way.

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.

标签: protractor
1条回答
做自己的国王
2楼-- · 2019-07-02 00:43
  1. You can't use :contains with the protractor.By.css locator because :contains is not part of the CSS3 spec (see here)

  2. 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.

Update:

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

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.

查看更多
登录 后发表回答