2014-10-01 67 views
3

早上好,Selenium總是出現'錯誤:2000ms超時'

我目前在學習如何用JavaScript(使用摩卡)驅動硒。我創建了一個非常基本的測試,在運行時給我帶來了很多麻煩。無論何時運行測試,都會創建一個新的chrome實例並顯示瀏覽器。當瀏覽器最初出現時,它會在URL框中放置「data:」,然後進入google.com。然後我得到以下錯誤回:

$摩卡測試

Array 
    #indexOf() 
     ✓ should return -1 when the value is not present! 

    Google Search 
    1) should work 


    1 passing (2s) 
    1 failing 

    1) Google Search should work: 
    Error: timeout of 2000ms exceeded 
     at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:157:19) 
     at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) 

下面是測試本身:

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

test.describe('Google Search', function() { 
    test.it('should work', function() { 
    var chromeOptions = new chrome.Options(); 
    chromeOptions.addArguments(['test-type']); 

    var driver = new webdriver.Builder().withCapabilities(chromeOptions.toCapabilities()).build(); 

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

回答

0

,如果它失敗的地方特別是一貫也許你應該考慮調用driver.Manage().Timeouts()

ImplicitlyWait()實際上並沒有像Thread.sleep()那樣等待,它只是爲驅動程序設置隱式等待的最長等待時間。只需在代碼開始時調用它(傳入20秒參數)就足夠了。詳細閱讀ImplicitlyWhitWebDriverWait類。

據我記得(當我不得不使用時),這是因爲你沒有得到預期/默認時間的響應。

3

您收到的錯誤消息看起來像是摩卡超時。設置超時在摩卡正常的方法是:

it("foo", function() { 
    this.timeout(value); 
    ... 
}); 

其中value是任何你想要的值(毫秒)。值爲0會關閉Mocha的超時。默認值是2000ms。

相關問題