Having trouble retrieving the webpage title using Cucumber-Protractor

Hey there! I'm facing an issue with Cucumber-Protractor where I can't seem to console log the webpage title in the "Given" tag, even though all my tests are passing. I've checked my Protractor.conf.js file and it looks fine to me. Here's how it looks: Protractor.conf.js-

exports.config = {
//seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
getPageTimeout: 60000,
allScriptsTimeout: 500000,
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
capabilities: {
    'browserName': 'chrome'
},

specs: [
    'features/Login.feature'
],

cucumberOpts: {
    require: 'features/steps/logic.js',
    format: [require.resolve('cucumber-pretty')]

}};

The logic.js file has been causing some trouble:

I am having trouble console logging the "webpagetitle" using the disp() function.

const assert = require('assert')
const {Before,Given,When,Then} = require('cucumber');

var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;

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

var options = new firefox.Options();
options.addArguments("-headless");

var driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();

Given('Go to Title', function () {
function navigate(callback) {
console.log("This line gets printed - Getting to Google");
driver.get("https://www.google.com/");
callback();

}

function disp() {
console.log("This line gets printed - Getting title");
driver.getTitle().then(function (webpagetitle) {
console.log(webpagetitle);
console.log("This line does not get printed - This section seems to have an issue.");

});
}
navigate(disp);
});

When('do nothing', function () {});

Then('do nothing here also', function () {});

Your help would be highly appreciated.

Error with @yong changes: Do I need to call that done callback anywhere (as you have already called)?

1) Scenario: Add two number # features\Login.feature:5
   × Given Go to Title # practice\example6\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173
       Error: function timed out, ensure the callback is executed within 5000 milliseconds
           at Timeout._time.default.setTimeout [as _onTimeout] (C:\Users\Mohit.Garg\Desktop\Cucumber practice\example6\node_modules\cucumber\lib\user_code_runner.js:81:20)
           at ontimeout (timers.js:436:11)
           at tryOnTimeout (timers.js:300:5)
           at listOnTimeout (timers.js:263:5)
           at Timer.processTimers (timers.js:223:10)
   - When do nothing # practice\example6\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173
   - Then do nothing here also # practice\example6\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173
   √ After # practice\example6\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173

1 scenario (1 failed)
3 steps (1 failed, 2 skipped)
0m05.006s
[16:00:07] Bye selenium standalone server.
[16:00:07] Chrome #01 had 1 test failure(s)
[16:00:07] Total failed spec(s): 1
[16:00:07] Process exited with error code 1

C:\Users\Mohit.Garg\Desktop\Cucumber practice\example6>

Answer №1

Protractor is constructed on top of selenium-webdriver and utilizes a method to manage asynchronous execution. When directly working with selenium-webdriver, it is necessary to oversee the asynchronous execution of each Cucumber step function using done()

var {setDefaultTimeout} = require('cucumber');

setDefaultTimeout(60 * 1000);

Given('Navigate to Homepage', function (done) {
    function goToPage(callback) {
        console.log("Logging - Redirecting to Google");
        driver.get("https://www.google.com/").then(function(){
           callback(done);
        })    
    }

    function displayTitle(done) {
        console.log("Logging - Retrieving title");
        driver.getTitle().then(function (pageTitle) {
            console.log(pageTitle);
            console.log("Issue in this section - not being logged properly");
            // call done() here
            done()
        });
    }
    goToPage(displayTitle);
});

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

Is browser instances created by htmlunit on the local machine during execution?

Currently, I am utilizing Htmlunit for the purpose of web scraping. My task involves logging into a website on behalf of users, adjusting settings in their profiles, and then returning. This is all accomplished solely using pure Htmlunit without the Seleni ...

Add a Page to Your Domain Name using the Text Input Box

I'm looking to create an input field that allows users to enter a text string, which will be added to a domain name when submitted, redirecting the user to a specific page. Here's how the process works: The user enters 'foo' into the ...

How can I maintain the default function for links that have been clicked using window.on('click') event listener?

I am currently working on a project to visualize the spatial positions of 4673 of the closest galaxies. To enhance the user experience, I have implemented customized click events that allow users to focus on individual galaxies and even change their colors ...

Error due to PlatformLocation's location dependency issue

My AppComponent relies on Location (from angular2/router) as a dependency. Within the AppComponent, I am using Location.path(). However, when running my Jasmine test, I encountered an error. Can you help me identify the issue with my Jasmine test and guide ...

What is the process for displaying a three.js project?

I've been trying to follow a beginner's tutorial on three.js from this link, but I'm encountering an issue in part 2 where nothing seems to render on my webpage. Despite checking out other guides, I can't seem to solve the problem. I u ...

utilize angular4 to dynamically link images from twitter based on certain conditions

Currently, I am attempting to retrieve the URL of images from tweets only if the tweet contains an image. I have been successful in fetching tweets and some images, but the issue arises when a tweet does not have an image, causing it to break and stop disp ...

I am hoping for the outcome to be directed to the homepage

I'm struggling to figure this out, as I am new to classic ASP and JavaScript. I hope someone can help me understand. I want to display the response.write on the main.asp (or the result) page, but each time I try, it redirects to pass.asp on a differen ...

Tips for displaying nested JSON data in an Angular UI grid

I am working with an Angular UI grid where I need to display data from a nested JSON object. The JSON contains a lot of information, but I only want to show a selected set of properties on the grid. Instead of putting the entire json.Products into the grid ...

Can you confirm if this email address is legitimate?

"Sophie Dupont"@sample.com While diving into RFC 5321 in an attempt to fully grasp the concept of a valid email address, I find myself overcomplicating things unnecessarily. This topic has been on my mind lately. i.e., within a quoted str ...

Cease the use of bi-directional data binding on an Angular directive

One way I've been attempting to send information to a directive is through the following setup: <ui-message data="{{app}}"></ui-message> In my controller, this is how I define it: app.controller("testCtrl", function($scope) { $scope.a ...

Retrieve a JSON object from a Knockout observable array using the object's ID

I'm struggling to find examples of ko.observablearrays that deal with complex JSON objects instead of simple strings. I have an observable array containing a large JSON object with several properties, and I need to retrieve a specific object based on ...

Error detected in JSON syntax... Where is it hiding?

After running the code through jsonlint, it flagged an error on line 17. However, upon close inspection of the file which contains about 1000 lines, I couldn't pinpoint the issue. It's possible that the problem lies further down the file, but I w ...

Is there a way to calculate the total of elements in an HTML table once it has been filtered by its

This table was created by defining different classes in JavaScript, with the actual table being larger but condensed to 3 lines for easier understanding. https://i.sstatic.net/r055E.png Manually input values in the first column to calculate A, B, C, and ...

Struggling to align the h1 tag with a p tag on the same line

How can I align the balance to the upper right-hand corner of my website? I'm new to HTML and CSS and struggling to get the p tag next to the h1 tag. I've tried using display inline for the h1 tag and float right for the p tag, as well as setting ...

The sign-up modal will refresh and then close when an API call is triggered upon submission in Next.js

Within my modal component, I have incorporated a feature that displays either a sign-in or sign-up form based on certain conditions. However, I am facing an issue where upon clicking the submit button on any of these forms, the modal closes regardless of t ...

Modifying an object's attribute in React.js by toggling a checkbox

As I delve into learning React, I am constructing a straightforward todo list. Here's the object contained within my initialState: getInitialState:function(){ return { items: [ { text:"Buy Fish", ...

Updating the image source using JQuery dynamically

Within my index.php file: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('.on').click(function ...

The makeSVG function from GoJS is failing to generate the correct HTML SVG object

I have been utilizing the Diagram.makeSVG() method to create an SVG from my diagram. Subsequently, I appended the SVG to a new empty tab using this script (diagram represents my Diagram Object): function print() { var newTab = window.open(), svg ...

Tips for eliminating duplicate words and potentially incorporating new terms into a text

Is there a way to eliminate specific words that are repeated exactly twice in a string, and if possible, add the word "and"? This should only apply to certain characters. For instance, take the following example: "Intruction in seated LE AROM x10 each ins ...

Retrieve the file path of the local system's home directory from the server

The server is up and running, I am looking to retrieve the file path of the local system in which the AWS server is operating using Node.js. ...