Selenium-webdriver causing promise to return `undefined`

I have encountered an issue where a function is returning undefined instead of the size of an element. Due to my limited knowledge of promises, I am struggling to rewrite it in a way that returns a resolved promise instead of undefined. How can I modify it to achieve this desired outcome?

When checking the console, I see the message:

Expected undefined to be less than undefined.

var s1 = utilitiesPageObject.getElemSize("css", "#main-content > div > div > div.col-xs-12.col-md-6.align-left");
driver.findElement(By.css('#main-content > div > div > div.col-xs-12.col-md-6.align-left > div.slide-down-container > div.slide-down-btn.btn')).click()
var s2 = utilitiesPageObject.getElemSize("css", "#main-content > div > div > div.col-xs-12.col-md-6.align-left")
expect(s1).toBeLessThan(s2);

The above code snippet is calling the getElemSize function from utilitiesPageObject.

getElemSize(css, elem){
    switch(css) {
        case 'css':
            this.driver.findElement(By.css(elem)).getSize().getHeight.then(s => {return s});
            break;
        case 'xpath':
            this.driver.findElement(By.xpath(elem)).getSize().getHeight.then(s => {return s});
            break;
        default:
            return null;
    }
}

Answer №1

Don't forget to include the return statement in your getElemSize function:

getElemSize(css, elem){
switch(css) {
    case 'css':
        return this.driver.findElement(By.css(elem)).getSize().getHeight.then(s => {return s});
        break;
    case 'xpath':
        return  this.driver.findElement(By.xpath(elem)).getSize().getHeight.then(s => {return s});
        break;
    default:
        return Promise.reject();
 }
}

Now getElemSize will return a promise.

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

The absence of data in a c# web api controller is causing issues with jQuery AJAX post requests

When I am sending data to a c# web api controller, I use the following method: $.ajax({ type: "POST", url: "menuApi/menu/Cost", data: JSON.stringify(order), contentType: "application/json", success: function (data) { window.alert(&apo ...

Looping through items using ng-repeat according to the chosen option in a select dropdown

After expanding my project, I encountered an issue with the main object structure: { screens: [{ id: 0, name: 'Screen0', sections: [{ id: 0, sectionItems: [] }, { id: 1, sectionItems: [] }, { i ...

Validation of links using Robot Framework Selenium

Is there a way to execute a keyword only if a specific link exists on the page? If the link doesn't exist, can we continue the test as normal? I attempted it like this but unfortunately it didn't work: ${Result}= Page Should Contain Link ${link ...

Can a sophisticated text editor be utilized without a content management system?

Many website builders utilize rich text editors as plugins to enhance content creation, such as in CMS platforms like Joomla and WordPress. However, can these same editors be easily integrated into a custom website built from scratch using just HTML, PHP ...

Add a variable to an existing array

As a beginner in the world of programming, I usually rely on online resources to solve my coding problems. However, this time I'm facing an issue that I can't find the solution to online. I have been trying to push a variable into an array and th ...

The onTableChange function in Mui-Datatable does not seem to be functioning properly when attempting to

Utilizing mui-datatable, I came across an official example on codesandbox where setState can be used on the tableState. You can check out the example here: https://codesandbox.io/s/trusting-jackson-k6t7ot?file=/examples/on-table-init/index.js handleTableIn ...

Having trouble sharing content from my React next.js website on social media due to issues with open graph meta tags

I'm currently facing an issue with my Next.js application when trying to share content on Facebook and Twitter. I've made sure to include the necessary open graph meta tags in the document's head section and I'm using React Helmet to dy ...

Struggling to save the resolved data from a promise in Node.js to a variable

I am currently utilizing nodejs to develop REST APIs and I am facing an issue with storing resolved data from multiple promises into a single variable. Here is a snippet of the code I am working with: var resultset={}; function getAllTeams() { retu ...

Loop through a JSON object

I am looking to iterate through a JSON object called transactionByFop. Here is the HTML code: <div ng-show="searchController.orderCAT.results.length" ng-repeat="(key,obj) in searchController.orderCAT.results track by $index"> <dl sty ...

Adding a div element with a background behind a text element using Jquery

Is there a way to position an absolute div behind text or images while staying in front of background images? I have content with background images behind the text, so setting the z-index of the absolute div to -1 won't work. Any suggestions on how to ...

What is the best way to display JSON response as code instead of a string in AngularJS?

When retrieving my article from the database as a JSON object, I encounter an issue with the body content. The HTML codes in the body are displayed as strings within double quotation marks by AngularJS. How can I resolve this? Angular controller snippet: ...

Connect a parent node to a specific subset within Mermaid's graph structure

Managing a complex Mermaid diagram that includes numerous subgraphs can be challenging. The size of the diagram often makes it difficult to maintain the correct order of subgraphs, leading to occasional rearrangements for clarity and positioning. However, ...

What is the best way to ensure that JavaScript runs smoothly following the dynamic loading of a user control into a div using

I need assistance with integrating a Web User Control containing JavaScript and CSS blocks into my main page using jQuery for dynamic loading. How can I ensure that the alert('haha') function executes when the user control is loaded within the "d ...

Retrieving components from Ajax response data

While I have a good grasp of PHP, diving into AJAX and dealing with JSON is proving to be quite challenging for me. My PHP script simply delivers a straightforward JSON string like this: {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": ...

Convert a Div into an Image using jQuery/JavaScript

I have been attempting to convert a div to an image using the html2canvas library from this link. Unfortunately, I have not had success in converting the full div to an image as the dropper is missing in the final result. URL: Visit this URL for reference ...

Utilizing ng-disabled with a custom directive

Is it possible to achieve the following: <directiveName parameter1=value1 parameter2=value2 ng-disabled="true"> </directiveName> I tried this but couldn't get it to work and didn't find many examples of its use. However, I can togg ...

5 steps to create a versatile function for activating attributes based on their values

Hey everyone! I was working on creating this calculator and I had different options to implement it, but I wanted to do it in a specific way. <form action=""> <label for="num1">Number A</label><br> <input type="number" na ...

Issue with ESLint arises following the installation of npm create-react-app package

ESLint is showing Invalid Options: - Unknown options: env, parserOptions, rules. The 'parserOptions' has been removed and you should now use the 'overrideConfig.parserOptions' option instead. Similarly, the 'rules' have been r ...

What's the best way to repeatedly run a scenario with various input data in SpecFlow?

Seeking guidance on the most efficient way to execute my scenario multiple times using SpecFlow with different input data. While I am familiar with Scenario Outline & Examples/Tables, I'm struggling to find a solution to my specific need. The task at ...

Determining the starting and ending position of text within an element using jQuery

Delving into the world of jQuery/JS has been both challenging and exciting for me. However, I've encountered a problem that's got me stumped. Here is a snippet of text that I need to work with: blablablabla <b data-start="" data-end="">#fo ...