Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting elements with the same selector in webdriver.io

I am using webdriver.io with chai and mocha for testing.

In one of my tests I need to count how many elements with the same CSS class are in the page. None of the webdriver.io API seems to return an array.

How can it be achieved?

like image 687
syymza Avatar asked Apr 24 '14 16:04

syymza


People also ask

What is $$ in WebDriverIO?

The $$ command is a short way to call the findElements command in order to fetch multiple elements on the page. It returns an array with element results that will have an extended prototype to call action commands without passing in a selector.

Can we use XPath in WebDriverIO?

In WebDriverIO also, you can write absolute XPath and relative XPath.

How do you select an element in WebDriver io?

You can use the UI Automator API, in particular the UiSelector class to locate elements. In Appium you send the Java code, as a string, to the server, which executes it in the application's environment, returning the element or elements.

Which of these finds the element based on the CSS selector engine with WebDriver?

Using inner text helps identify and create CSS Selectors in Selenium WebDriver by utilizing a string pattern that the HTML Tag manifests on the web page. This mechanism is used most frequently to locate web elements on account of its simplified syntax.


2 Answers

This is how you do it:

client.elements('.myElements', function(err,res) {
    console.log('element count: ',res.value.length);
});

Explanation: with elements you fetch all elements according given selector. It returns an array of webdriver elements which represents the amount of existing elements on the page.

like image 163
ChristianB Avatar answered Sep 29 '22 09:09

ChristianB


For version 4 of webdriver.io, this is the way

client.elements('.selector').then(function (elems) {
    console.log(elems.value.length);
});
like image 43
isHristov Avatar answered Sep 29 '22 10:09

isHristov