Troubleshooting a broken Selenium Test in Mocha with Javascript

Attempting to execute a selenium mocha test to verify the title of Google's website. The setup involves Web.js and WebTest.js classes. Uncertain if proceeding in the correct manner.

Web.js

const {Builder, By, Key, until, WebElement} = require('selenium-webdriver');
var driver = new Builder().forBrowser('internet explorer').build(); 
var url = 'https://www.google.com/';

function Web() {

    var promise = new Promise(function(resolve,reject){
        return driver.get(url);
    }).then(function(title) {
        var title;
        title = driver.getTitle().toString();
        return title;
    }).catch(function(err){
        console.log(err);
    });

    return title;
}



Web.prototype.getTitle = function (title) {

    var title =  Web();
    while (title == null){
        title = Web();
    }
    return (title);
}

module.exports.Web = Web;

WebTest.js

assert = require("assert");
Web = require("../Web.js").Web

describe("A web function", function () {
    describe("getting google's title", function () {
        it("should return Google", function () {
            var result = new Web().getTitle();
            assert.equal("Google", result, "But the string " + result + " was returned instead");
        });
    });
});

Encountering the error "ReferenceError: title is not defined" indicating a potential scope issue, seeking guidance on resolving this matter effectively.

Your assistance would be greatly appreciated.

Answer №1

Try out this solution:

const webdriver = require("selenium-webdriver");

const DriverFactory = {
    create: function (browser) {
            return driver = new webdriver
                .Builder().forBrowser(browser)
                .build();
    }
}
module.exports = DriverFactory;

Now, implement this module in your test script

const DriverFactory = require('./driverFactory.js');

const assert = require("chai").assert;

describe("Check the title", function () {
    this.timeout(40000);
    let driver;

    before(async function () {
        driver = await DriverFactory.create("firefox");
    });

    after(async function () {
        await driver.quit();
    });

    it("1.Open Google website", async function () {
        await driver.get("https://www.google.com");
    });

    it("2.Verify the title is 'Google'", async function () {
        const title = await driver.getTitle();
        assert.equal(title, "Google");
    });

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

AngularJS facing difficulty in resolving the returned promise

I am facing an issue with two cascading drop-downs where the JSON data being returned to the second drop-down is not rendering properly due to a promise resolution problem <div class="row" style="padding-bottom:50px;width:85%;"> <select data ...

What is the process for the event loop moving into the poll phase?

There is a scenario outlined in the event loop explanation on the Node.js official website. When setTimeout is triggered, and the callback queue for the timer phase isn't empty, why does the event loop move on to the poll phase? The site mentions that ...

Learn how to design a dynamic table that adjusts its schema based on specific breakpoints, with or without the use of Bootstrap

Is there a way to design a table that automatically changes its structure when viewed on a mobile screen? For larger screens like laptops, the table should display as follows: Email Degination Team Last Active Location [email protected] Manag ...

What is the best way to position the left sidebar on top of the other components and shift them to the

Currently, I am working on a project to display NBA data fetched from an API. I am aiming to recreate the design showcased here: Dribbble Design My main challenge lies in overlaying the left sidebar onto the main box and shifting the components sligh ...

The element cannot be clicked at this point as another element would intercept the click:

Having an issue with clicking a button at the top of the page. The CSS selector works fine in my local Eclipse environment, but when trying to run it on Jenkins server on my local machine, it fails with an error stating that the element is not clickable. T ...

What could be causing ng-submit to not successfully transmit data?

I'm currently going through this Yeoman tutorial, but I'm encountering some issues. The new todo is not being added to the $scope.todos as expected, and I'm struggling to identify the reason behind it. You can access the code here: Upon c ...

The method for renaming a namespace when exporting it in Typescript

Suppose I am using a third-party namespace Foo in my Typescript code. I intend to create some utility functions for this module within a module called Utility.Foo. The issue here is that the original Foo would be hidden from functions defined inside Util ...

Autocomplete Dropdown failing to select default value when defaultValue option is set

stateNPAValue[formData.state.vale] = 0: "All",1: "959", 2: "203",3: "860", 4: "475" // API response for NPA data const [selectedNamesState, setSelectedNamesState] = useState([]); const transformedNpaData ...

Transferring a Multidimensional Javascript Array to a PHP Page

I am working with 2 different pages. berechnungseingabe.php On this page, I am generating a Multidimensional Javascript Array. Array[19] 3: Array[0] 7: Array[0] 11: Array[0] Anzahl: "1" Driving: 2380 Index: "13" Latitude: 48.0078 ...

Vue.js HTML not refreshing after axios request changes object property

Issue with Vue.js not updating HTML after axios GET request. Here's the structure: <span @click="tryResponse"> Data_object: {{ data_object }} <br> Data_object.serial: {{ data_object.serial }} <br> </span> Data ...

creating a scrolling function using Python with Selenium

I'm currently working on a script for LinkedIn using Selenium in Python, and I've run into an issue. The problem is that when I try to scroll the window of a dialog box, my script ends up scrolling the background window instead. driver=webdriver ...

The state of connection is constant within the WebRTC Data Channel

I've been working on sending JSON/Text data through a peer connection using WEBRTC, but I'm facing an issue where my datachannel is stuck in the connecting state. This seems to be causing my text messages not to be sent to the connected peer. He ...

The combination of XPath, utilizing matches and regular expressions, appears to be functioning correctly in the XPath tester tool. However, when implemented in the code, it is resulting in an InvalidSelectorException and failing to execute the

HTML <div class="top-section" style="" xpath="1"> <input id="role" value="admin" hidden=""> <small>Welcome </small> <b style="">8828024404, MCGM</b> <a href=""><img alt="Attenda ...

Error encountered: jquery form validation fails to register changes

I am currently developing a calculator-like code where users will submit a form multiple times, and I need to save the result of calculations only if there are changes in the form. For instance, when a user clicks the "Calculate" button for the first time, ...

Tips for adjusting the range slider's increment by selecting a radio button

There are two radio buttons and multiple range sliders available. Each radio button has a distinct value assigned to it. The desired functionality is that when one of the radio buttons is clicked, the maximum value should change (from 12 to 24 or from 24 ...

Error: The function $(...).ekkoLightbox is not defined

Recently, I attempted to integrate ekkolightbox into my WordPress website. However, each time I selected an image, I encountered the following error message: Uncaught TypeError: $(...).ekkoLightbox is not a function Initially, I suspected that the issue ...

What is the method to store and retrieve data attributes linked to elements such as select options within the React framework?

Despite my efforts, I'm currently unable to retrieve the data attribute from an option using React as it keeps returning null. <select onChange={(e) => this.onIndustryChangeOption(e)} value={this.props.selectedIndustry}> <opti ...

Using jQuery to traverse through an array of functions

I am currently working on implementing navigation functionality within an array of functions using jQuery. Specifically, I have "forward" and "back" buttons that are intended to navigate through the array. However, I am encountering an issue with the back ...

Executing several Javascript functions when the page is loaded

I have attempted to invoke 4 JavaScript functions from the HTML body when the page is loaded. Each function calls a JSP servlet to retrieve data from a database and populate the respective list boxes. Currently, I am focusing on the edit screen where I am ...

Encounters an error when attempting to use the TimerTask method in conjunction with WebDriver

With my code, I aimed to make it so that if the test exceeds 10 seconds, Selenium displays the window title and then closes it. Here is the code snippet: import java.io.File; import java.util.Timer; import org.junit.Assert; import org.junit.Test; import ...