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

问题描述 投票:0回答:2

我正在测试一个按标题过滤产品的页面,并希望设定对测试正确性的期望:页面上的所有标题都包含WORD«Juvederm»,而不是任何其他标题(«Emla»,«Coolsense»等等。))。并且只能通过第一个单词找到标题 - Juvederm(不是完整标题:JUVEDERM®ULTRASMILE Europe;JUVEDERM®VOLBELLAwith 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(s)正确(如果页面上有“Juvederm” - 它显示“Passed”,如果没有 - “Not PASSED”:

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”)。

请告诉史密斯。

javascript jasmine protractor
2个回答
1
投票

您需要更改expect语句,如下所示。

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

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®');
    }); 
});
© www.soinside.com 2019 - 2024. All rights reserved.