Ilya Tregubov
Ilya Tregubov

Reputation: 5

How to check if one of several elements exists?

How to check if one of elements exists?

I know there is a function assertExists, but it only checks for one specific element. I need to check whether one of three elements exists on a page (for example I have a login link which is of different class for different sites, but normally I can group these sites in three categories). Is there any 'and/or' conditions that can be inserted in assertExists?

Upvotes: 0

Views: 562

Answers (1)

Artjom B.
Artjom B.

Reputation: 61922

You have like always with CasperJS multiple ways to achieve this.

The most versatile way is to use casper.exists:

test.assert(casper.exists("selector1") || casper.exists("selector2")); // 1
test.assert(casper.exists("selector1") && casper.exists("selector2")); // 2

Then you could use , to concatenate two CSS selectors or | for two XPath expressions, which makes both an OR operation

test.assertExists("selector1, selector2");
test.assertExists(x("selector1 | selector2"));

If you want an AND operator between the selectors, you can just break this into two statements, because you're testing and one of them will fail:

test.assertExists("selector1");
test.assertExists("selector2");

I used only two selectors, but this also works the same way for 3 or more selectors.

Upvotes: 1

Related Questions