简体   繁体   English

Javascript Mocha Selenium Test不起作用

[英]Javascript Mocha Selenium Test not working

I am trying to run a selenium mocha test to check the title of the website google. 我正在尝试进行硒摩卡测试,以检查Google网站的标题。 I am doing this in the configuration of Web.js and WebTest.js. 我正在Web.js和WebTest.js的配置中执行此操作。 Here are my classes and I'm not sure if I'm going at this the correct way or not. 这是我的课程,我不确定是否要采用正确的方法。

Web.js 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 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. 我收到错误“ ReferenceError:标题未定义”,这使我认为我遇到了范围问题,但是我不确定如何正确执行此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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