简体   繁体   中英

Javascript Mocha Selenium Test not working

I am trying to run a selenium mocha test to check the title of the website google. I am doing this in the configuration of Web.js and WebTest.js. Here are my classes and I'm not sure if I'm going at this the correct way or not.

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");
        });
    });
});

I am getting the error "ReferenceError: title is not defined" which leads me to believe I have a scope problem, but I'm not sure how to do this correctly.

Thank you for any help.

This should work:

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

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

And then use this module in your test

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

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

describe("Get title", function () {
    this.timeout(40000);
    var 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.The title is 'Google'", async function () {
        var title = await driver.getTitle();
        assert.equal(title, "Google");
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM