Shadow DOM 中如何等待元素可见的解决方案

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

目前,Robot Framework 库中还没有现成的解决方案来说明如何等待元素在 Shadow DOM 中的页面上可见。我想出了一种方法。也许对其他人有用。

我创建了一个关键字:

Verify ShadowDom Element Visible
    [Arguments]    ${Element}
    register keyword to run on failure    Capture Page Screenshot
    ${previous_kw}=    register keyword to run on failure    Nothing
    FOR    ${index}    IN RANGE    1    30
        sleep    1s
        ${status2}=    Run Keyword and return Status    execute javascript    return ${Element}.checkVisibility();
        IF    '${status2}'=='False'    CONTINUE
        ...    ELSE    BREAK
    END
    register keyword to run on failure    ${previous_kw}
    IF    '${status2}'=='False'    Capture Page Screenshot
    IF    '${status2}'=='False'    Fail    Element not visible after 30 sec.


Verify ShadowDom Element Visible      document.querySelector("xxxx-block-wizard-footer").shadowRoot.querySelector(".xxxx-block-wizard-footer__cta-primary").shadowRoot.querySelector("button")

在这种情况下,我们期望 Shadow DOM 中的某个按钮在 30 秒内出现在页面上。如果按钮没有出现,将捕获屏幕截图并将失败消息发送到日志。

注意:如果该按钮可见,则所有验证失败的尝试都不会截取屏幕截图。 祝你好运。

element visibility shadow-dom
1个回答
0
投票

首先,感谢您为我指明了正确的方向! :) 不过,您的关键字包含错误和一些不必要的内容:

${status2}=    Run Keyword and return Status    execute javascript    return ${Element}.checkVisibility();

仅当 javascript 返回异常时(即使该元素不可见),这实际上才返回 False

在这里,我根据您的想法创建了一个关键字,我认为可以更广泛地使用它,并且不会出现上述问题:

Wait Until ShadowDom Element is Visible 
    [Arguments]    ${locator}    ${check_period}=1s    ${max_tries}=30 
    ${try}    Set Variable    ${1} 
    ${max_tries}    Convert To Integer    ${max_tries} 
    #locators are in 'dom:<selector>' format, we need to get rid of the 'dom:' substring
    ${locator}    Replace String    ${locator}    dom:    ${EMPTY} 
    WHILE    ${try} <= ${max_tries}  
        ${element_visible}    Run Keyword And Continue On Failure    Execute Javascript    return ${locator}.checkVisibility();
        # if there is a JavascriptException (ie. the DOM is not fully loaded yet) this returns None
        IF    ${element_visible}    BREAK 
        ${try}    Set Variable    ${try + 1} 
        Sleep    ${check_period} 
    END    
    IF    ${element_visible} 
        Log    Element found at ${locator} 
    ELSE 
        Fail    Element Not Visible at ${locator} 
    END
© www.soinside.com 2019 - 2024. All rights reserved.