使用量角器的开关定位非角度iframe的元素

时间:2014-10-22 15:27:02

标签: angularjs webdriver protractor

我有一个包含iframe的angularjs应用程序,该iframe显示允许登录其他网站的页面。我试图在iframe中包含的字段中添加一些值,但我找不到使用任何定位器的元素。

这是我的测试:

describe("Harvest", function() {

  beforeEach(function () {

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

expect(browser.getCurrentUrl()).toMatch('_*#/login$');

element(by.model('user.username')).sendKeys('sam');
element(by.model('user.password')).sendKeys('pass');

element(by.id('bt_signin')).click();

  });

  afterEach(function () {
    browser.executeScript('window.sessionStorage.clear();');
  });

  describe('A user', function () {

beforeEach(function () {
  browser.get('http://localhost:8110/');
});

it('should be able to obtain an access token from harvest', function () {
  expect(browser.getCurrentUrl()).toMatch('_*#/home$');

  //Display and close the window
  element(by.id('btHarvest')).click();

  expect(element(by.id('modalHarvest')).isPresent()).toBe(true);

  element(by.id('btCloseModal')).click();

  expect(element(by.id('modalHarvest')).isPresent()).toBe(false);

  //Authenticate into harvest
  element(by.id('btHarvest')).click();

  expect(element(by.id('modalHarvest')).isPresent()).toBe(true);

  browser.switchTo().frame('iframeHarvest');

  //It fails here with a null exceptions, guess it can't find it
  element(by.id('email')).sendKeys(browser.params.harvestLogins.user);
  element(by.id('user_password')).sendKeys(browser.params.harvestLogins.password);
  element(by.id('sign-in-button')).click();

  expect(element(by.name('commit')).isPresent()).toBe(true);

  browser.driver.switchTo().defaultContent();



});

});

});

这是生成的异常

1) Harvest A user should be able to obtain an access token from harvest
   Message:
     [31mError: Error while waiting for Protractor to sync with the page: {}[0m
   Stacktrace:
     Error: Error while waiting for Protractor to sync with the page: {}
    at Error (<anonymous>)
==== async task ====
WebDriver.executeScript()
    at null.<anonymous> (/Users/samdurand/workspace/cake/subbie/src/test/web/js/features/harvest.js:45:22)
==== async task ====
Asynchronous test function: it()
Error
    at null.<anonymous> (/Users/samdurand/workspace/cake/subbie/src/test/web/js/features/harvest.js:26:5)
    at null.<anonymous> (/Users/samdurand/workspace/cake/subbie/src/test/web/js/features/harvest.js:20:3)
    at Object.<anonymous> (/Users/samdurand/workspace/cake/subbie/src/test/web/js/features/harvest.js:1:63)

根据建议更正iframe调用后,我收到此错误:

1) Projects A user is possible to create a project based on harvest data
   Message:
     [31mError: Error while waiting for Protractor to sync with the page: {}[0m
   Stacktrace:
     Error: Error while waiting for Protractor to sync with the page: {}
    at Error (<anonymous>)

3 个答案:

答案 0 :(得分:3)

正如所讨论的,您面临的问题是因为您切换到的iframe是非角度的。因此,切换到element(by.id)后,iframe将无效。相反,需要使用findElement

答案 1 :(得分:2)

既然您的是Angular应用程序,请尝试使用:

browser.switchTo().frame('iframeHarvest');

而不是:

browser.driver.switchTo().frame(element(by.id('iframeHarvest')));

答案 2 :(得分:0)

我找到了一个解决方案,但我还是等待角度团队的解释,因为我不明白为什么使用element失败但是直接使用驱动程序成功。这是有效的代码(我包括来自@Sakshi的建议,即使它认为它没有改变结果):

browser.switchTo().frame(element(by.id('iframeHarvest')));
browser.driver.findElement(by.id('email')).sendKeys(browser.params.harvestLogins.user);

编辑:这是量角器主题https://github.com/angular/protractor/issues/1465