如何使用Browserstack,selenium和mocha进行正确的测试?

时间:2016-06-03 14:27:38

标签: selenium selenium-webdriver mocha browserstack

我试图学习使用browserstack测试我的网络应用。 我在他们的网站上运行他们的例子

var assert = require('assert'),
  fs = require('fs');

var webdriver = require('selenium-webdriver')
  test = require('selenium-webdriver/testing');

test.describe('Google Search', function() {
  var driver, server;

  test.before(function() {
  var capabilities = {
    'browserName' : 'firefox', 
    'browserstack.user' : 'my username',
    'browserstack.key' : 'my key'
   }
  driver = new webdriver.Builder().
    usingServer('http://hub-cloud.browserstack.com/wd/hub').
    withCapabilities(capabilities).
    build();
  });

  test.it('should append query to title', function() {
    driver.get('http://www.google.com');
    driver.findElement(webdriver.By.name('q')).sendKeys('BrowserStack');
    driver.findElement(webdriver.By.name('btnG')).click();
    driver.wait(function() {
      return driver.getTitle().then(function(title) {
        return 'BrowserStack - Google Search' === title;
      });
    }, 1000);
  });

  test.after(function() { driver.quit(); });
});

它给了我这个输入

Google搜索     1)"在所有"之前钩

0传球(2s)   1失败

1)谷歌搜索"之前"钩:      错误:超过2000毫秒的超时。确保在此测试中调用done()回调。

我试图运行自己的测试

var assert = require('assert');
var fs = require('fs');

var webdriver = require('selenium-webdriver');
describe('Google Search', function(){
    var driver;
    var server;
    var capabilities = {
        'browserName' : 'firefox', 
        'browserstack.user' : 'myuser',
        'browserstack.key' : 'mykey'
    }
    before(function(done){
        driver = new webdriver.Builder()
            .usingServer('http://hub-cloud.browserstack.com/wd/hub')
            .withCapabilities(capabilities)
            .build();
        done();
    });
    after(function(){
        driver.quit();
    });
    describe('Here we go', function(){
        it('Should do something', function(done){
            driver.get('http://www.google.ro').then(done);
            // done();
        })
    });
})

它给了我这个输出

      Google Search
    Here we go
      1) Should do something


  0 passing (2s)
  1 failing

  1) Google Search Here we go Should do something:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

当我评论driver.get它可以正常工作......但我有点需要它..

那么我该如何进行功能测试呢? 我在谷歌上找不到任何东西..

1 个答案:

答案 0 :(得分:2)

看起来Selenium的默认超时时间太短了。你应该把它增加到30秒:

mocha mocha-spec.js --timeout 30000

您还可以在脚本中设置超时:

before(function(done){
    this.timeout(30000);

    driver = new webdriver.Builder()
        .usingServer('http://hub-cloud.browserstack.com/wd/hub')
        .withCapabilities(capabilities)
        .build();
    done();
});