2014-07-18 27 views
15

我可以在互聯網上找到的每個量角器示例似乎都使用browser.get和web URI。使用量角器打開文件

browser.get('http://localhost:8000'); 

我想使用Selenium簡單的導航到一個file://路徑,這樣我就不需要爲了進行測試運行本地Web服務器。我需要的只是一個簡單的HTML頁面和一些資產。

雖然這似乎並不奏效。

browser.get('file:///Users/myusername/dev/mpproject/spec/support/index.html'); 

當我將該URI粘貼到瀏覽器窗口中時,我得到一個HTML頁面。當我嘗試用量角器打開它時,我得到一個超時。

如何使用量角器在此頁面上運行測試?理想的答案將使用myproject根的相對文件路徑。

回答

-4

我覺得有錯字錯誤。在「get」方法中,您應該將URL包含在雙引號「」中。

WebDriver driver=new FirefoxDriver(); 
driver.get('file:///E:/Programming%20Samples/HTML%20Samples/First%20Program.html'); 
+0

[雙引號和單引號在JavaScript中是等效的](http://stackoverflow.com/q/242813/574190),所以不能有所作爲。我雖然嘗試,但它不起作用。 –

+0

據我所知,當您使用dirver.get方法時,您應該提供雙引號......當我們使用單引號時,它會抱怨編譯錯誤消息。 – Uday

0

什麼是錯誤日誌:使用雙引號像下面

試試?

這可能與「加載」角度有關。爲此,您可以嘗試 browser.driver.ignoreSynchronization = true;

錯誤日誌肯定有助於試圖理解問題。

13

我發佈瞭解決方案我已經found in here幫助我運行一個文件協議的量角器。

默認情況下,量角器使用data:text/html,<html></html>作爲resetUrl,但location.replacedata:到不允許的file:協議(我們會得到「不允許本地資源」的錯誤),所以我們有一個與file:取代resetUrl協議:

exports.config = { 
    // ... 

    baseUrl: 'file:///absolute/path/to/your/project/index.html', 

    onPrepare: function() { 

     // By default, Protractor use data:text/html,<html></html> as resetUrl, but 
     // location.replace from the data: to the file: protocol is not allowed 
     // (we'll get ‘not allowed local resource’ error), so we replace resetUrl with one 
     // with the file: protocol (this particular one will open system's root folder) 
     browser.resetUrl = 'file://'; 
    } 

    // ... 
}; 

如果你想運行一個相對路徑到項目文件夾,那麼你可以只使用Node.js的工具,因爲量角器在Node.js的環境中運行。例如,__dirname將返回保存量程器配置文件的目錄的絕對路徑。其結果是使用:

exports.config = { 
    // ... 

    baseUrl: 'file://' + __dirname + '/spec/support/index.html' 

    // ... 
}; 

另外,如果您的應用程序確實XHR請求一些端點,這是不是從file:允許,您可能需要使用自定義標記運行測試的瀏覽器。在我的情況下,它是鉻:

exports.config = { 
    // ... 

    capabilities: { 
     browserName: 'chrome', 
     chromeOptions: { 
      // --allow-file-access-from-files - allow XHR from file:// 
      args: ['allow-file-access-from-files'] 
     } 
    } 

    // ... 
} 
+1

祝福你這個美好的修復!一切都正是我所需要的 –

+0

如何在我的測試規範中打開本地html文件呢?我試着沒有使用'browser.get'和'browser.get()',假設它會打開指定的baseUrl。但是,既沒有工作,它只是不加載頁面,因此測試失敗。 – CGFoX

+0

實際上,它使用'browser.get('')',但僅在'onPrepare'內添加'browser.ignoreSynchronization = true;'後才能使用。 – CGFoX

4

我有相同的錯誤,並通過應用邁克爾Radionov的修復,但刪除baseUrl修復。這裏是我的設置:

protractor.config.js:

exports.config = { 

    capabilities: { 
    browserName: 'chrome' 
    }, 

    specs: [ 
    '*.js' 
    ], 

    onPrepare: function() { 
    // By default, Protractor use data:text/html,<html></html> as resetUrl, but 
    // location.replace from the data: to the file: protocol is not allowed 
    // (we'll get ‘not allowed local resource’ error), so we replace resetUrl with one 
    // with the file: protocol (this particular one will open system's root folder) 
    browser.ignoreSynchronization = true; 
    browser.waitForAngular(); 
    browser.sleep(500); 
    browser.resetUrl = 'file:///'; 
    } 

}; 

e2etest。js:

'use strict'; 

describe("Buttons' tests are started", function() { 

    it('Should retrieve 20 records into table', function() { 

     browser.get('file:///C:/Users/path_to_file/index.html'); 

     /* Test codes here */ 

    }); 

}); 
+0

這解決了我的問題。我仍然將它與baseUrl結合起來,在測試規範中有更好的URL。對我來說,它也沒有睡眠功能,這可能會加速測試。 – CGFoX