Promise fires off prematurely

The WebDriverJS API provides code examples like the following:

driver.get("http://www.google.com");
driver.findElement(webdriver.By.name("q")).sendKeys("webdriver");
driver.findElement(webdriver.By.name("btnG")).click();
driver.getTitle().then(function(title) {
 console.log(title);
 //assertEquals("webdriver - Google Search", title);
});

In this case, the title is "Google". It's also possible to modify the code as shown below:

driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("webdriver");
driver.findElement(By.name("btnG")).click().then(function(){
  /*WebDriverWait.until(function() {
    driver.getTitle().then(function(title) {
      console.log(title);
      title = "webdriver - Google Search"
    });
  });*/

  driver.sleep(3000).then(function() {
    driver.getTitle().then(function(title) {
      console.log(title);
    });
  });
});

After these modifications, the title becomes "webdriver - Google Search". The status of the promise changes before the page finishes loading. This example is taken from the official API documentation and should be functional.

I came across an example of waiting on Stack Overflow, but I'm unsure how to implement it in working code using WebDriverJS since the API does not mention the "until" method.

Answer №1

If you want to see an example of how to utilize the wait and until functions, check out this snippet of code below:

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
  return driver.getTitle().then(function(title) {
    console.log(title);
    return title === 'webdriver - Google Search';
  });
}, 1000).then(function() {
  console.log('done!')
});

driver.quit();

This code will produce the following output:

webdriver - Google Search
done!

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

Accessing a local file with JavaScript using JSON (unanswered inquiries from Stack Overflow)

I need assistance in reading and parsing a local .JSON file using JavaScript. Specifically, I am looking for an example code snippet that successfully accomplishes this task. The code I currently have is unable to load the local file. var xmlhttp = new XM ...

Error encountered in Python and Selenium when trying to retrieve attributes from an iframe

While trying to test some cookie accept code, I encountered an exception when working with the get_attribute method. Below is the code snippet I have: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.c ...

Retrieving a Node.js variable from a front-end JavaScript file

Is there a way to access a node.js variable from the backend within a javascript file using pug? It appears to be functional when utilizing inline javascript in the pug template, however, I am encountering difficulties when trying to link to a .js file. ...

Issue with Angular 5: Unable to update template within Observable sequence

When I have a component and need to display a loader Here is the component code: ngOnInit() { this.route.params .do(x => this.contentLoading = true) .switchMap(x => this.getClientSearchResults()) .subscribe(x => { ...

The average duration for each API request is consistently recorded at 21 seconds

It's taking 21 seconds per request for snippet.json and images, causing my widget to load in 42 seconds consistently. That just doesn't seem right. Check out this code snippet below: <script type="text/javascript"> function fetchJSONFil ...

React component allowing for the reuse of avatars

As a novice in React, I've encountered a challenge. My goal is to develop a simple reusable component using MUI. Specifically, I aim to create an avatar component that can be easily customized with different images when called upon. I want the flexibi ...

Send the JSON file to the server by uploading it

Situation Currently, I'm dealing with a page comprising questions structured as shown below: sections:[{ section: 1, questions:[{ question: 1, attachment: [FormData Object] ... }, { question: 2, ...

Ways to bring GIFs into NextJS

I am currently working on my portfolio website using Nextjs and I would like to incorporate gifs into the site. However, I have been struggling to figure out how to do so. Below is the code that I have been working with: https://i.stack.imgur.com/zjoiD.pn ...

Discovering the right row to input the data and successfully integrating it: What is the best

Below is the code I am using to add a new task: <fieldset> <legend>Create New Task</legend> <input type="text" ng-model="subtaskname" placeholder="Subtask Name" ><br /> <select id="s1" ng-model="selectedItem" ng-options=" ...

Running the npm install command will eliminate any external JS or CSS files that are being

For my project, I incorporated jquery-datatimepicker by including jquery.datetimepicker.min.css and jquery.datetimepicker.full.min.js in angular.json and placed them within the node_modules/datetimepick directory. Here is a snippet of my angular.json conf ...

Error Encountered: Module Missing Following NPM Installation

I just started using Node and ran into an issue after setting up a new node project with NPM init. I tried installing lodash by running the command: npm install lodash --save However, the command resulted in the following error: npm ERR! code MODULE_NOT ...

Sorting rows dynamically with jQuery Tablesorter

I've been struggling to find a solution for my specific issue. I need to have a static row in a large table that will not sort or move, allowing for repeated headers. Can anyone offer assistance with this? $("#dataTable").tablesorter({ ...

Animate a list to expand and collapse the menu options

Here's the concept I'm aiming to achieve: When "PORTFOLIO" is clicked, smoothly push down everything on the page; New links should fade in smoothly; If "PORTFOLIO" is clicked again, reverse all animations. This is my current code snippet: $( ...

Exploring the capabilities of dynamic pathname routing in Next.js

Consider this scenario: there is a path that reaches me as /example/123 and I must redirect it to /otherExample/123. My code utilizes next/router, extracting the URL from router.asPath. if(router.asPath == '/example/123') { Router.push(' ...

JavaScript encountered a server error when attempting to utilize emotion/styled

import Link from "next/link"; import Image from "next/image"; import { Text, useColorModeValue } from "@chakra-ui/react"; import { styled } from "@emotion/styled" const LogoBox = styled.span` font-weight: bold; font ...

I successfully executed my first node.js http request, but it is now encountering an error

Utilizing the 'request' module in node.js for sending http requests has been successful initially. However, it is now returning an error. var request = require('request'); request('http://www.google.com', function (error, res ...

Linking a Kendo widget to an HtmlHelper component through JavaScript in MVC/Razor

I am currently working on integrating an MVC ListBoxFor control with a Kendo MultiSelectFor to bind and update data. The goal is to have a list of users in the ListBox, and a list of available users in the MultiSelect box. When users are selected from the ...

Having trouble with a FlatList component causing a blank screen in React Native?

I've been attempting to display data in a view using FlatList but instead of showing the predefined values, it's just showing a blank screen. This is what I have tried so far. Note: I purposely excluded import elements from react import { ...

React (Next) fails to trigger a re-render following a Redux state alteration (confirming that the state is indeed returned as a new object)

I'm currently experiencing an issue with re-rendering in my NextJS application after a state change. The sendMessageForm function triggers a redux action sendMessage to update the message within the state. Despite returning a new object (return ...

What language should be used for JSON data formats?

I am dealing with a JSON file named myjson.cfg that has the following structure: { "values": { "a": 1, "b": 2, "c": 3, "d": 4 }, "sales": [ { "a": 0, "b": 0, "c": 0, "d": 0, ...