一些Webdriver IO摩卡柴问题

时间:2016-08-24 01:48:16

标签: node.js selenium mocha chai webdriver-io

我是新手,使用带有Mocha和Chai的webdriver-io。首先,这是我的剧本:

var homePage = 'http://www.mypage.com';
var expect = require("chai").expect;
var headerText = 'h1.browse-header-title';
var currentHeaderText;
var links = ['Furniture','Fine Art','Jewelry & Watches','Fashion'];

describe('Test Suite 1', function(){

    before(function(){
       console.log('Running navigation h1 tag suite');
    });

    afterEach(function(){
        browser.close();
        // What method do I use?
    });

    it('Should click Furniture and page header should match', function(done){
       browser.url(homePage).click('a[data-tn="global-nav-item-link-furniture"]');
        currentHeaderText = browser.getText(headerText);
        expect(currentHeaderText).to.equal(links[0]);
        console.log('h1 tag is '+currentHeaderText+'');
    });
    it('Should click Fine Art and page header should match', function(done){
        browser.url(homePage).click('a[data-tn="global-nav-item-link-fine-art"]');
        currentHeaderText = browser.getText(headerText);
        expect(currentHeaderText).to.equal(links[1]);
        console.log('h1 tag is '+currentHeaderText+'');

    });
    it('Should click Jewelry & Watches and page header should match', function(done){
        browser.url(homePage).click('a[data-tn="global-nav-item-link-jewelry-&-watches"]');
        currentHeaderText = browser.getText(headerText);
        expect(currentHeaderText).to.equal(links[2]);
        console.log('h1 tag is '+currentHeaderText+'');
    });
    it('Should click Fashion and page header should match', function(done){
        browser.url(homePage).click('a[data-tn="global-nav-item-link-fashion"]');
        currentHeaderText = browser.getText(headerText);
        expect(currentHeaderText).to.equal(links[3]);
        console.log('h1 tag is '+currentHeaderText+'');
    });

});

我的第一个问题是,是否有更好的地方存储变量和适当的方法来调用它们?

当运行afterEach browser.close()函数时,重置浏览器会话的最佳方法是什么,我尝试了browser.reset()但是在调用第二个测试时它似乎无法正常工作。 mocha和chai是否有更好的方法来关闭浏览器,重置会话并打开浏览器并转到主页?

这些是我给出的要求:

1)测试必须用mocha编写,并使用chai进行断言。该 用于驱动测试的框架必须是webdriverIO - 没有本机selenium命令。

2)测试应该以利用页面对象模式的方式编写

3)可能在其他测试中使用的变量(例如用户) 电子邮件/密码)应与测试文件分开存储。

1 个答案:

答案 0 :(得分:1)

为了将变量存储在代码之外,有几种方法。

  1. 您可以利用wdio.conf.js并开始添加this之类的customConfig对象 并在代码_page.navigate(browser.options.customConfig.baseUrl);中使用它们,例如this
  2. 或者将其保存在单独的.json文件中,使用nconf引入您的测试代码
  3. 或者只是导入data.json,例如从'./testdata.json'导入testdata并直接使用它们
  4. 就browser.close(),要求而言,如果它是一个工作流,一系列用户操作,我会将它们保存在一个文件中,以便在一个浏览器会话中运行它们。 如果这些是不相关的测试,单独的规范,让wdio / mocha为您处理并行执行和浏览器会话。