量角器:如何正确设置每个函数的期望getText()?

时间:2019-02-17 20:54:13

标签: javascript jasmine protractor

我正在测试一个按标题过滤产品的页面,并希望对测试的正确性设定一个期望:该页面上的所有标题均包含单词«Juvederm»,而不包含其他任何标题(«Emla», «酷感»等)。 并且仅应按第一个单词找到标题-Juvederm(而非完整标题:JUVEDEM®ULTRA SMILE Europe;JUVEDDERM®VOLBELLA WITH LIDOCAINE Europe)。

一种产品的HTML为:

<div class="col-xs-12 col-sm-6 col-md-4 block_history_" style="">
<section class="product">
    <div class="product__holder">
        <a href="https://www.cosmedicalsupplies.com/.html" class="product__category"></a>
        <h2 class="product__title">
            <a href="juvederm-hydrate.html">JUVEDERM® HYDRATE™ Europe</a>
        </h2>
    </div>
</section>

我的代码console.log正确(如果页面上显示“ Juvederm”,则显示“已通过”,否则显示“未通过”:

it("products titles on the page should be Juvederm", () => { 
let product = $$(".product > div > h2 > a");

    browser.get(«…/advanced_search.html?keyword=juvederm");  

    product.each(function(element, index) {
        element.getText().then(function(text) {
            if(text.indexOf("JUVEDERM®") !== -1) {
                    console.log(«Passed»);
            }else{
                    console.log("NOT PASSED");
            }
       }); 
    }); 
});

但是我自己决定要有一个期望。 如何/在哪里设置?期望是什么?当我在“如果”中使用

expect(order.product.getText()).toEqual(text);

我收到有关文本差异的错误:

JUVEDERM® HYDRATE™ Europe toEqual JUVEDERM®

在“如果”中使用时

browser.wait(() => {
    return EC.textToBePresentInElement(order.product);
    }, 3000, "ASDF");

测试通过(但我需要查看“JUVEDERM®”,而不是“ ASDF”)。

请指教一下。

2 个答案:

答案 0 :(得分:1)

您需要像下面那样更改Expect语句。

expect(order.product.getText()).toContain('JUVEDERM®');

答案 1 :(得分:1)

使用toContain()检查是否包含子字符串。

it("products titles on the page should be Juvederm", () => {
    let products = $$(".product .product__title > a");

    browser.get("advanced_search.html?keyword=juvederm");  

    products.each(product => {
        expect(product.getText()).toContain('JUVEDERM®');
    }); 
});