Protractor - see text on page

I am trying to write a test in Protractor without linking my test strongly to the specific page layout.

For example, with a typical login page, I would like to verify that if the credentials are incorrect, an error message appears.

The way my markup displays this error:

<div class="alert alert-danger">
    <ul>
        <li>Invalid username or password.</li>
    </ul>
</div>

This unordered list may contain other errors, so I don’t want to make a statement in the list item itself. In any case, I can decide not to display the error in the form of a list and may want to show it in some other way.

I just want to claim that the page contains: Invalid username or password.

I would like to do something like:

expect(page.getContents()).to.contain('Invalid username or password.'); 

But of course this does not work.

- , Protractor, ?

+4
2

element.getText().

https://angular.imtqy.com/protractor/#/api?view=webdriver.WebElement.prototype.getText:

(.. CSS) innerText , , , - .

. expect(text).toContain("Invalid username or password")

angularjs.org:

 browser.driver.get('https://angularjs.org/')
    el = element(by.css('.first'))
    el.getText().then(function(text){expect(text).toContain("AngularJS is a toolset for")});
+5

xPath DOM, , , , .

var myText = "Invalid username or password.";
var selectedElement = element(by.xpath("//*[. = '" + myText + "']"));

expect(selectedElement.isPresent()).to.eventually.equal(true, "This text is not present in page.").and.notify(callback);

. , chai-as-, , , sintax . , : expect(selectedElem)... .

+2

Source: https://habr.com/ru/post/1615735/


All Articles