angularjs - 无法使用 Protractor 在 Angular JS 应用程序中查找元素

标签 angularjs selenium protractor

我是 Angular JS 应用程序测试的新手。我尝试过在线提供的示例 Angular JS 应用程序。

链接:Angular JS application

经过网上探索,Protractor是测试AngularJS的最好方法。我尝试使用以下代码来查找上面链接中的元素。我尝试了所有可能的定位器,但无法捕获任何网络元素。

Protractor 版本:2.5.1

代码:

describe('angularjs homepage', function() {

  it( 'should load the home page', function() {
      browser.ignoreSynchronization = true
      browser.get('http://themeforest.net/item/square-responsive-admin-app-with-angularjs/full_screen_preview/7511722');
      browser.driver.manage().window().maximize()
      browser.sleep(6000);
      element(by.xpath('.//*[@id="nav"]/li[2]/a')).click();
  });
});

配置文件::

exports.config = {
    //The address of a running selenium server.
  seleniumAddress: 'http://localhost:4444/wd/hub',
 //Here we specify the name of the specs files.
  specs: ['testspec.js']
}

错误:

 1) angularjs homepage should load the home page
   Message:
     NoSuchElementError: No element found using locator: By.xpath(".//*[@id=\"na
v\"]/li[2]/a")
   Stacktrace:
     NoSuchElementError: No element found using locator: By.xpath(".//*[@id=\"na
v\"]/li[2]/a")
    at new bot.Error (C:\Users\CP042756\AppData\Roaming\npm\node_modules\protrac
tor\node_modules\selenium-webdriver\lib\atoms\error.js:108:18)
    at C:\Users\CP042756\AppData\Roaming\npm\node_modules\protractor\lib\element
.js:694:15
    at Array.forEach (native)
    at goog.async.run.processWorkQueue (C:\Users\CP042756\AppData\Roaming\npm\no
de_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:130:
15)
    at process._tickCallback (node.js:356:9)
Error

请帮忙,谢谢

最佳答案

所需的元素位于 iframe 内 - switch to it's context在查找元素之前:

browser.switchTo().frame("preview-frame"); 
element(by.xpath('.//*[@id="nav"]/li[2]/a')).click();

preview-frameiframe 的名称。


您可能已经知道,您的 AngularJS 应用程序已加载到此 iframe 中 - 这可能就是您设置 ignoreSynchronization 标志的原因。

我认为您可以在不关闭同步的情况下测试您的应用程序,并且根本不需要调整 ignoreSynchronization 标志 - data-ng-app 属性已设置为 on iframe 内的 body。如果您直接访问加载 iframe 的 URL,protractor 会自动检测您的 AngularJS 应用程序并同步使用它:

it('should load the home page', function() {
  browser.get('http://iarouse.com/dist-square/v2.0.1/index.html');
  browser.driver.manage().window().maximize();

  element(by.xpath('.//*[@id="nav"]/li[2]/a')).click();
});

关于angularjs - 无法使用 Protractor 在 Angular JS 应用程序中查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33498845/

相关文章:

javascript - 单击另一个函数即可触发 Angular 函数

javascript - Protractor - ngFor 循环

javascript - 用 Protractor 打开文件

javascript - 你可以使用 Angular 依赖注入(inject)而不是 RequireJS 吗?

javascript - Angular js中可以像静态数组一样吗?

javascript - Angular : Add attribute (programmatically) to element with binding

javascript - 如何使用 PHP behat/mink selenium2 chrome webdriver 处理 javascript 警报窗口

java - Selenium div 下拉列表 - 元素不可见

python - 尝试通过 Chromedriver 2.34、Selenium 3.8 和 Python 3.6.2 登录 Wells Fargo 时出错

Typescript 找不到模块 Protractor