What is the best way to integrate a block of code containing loops into the intern framework?

Currently, I am working with intern framework and selenium. My goal is to create a loop that can search for elements within a table. This loop will identify each element and store them in an array so that operations can be performed on them later.

The basic concept is as follows:

browser.wait(2000)            
.then(function () {
    while (true) {
    ifHasElements=browser.isDisplayed("/html/body/div[1]/div[5]/div[3]/div/div[3]/div[1]/table[2]/tbody/tr["+contRowsTable+"]").end()                    
    if (ifHasElements) {
        console.log("into if")                         
        browser.elementByXPath("/html/body/div[1]/div[5]/div[3]/div/div[3]/div[1]/table[2]/tbody/tr["+contRowsTable+"]/td[1]")
        .clickElement()
        .end()                    
        rows[contRowsTab]=browser.elementByXPath("/html/body/div[1]/div[5]/div[3]/div/div[3]/div[1]/table[2]/tbody/tr["+contRowsTable+"]")              
        } else {
           break
         }
    contRowsTab++;
    contRowsTable++;                  
    }
})

I am unsure whether it is possible to run a loop and simultaneously fetch elements within the 'then' block. Any assistance on this matter would be greatly appreciated. Thank you.

Answer №1

Consider trying something along these lines:

let visibleRows = [];
let stopStorage = false;

return browser
    .wait(2000)
    .then(function () {
        return this.parent
            // locate all rows
            .findAllByXpath('/html/body/div[1]/div[5]/div[3]/div/div[3]/div[1]/table[2]/tbody/tr')
            .then(function (rows) {
                // store visible rows until an invisible row is found
                return rows.reduce(function (promise, row) {
                    return promise.then(function () {
                        return row
                            .isVisible()
                            .then(function (isVisible) {
                                if (!isVisible) {
                                    stopStorage = true;
                                }
                                if (isVisible && !stopStorage) {
                                    visibleRows.push(row);
                                }
                            });
                    });
                }, this.parent);
            })
            .then(function () {
                // for each visible row, simulate a click on the first cell
                return visibleRows.reduce(function (promise, row) {
                    return promise
                        .findByXpath('./td[1]')
                        .click()
                        .end();
                }, this.parent);
            });
    });

This code identifies and retains the initial sequence of observable rows. Subsequently, it clicks on the very first cell in each of those rows.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Finding the precise XPath for a specific column title with C# and the Selenium WebDriver: A step-by-step guide

My dilemma is with the HTML snippet below, which I'm attempting to locate using XPath. Despite my efforts, the following code doesn't seem to be functioning as expected: <label class="ms-Label headerText-269" title="Reference Id ...

Using JavaScript to dynamically alter the background image of an HTML document from a selection of filenames

Just starting out with JavaScript and working on a simple project. My goal is to have the background image of an HTML document change to a random picture from a directory named 'Background' every time the page is opened. function main() { // ...

Modify the location of the input using jQuery

Hey there, I've got this snippet of HTML code: <div class='moves'> <div class='element'><input type='text' value='55' /></div> <input class='top' type='text&apo ...

What is the best way to filter an array based on the property of its inner array?

I am grappling with the following array of objects: let a = [ { b: [1, 11, 12], c: "a1" }, { b: [2, 56, 1], c: "a2" }, { b: [1, 2, 3], c: "a3" } ] In search of an operation that is b ...

"One way to embed a YouTube video player in a webpage is by replacing the YouTube link with an iframe YouTube video player,

As the administrator of a forum, I am looking for a way to automatically convert any YouTube video links posted by users into embedded iframe players on my ASP.NET+C# platform. Is there a method to replace this Link http://youtu.be/uN-A8eHte1g with < ...

Navigating through the React components using react-router and accessing the props can

Is there a way to access this.props from the <Router>'s onUpdate function? I need to know the current route and access this.props.route in order to identify the page (name). Unfortunately, I haven't found any resources on how to achieve thi ...

Utilizing RequireJS with Laravel 4

I am trying to use require js and laravel to manage the assets directory. I have placed the require.js file in the assets/js directory, along with main.js. The main.js file contains: require.config({ baseURL: '', paths: { userPre ...

What is the best way to eliminate duplicate data from a JSON file?

In the code snippet below, there is a function that queries and retrieves data from a getjson. The issue at hand is how to filter out repeated results so that they do not appear. Is there a way to prevent duplicate data from being displayed? var cuit ...

Node.js removes leading zeroes from dates

I am looking to generate a string with the current date and time in the format: "2021-06-02 09:37:38" const today = `${new Date().toLocaleDateString('sv-se')} ${new Date().toLocaleTimeString('sv-se')}`; console.log(today); ...

Unable to access path for children through buttons in parent path

As a data scientist entering the world of frontend development, I find myself faced with the task of creating a UI at the request of my boss. Please bear with me as I attempt to explain my issue in layman's terms. Currently, I am using Vue.js and hav ...

Issue with Discord.js: Newly joined user's username appears as undefined

robot.on('guildmateEntry', person =>{ const greeting = new Discord.MessageEmbed() .setTitle(`Greetings to the realm, ${individual}!`) const room = person.guild.channels.cache.find(channel => channel.name === " ...

A comparison between Angular JSON and JSONP $promise

When making a JSON call from my controller.js: $scope.userInvestors = userInvestors.query({UserID:$scope.user.uid}, function(userInvestors) { console.log("yep yer here"); } Using this $resource: factory('userInvestors', function($resour ...

Custom keyword not found within the robot framework

I am currently facing an issue with importing a custom Python keyword into Robotframework. Here's the scenario: I have a file named file.robot with the following contents: Library SeleniumLibrary Library ../../../python_scripts/mapActions.py D ...

The submit function for Ajax is not functioning properly

<script> var markerLatitude; var markerLongitude; function initializeMap() { var centerCoordinates = new google.maps.LatLng(51.8979988098144, -2.0838599205017); var mapOptions = { zo ...

The expected origin for the sine wave animation is not being met as intended

Experimenting with three.js, I am attempting to create an animation of a sine wave that originates from where a plane is clicked with the mouse. However, there seems to be a discrepancy in the ripple's starting point as it always emanates from the mid ...

ACTUAL preventing a component from losing its focus

I have been exploring ways to effectively stop a DOM element from losing focus. While researching, I came across different solutions on StackOverflow such as: StackOverflow solution 1 StackOverflow solution 2 StackOverflow solution 3 However, these sol ...

The integration of async/await functionalities is not compatible with promisify

I'm encountering an issue with using the util.promisify function in my code. It seems like I am having trouble grasping how promises and promisify work together. When I use new Promise, everything works fine, but when I switch to using promisify, it d ...

What could be causing this issue in the JavaScript code?

Can anyone explain why this code isn't functioning correctly? Although it prints "generating fish," nothing is being printed after that. function fish(x, y, degree, genes, Snumber) { this.x = x; this.y = y; this.d ...

Localhost file not refreshing

I'm feeling quite frustrated at the moment. It seems like such a simple issue, but I can't seem to figure it out. I have created a basic webpage with a "tree-view" structure. In my readjson.js file, I am reading from a json file located in json/ ...

Locating the source of the function call within a file in Node.js

Is it possible to determine the location of a file if a method from a module is called within that file? For example: // my-module.js module.exports = { method: function () { // I would like to know the path to my-app.js here } } // my-other-mod ...