node.js - 如何从 Protractor 中包含的文件进行导航?

标签 node.js protractor e2e-testing

我已经完成了这个SO问题中提到的操作,将一个文件包含到我的 Protractor 测试中:How to reuse code in Protractor / AngularJS Testing 。问题是从包含的代码内部调用的方法对我看到的浏览器窗口没有影响。

我在一个单独的文件中创建了一个登录方法并将其包含在内,但是当我从 Protractor 测试中调用该方法时,打开的浏览器只会在那里停留几秒钟,然后继续,就像我没有调用任何东西一样。我可以通过 console.log 验证该方法是否被调用。下面是我的代码文件。

login.js,包含的登录功能:

exports.login = function ()
{
    console.log("Login method called");
    it('logs in', function () {
        browser.get('http://my-url.com/login/');

        // log in to continue with checkout
        element(by.xpath('//*[@id="login-form"]/div[1]/input')).sendKeys("username");
        element(by.xpath('//*[@id="login-form"]/div[2]/input')).sendKeys("password");
        element(by.xpath('//*[@id="login-form"]/div[3]/button')).click();

        // login functions on timeout
        browser.driver.sleep(1000);
    });
}

conf.js, Protractor 配置文件:

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    specs: [/*'specs/overview.js',*/ 'specs/shopping-cart-state.js', 'specs/checkout-state.js', 'specs/buy-item.js'],
    onPrepare: function () {
        var SpecReporter = require('jasmine-spec-reporter');
        // add jasmine spec reporter
        jasmine.getEnv().addReporter(new SpecReporter({ displayStacktrace: 'all' }));
        protractor.login = require('./login.js');
    },
    jasmineNodeOpts: {
        print: function () { },
        defaultTimeoutInterval: 5000000
    }
};

spec.js,测试规范:

describe('Log in with an included function', function () {

    it('logs in', function () {    

        protractor.login.login();

        browser.driver.sleep(5000);
    });
});

即使我将浏览器对象作为参数传递给登录函数,仍然没有任何反应,也没有收到任何错误消息。谁能告诉我我做错了什么?

最佳答案

不要将逻辑包装在“登录”模块的 it() 内:

exports.login = function ()
{
    this.login = function () {
        console.log("Login method called");

        browser.get('http://my-url.com/login/');

        // log in to continue with checkout
        element(by.xpath('//*[@id="login-form"]/div[1]/input')).sendKeys("username");
        element(by.xpath('//*[@id="login-form"]/div[2]/input')).sendKeys("password");
        element(by.xpath('//*[@id="login-form"]/div[3]/button')).click();

        // login functions on timeout
        browser.driver.sleep(1000);
    };
}

关于node.js - 如何从 Protractor 中包含的文件进行导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34184623/

相关文章:

mysql - 我可以使用node.js将mysql查询结果调用到html文件中吗?

node.js - 如何使用 Docker Compose 将容器 A 中的 Node.js TCP 客户端连接到容器 B 中的 TCP 服务器?

javascript - Protractor 5.1.1 不适用于 Chrome 58

javascript - 如何在 e2e 测试中循环遍历行列

javascript - 无法在 "Type to search dropdown"中输入文本,这是使用 testcafe 的 react 组件

node.js - 在 Lambda 函数中使用 axios 时根据先前的请求设置 cookie

node.js - NginX 上的 SSL 证书

windows - Protractor/ Selenium "could not find chromedriver at"(在 Windows 上)

javascript - 匹配字符串不区分大小写

testing - 从使用testcafe开源升级到studio有用吗?