测试以检查ul元素并保存在对象中

时间:2015-04-30 20:10:52

标签: angularjs protractor karma-jasmine karma-mocha

我正在尝试编写我的第一个量角器测试。

我正在寻找目录列表及其下面的数字。

我想查看对象中部门名称的数字。

我有这个测试,但我不知道该怎么办

所以例如我的数组将有

{任命中心:'任命中心
(603)598-3323',.....}等

it('go to site', function() {
    browser.get('http://site/Department-Directory');

    browser.wait();

    var directory = element(by.css(".directory"));

  });

<ul class="directory">
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Patients-Visitors/Pre-Registration">Appointment Central </a>
      </h3><p>Appointment Central<br>(603) 598-3323</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a style="text-decoration: none;">Billing Inquiries</a>
      </h3><p>(603) 595-3975</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Breast-Care">Breast Care Center</a>
      </h3><p>(603) 595-5700</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Business-Health">Business &amp; Health</a>
      </h3><p>(603) 595-7371</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Cancer-Center">Cancer Center</a>
      </h3><p>(603) 880-3408</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Cardiovascular-Diabetes">Cardiovascular and Diabetes Center</a>
      </h3><p>(603) 595-3971</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Cardiovascular-Diabetes/Cardiac-Rehabilitation">Cardiac Rehabilitation</a>
      </h3><p>(603) 595-3971</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Neuropsychology-Learning-Behavioral-Medicine">Center for Neuropsychology, Learning, and Behavioral Medicine</a>
      </h3><p>(603) 598-3384</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Childbirth">Childbirth Center</a>
      </h3><p>(603) 595-3162</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Community-Health-Programs">Community Health Programs</a>
      </h3><p>(603) 595-3168</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Diagnostic-Imaging-Services ">Diagnostic Imaging Services</a>
      </h3><p>Nashua: (603) 598-3323<br>Milford: (603) 673-9958</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Endoscopy">Endoscopy</a>
      </h3><p>(603) 595-5300, ext. 66140</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.fullcirclemidwiferynh.com/">Full Circle Midwifery Care</a>
      </h3><p>(603) 595-3951</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Hospitalists">Hospitalists</a>
      </h3><p>(603) 882-3000, ext. 63882</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Intensive-Care-Unit">Intensive Care Unit (ICU)</a>
      </h3><p>(603) 882-3000, ext. 66200</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a style="text-decoration: none;">Medical Records</a>
      </h3><p>(603) 882-3000, ext. 63898</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://stjosephhospital.com/Medical-Staff">Medical Staff &amp; Allied Health Professionals</a>
      </h3><p>
<br>
(603) 595-3032
<br>
</p>
<br>
</div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.merrimackvalleypedi.com/">Merrimack Valley Pediatrics </a>
      </h3><p>(603) 880-1440</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://nashuamedicalgroup.org/">Nashua Medical Group</a>
      </h3><p>(603) 891-4400</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.nashuapedi.com/">Nashua Pediatrics - Milford</a>
      </h3><p>(603) 673-3870</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.nashuapedi.com/">Nashua Pediatrics - Nashua</a>
      </h3><p>(603) 889-6671</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.obgynsnh.com/">OB/GYN Associates of Southern NH - Milford</a>
      </h3><p>(603) 673-7786</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.obgynsnh.com/">OB/GYN Associates of Southern NH - Nashua</a>
      </h3><p>(603) 883-3365</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.obgynsnh.com/">OB/GYN Associates of Southern NH - Windham</a>
      </h3><p>(603) 883-3365</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Palliative-Medicine">Palliative Medicine Services</a>
      </h3><p>(603) 882-3000</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a style="text-decoration: none;">Patient Advocate</a>
      </h3><p>(603) 882-3000 ext. 63318</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Pharmacy">Pharmacy (Retail)</a>
      </h3><p>(603) 595-3090</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.pulmonaryassociatesnh.com/">Pulmonary Associates</a>
      </h3><p>(603) 889-4131</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Rehabilitation-Services">Rehabilitation Services (Outpatient)</a>
      </h3><p>(603) 595-3076</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Respiratory-Therapy">Respiratory Therapy</a>
      </h3><p>(603) 882-3000, ext. 67400</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Roger-Dionne-Senior">Roger Dionne, MD, Senior Center</a>
      </h3><p>(603) 595-3722</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/SAGE-Senior-Citizens">SAGE for Senior Citizens</a>
      </h3><p>(603) 578-5065</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjfmc.com/default.aspx">SJ Family Medical Center - Hudson</a>
      </h3><p>(603) 882-6700</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjfmc.com/default.aspx">SJ Family Medical Center - Merrimack</a>
      </h3><p>(603) 424-4181</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjfmc.com/default.aspx">SJ Family Medical Center - Milford</a>
      </h3><p>(603) 673-3014</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjfmc.com/default.aspx">SJ Family Medical Center - Nashua</a>
      </h3><p>(603) 883-7970</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjfmc.com/default.aspx">SJ Family Medical Center - Sky Meadow</a>
      </h3><p>(603) 891-4804</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjinternalmedicine.com/">SJ Internal Medicine</a>
      </h3><p>(603) 595-3614</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjneurologyassociates.com/">SJ Neurology Associates</a>
      </h3><p>(603) 594-9468</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjsurgicalassociates.com/">SJ Surgical Associates</a>
      </h3><p>(603) 598-0455</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Sleep-Disorders">Sleep Disorders Center</a>
      </h3><p>(603) 598-3374</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Patients-Visitors/Spiritual-Care">Spiritual Care Services</a>
      </h3><p>(603) 882-3000, ext. 67800</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.stjosephhhc.org/">St. Joseph Home &amp; Hospice Care</a>
      </h3><p>(603) 673-3460</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="http://www.sjhacademiccenter.org/">St. Joseph School of Nursing</a>
      </h3><p>(603) 594-2567<br>(800) 370-3169</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a style="text-decoration: none;">Surgical Services - Inpatient </a>
      </h3><p>(603) 882-3000, ext. 66230</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a style="text-decoration: none;">Surgical Services - Outpatient</a>
      </h3><p>(603) 882-3000, ext. 69953</p></div>
  </li>
  <li class="tocitem">
    <div class="desc">
      <h3>
        <a href="/Giving/Volunteering">Volunteering</a>
      </h3><p>(603) 595-3044</p></div>
  </li>
</ul>

1 个答案:

答案 0 :(得分:1)

element.all()map()应该有所帮助:

var links = element.all(by.css("ul.directory li.tocitem div.desc h3 a"));
links.map(function(link) {
    var key = link.getText();
    var value = link.element(by.xpath("following-sibling::p")).getText();

    return {
        key: value 
    };
}).then(function (result) {
    console.log(result);
});

这将为您提供一系列对象。如果您想要单个对象,请使用reduce()

var links = element.all(by.css("ul.directory li.tocitem div.desc h3 a"));
links.reduce(function(acc, link) {
    var key = link.getText();
    var value = link.element(by.xpath("following-sibling::p")).getText();

    acc[key] = value;
    return acc;
}, {}).then(function (result) {
    console.log(result);
});