2014-11-17 63 views
6

我正在嘗試使用PhantomJS瀏覽器在Amazon EC2環境中運行量角器測試。我知道用量角器並沒有提醒用戶PhantomJS,但它是我現在唯一的選擇。測試總是在Win7環境中工作。爲什麼多個GET請求失敗量角器測試?

當測試看起來像這樣它總是工作正常

describe('Portal Login End 2 End test', function() { 
    it('should automatically redirect to login /form', function() { 
     browser.get(''); 
     expect(browser.getLocationAbsUrl()).toMatch("/login"); 
    }); 

    it('should automatically redirect to dashboard page after successful login', function() { 
     element(by.model('user.username')).sendKeys('admin'); 
     element(by.model('user.password')).sendKeys('admin'); 
     element(by.buttonText('Sign in')).click(); 
     expect(browser.getLocationAbsUrl()).toMatch("dashboard/home"); 
    }); 
}); 

但是當一個GET請求在beforeEach功能是犯了這樣的

describe('Portal Login End 2 End test', function() { 
    beforeEach(function() { 
     browser.get(''); 
    } 
    it('should automatically redirect to login', function() { 
     expect(browser.getLocationAbsUrl()).toMatch("/login"); 
    }); 

    it('should automatically redirect to dashboard page after successful login', function() { 
     element(by.model('user.username')).sendKeys('admin'); 
     element(by.model('user.password')).sendKeys('admin'); 
     element(by.buttonText('Sign in')).click(); 
     expect(browser.getLocationAbsUrl()).toMatch("dashboard/home"); 
    }); 
}); 

測試失敗的大部分時間,但並不總是,出現以下錯誤

UnknownError: Error communicating with the remote browser. It may have died. Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:33' System info: host: 'ip-10-249-98-182', ip: '10.249.98.182', os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-504.el6.x86_64', java.version: '1.6.0_45' Driver info: driver.version: EventFiringWebDriver

因此,當噸wo GET請求在最常失敗的相同測試中進行,但只有一個請求發出時,它始終工作。正如我上面寫的,兩個GET請求在Win7環境中工作正常,但在Linux中不起作用。

我已經看到一些這個錯誤的帖子,但沒有如何解決它。任何人在那裏解決方案?

+0

同樣的錯誤在這裏..並有困難修復它.. –

回答

0

我不確定beforeEach是否在瀏覽器中等待工作完成,但it函數是否等待。嘗試在it函數中包裝get呼叫。

describe('Portal Login End 2 End test', function() { 
    beforeEach(function() { 
      it('redirects',function(){browser.get('');}); 
    }); 
    // ..... 
}); 
+1

感謝您的回覆。嘗試過,但它看起來像beforeEach不接受它,因爲它異步,它()不是。在每次測試中,如果不使用beforeEach,而是首先使用GET請求,我也會得到隨機行爲。 – Ola

0

如果你的問題,如你所說,是因爲瀏覽器是不是在等待中beforeEach的獲取,添加browser.waitForAngular();

describe('Portal Login End 2 End test', function() { 
beforeEach(function() { 
    browser.get(''); 
} 
it('should automatically redirect to login', function() { 
    browser.waitForAngular(); 
    expect(browser.getLocationAbsUrl()).toMatch("/login"); 
}); 

it('should automatically redirect to dashboard page after successful login', function() { 
    element(by.model('user.username')).sendKeys('admin'); 
    element(by.model('user.password')).sendKeys('admin'); 
    element(by.buttonText('Sign in')).click(); 
    expect(browser.getLocationAbsUrl()).toMatch("dashboard/home"); 
}); 
}); 

因爲它的測試順序運行,你不應該在每個塊中調用它。

您還可以嘗試在beforeEach函數中調用browser.waitForAngular(),但我更願意在it函數中調用該函數。