【问题标题】:Get injected javascript from a website when testing with Selenium / Protractor使用 Selenium / Protractor 进行测试时从网站获取注入的 javascript
【发布时间】:2021-10-21 11:16:49
【问题描述】:

我正在使用 Selenium / Protractor 来测试我的应用程序的 UI 和 html。我有一个注入到网站中的,我想获取这个脚本标签的内容,以便我可以将它用于一些测试。

脚本示例如下:

<script type="application/ld+json">

    {
        "@context": "http://schema.org",
        "@type": "LocalBusiness",
        "name": "The Best Company Name",
        "image": "https://www.urltowebsite.us/WebContent/Images/Dealer/6/Logo.png?preset=Dealer.Logo",
        "address": {
            "@type": "PostalAddress",
            "streetAddress": "19 North Suite 2 ",
            "addressLocality": "Mobile",
            "addressRegion": "MS",
            "postalCode": "39107",
            "addressCountry": "USA"
        },
        "geo": {
            "@type": "GeoCoordinates",
            "latitude": "32.351923",
            "longitude": "-88.73434"
        },
        "openingHoursSpecification": [{
                "@type": "OpeningHoursSpecification",
                "dayOfWeek": [
                "Monday"
                ],
                "opens": "07:30:00",
                "closes": "17:00:00"
            },
            {
                "@type": "OpeningHoursSpecification",
                "dayOfWeek": [
                "Tuesday"
                ],
                "opens": "07:30:00",
                "closes": "17:00:00"
            },
            {
                "@type": "OpeningHoursSpecification",
                "dayOfWeek": [
                "Wednesday"
                ],
                "opens": "07:30:00",
                "closes": "12:00:00"
            }
        ]
    }
</script>

我在下面尝试了这种方法,有时它似乎有效,但其他时候我只会得到 NULL。可能是因为时间问题?

// site.po.ts (po == page object)
export class Site {
    async getLDJson() {
        const ldJsonScript = await browser.element(by.xpath(
            '//script[@type="application/ld+json"]'
        )).getAttribute('innerHTML');
        const parsedLdJson = JSON.parse(ldJsonScript);
        return parsedLdJson;
    }
}

我会在测试之前调用它,这样我就可以保证我有信息

// dealership.test.ts
describe('Dealership Site', () => {
    const site: Site = new Site();
    const cu: ContactUsPage = new ContactUsPage();

    beforeAll(async () => {
        await cu.goToDealership();
        browser.sleep(3000);
        await site.getLDJson();
    });
});
//contact_us.po.ts
    async goToDealership(dealerName: string = 'wilsonsaw', nextPage: string = "") {
        await browser.waitForAngularEnabled(false);
        var url = this.composeUrl(dealerName);

        const currentUrl = await browser.getCurrentUrl();
        url = `${url}${nextPage}`;

        if (url !== currentUrl) {
            await browser.navigate().to(url);
        }
    }

【问题讨论】:

    标签: javascript selenium testing protractor


    【解决方案1】:

    browser.sleep(3000); 需要 await 等到它完成


    *  waitUntilElementHasAttribute
    *  @param    {ElementFinder}    $element                    Locator of element
    *  @param    {String}           attributeName               Attribute of an element to evaluate
    *  @param    {number}           [timeout=timeouts.ms1000]   Time in ms (med. is 1000ms)
    *  @return   {Promise}
    */
    let waitUntilAttributeIsntNull = ($element, attributeName, timeout = 1000) => {
        return browser.wait(
            () => $element.getAttribute(attributeName).then(attributeValue => {
                return attributeValue !== null
            }),
            timeout,
            'Wait until element \'' + $element.locator() + '\' is not null',
        );
    }
    
    // usage
    await waitUntilAttributeIsntNull(
      element(by.xpath('//script[@type="application/ld+json"]')), 
      'innerHTML', 
      3000
    )
    

    唯一可能仍然出错的是,如果元素还不存在,函数将失败。我建议只添加常规 wait for presence 然后使用此功能

    【讨论】:

    • 我将await 添加到browser.sleep 行,但它没有解决我遇到的问题。我仍然将NULL 作为我的ldJsonScript 变量。
    • 你的 goToDealership 功能是什么?
    • hm...我第一次看到browser.navigate().to(url);。它没有记录在量角器页面上。使用 browser.get 更安全
    • 好吧...那你为什么不写一个等到脚本不是null的函数呢? sleep 3000 不保证任何东西。如果您需要帮助,请告诉我
    • 不,那太复杂了。我更新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2017-08-08
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多