量角器cssContainingText,函数作为参数

时间:2017-06-06 09:22:51

标签: javascript protractor

我正在尝试测试我的输入字段是否正确计算了信用。

此测试应选择1-100(学分)之间的随机数,然后乘以5($)并显示总价。

function randomZip() {
  return Math.floor(Math.random() * 100);
}
var creditQuantity = randomZip();

function creditQuantityMultiplied() {
  creditQuantity * 5;
  return;
};

element(by.css("a[href='/credits']")).click();
element(by.css('input[name="count"]')).sendKeys(creditQuantity);
$('.text_per').click();
browser.sleep(1500);

var checkSumCredits = element(by.cssContainingText('.inline-block.price_credit.text_per', creditQuantityMultiplied()));

expect(checkSumCredits.isPresent()).toBeTruthy();
<div class="credits-block" id="creditsBuyForm">
  <h2>Buy Credits</h2>
  <div class="credits">
    <div class="block">
      <span class="text_per">Amount</span>
      <input type="text" name="count" value="25" class="text_per">
      <span class="text_per">x</span>
      <div class="inline-block price_credit">$5.00</div>
      <span class="text_per">per credit</span> </div>
    <div class="block">
      <span class="text_per">Total</span>
      <div class="inline-block price_credit text_per">
        <img width="32" height="32" src="/app-static/css/img/coins_2.png" class="inline-block"> $125.00</div>
      <div class="inline-block">
        <form action="/cart/index.php" method="post" id="remoteadd" class="trackable-init">
          <input type="hidden" name="app" value="ecom"> <input type="hidden" name="ns" value="addcart">
          <input type="hidden" name="cart_api" value="Y"> <input type="hidden" name="price" value="5.00">
          <input type="hidden" name="name" value="Credits ($5.00 per credit)">
          <input type="hidden" name="quantity" value="25">
          <input type="hidden" name="number" value="a:3:{s:7:&quot;user_id&quot;;i:7996;s:10:&quot;company_id&quot;;i:1;s:9:&quot;item_type&quot;;i:1;}">
          <input type="hidden" name="delivery_method" value="N">
          <input type="hidden" name="SUBMIT" value="SUBMIT">
          <input type="hidden" name="taxcountry" value="STANDARD">
          <input type="hidden" name="taxstateprov" value="STANDARD"> <input type="hidden" name="taxclass" value="DIGITAL">
          <button type="submit" class="def-ui-button-buy ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" role="button">
            <span class="ui-button-icon-primary ui-icon ui-icon-cart"></span>
            <span class="ui-button-text">Buy</span>
            </button>
        </form>
      </div>
    </div>
  </div>
</div>

当我运行此代码并且我不确定问题是cssContainingText是否需要2个字符串args并且我传递1个字符串1整数或其他内容时,我得到了预期的错误。

1 个答案:

答案 0 :(得分:0)

当我查看您的代码时,我发现以下情况

  1. 您使用随机生成的element(by.css('input[name="count"]'))填充creditQuantity,例如它是值47
  2. 然后您正在使用expect(checkSumCredits.isPresent()).toBeTruthy();进行断言。 checkSumCredits基于以下事实:如果它可以找到包含creditQuantityMultiplied()生成的值的元素
  3. 我认为creditQuantityMultiplied()有2个错误:

    1. 它不会返回值,只会返回return
    2. 如果您更改creditQuantityMultiplied()也返回一个值,它也会生成一个唯一值。这永远不会等于之前的随机值47
    3. 这就是为什么你找不到匹配并返回false /失败。

      希望有所帮助