【问题标题】:How to perform auto Scroll to element in Protractor test if Protractor fails to locate the element int the view?如果 Protractor 无法在视图中找到元素,如何在 Protractor 测试中执行自动滚动到元素?
【发布时间】:2018-10-11 03:05:53
【问题描述】:

我有一套测试,如果量角器无法在当前视图中找到元素,我想自动滚动到页面上的元素。

测试是在对 UI 进行一些更改以修复标题之前编写的。这导致很少有 e2e 测试失败。

目前,我必须转到每个失败的“它阻塞”并使用辅助函数滚动到元素。

如果找不到元素,我希望指示量角器再次查找元素。

这可能吗?

【问题讨论】:

    标签: angularjs jasmine protractor automated-tests end-to-end


    【解决方案1】:

    您可以创建一个等待(ExpectedCondition),例如在单击该元素或执行任何其他操作之前等待某个超时的元素。

    等待 waitUntilPresenceOfElement(element(by.id('some id')));

    export async function waitUntilPresenceOfElement(element: ElementFinder, timeout: number = 5000): Promise<any> {
        return await browser.wait(ExpectedConditions.presenceOf(element),
            timeout,
            'Waiting for element ' + element.locator() + 'to be PRSENCE with timeout ' + timeout + 'ms'
        ); }
    

    如果未找到超时定义的元素,则有问题。

    第二种解决方案是使用异常句柄并创建一些重试函数。

    export async function clearText(textBox: ElementFinder) {
    
        try {
            await textBox.clear().then(() => {
                console.log('Text field has been cleared.');
            });
        } catch (error) {
            // example for no such element exception
            if (error instanceof NoSuchElementError) {
                // retry function
            } else {
                console.log('Error wile trying to clear text from element.');
                throw error;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      在需要“滚动”到某个元素并单击看不见的元素时遇到了同样的问题。

      这会使用该元素,然后将视图转移到它,以便您可以点击它:

      等待 browser.actions().mouseMove('element').perform()

      【讨论】:

        猜你喜欢
        • 2014-01-27
        • 1970-01-01
        • 1970-01-01
        • 2015-01-17
        • 1970-01-01
        • 1970-01-01
        • 2019-03-25
        • 2018-08-09
        • 1970-01-01
        相关资源
        最近更新 更多