使用量角器无法在Safari上的iframe中找到元素

时间:2019-01-08 12:41:48

标签: javascript selenium iframe safari protractor

我们有一个带iframe的html页面。如下面的代码所示,iframe是由javascript生成的(这是简化版本,因为我们无法显示生产代码)。我们的目标是使用量角器编写端到端测试。我们要确保iframe中存在div元素(下面也给出了测试)。问题是该测试通过了Mac OS Mojave上的Chrome,但通过了Safari 12.0.2。 如果JS未生成iframe,则测试将在Safari上通过。

<html>
<head>
    <script>

function domOnLoad() {
    const rootElement = document.getElementById('root');

    const iframeElement = document.createElement('iframe');
    iframeElement.frameBorder = 0;
    iframeElement.seamless = true;
    iframeElement.scrolling = 'no';

    rootElement.appendChild(iframeElement);

    const divElement  = document.createElement('div');
    divElement.id = 'iframeRoot';

    const textElement = document.createTextNode('Test Iframe');
    divElement.appendChild(textElement);

    iframeElement.contentDocument.body.appendChild(divElement);

    console.log(rootElement);
}

    </script>
</head>
    <body onload="domOnLoad()">
        <div id="root">
        </div>
    </body>
</html>

describe('Test should have', function() {
    browser.waitForAngularEnabled(false);

    it('div inside iframe with an id iframeRoot', () => {
        browser.get('http://localhost:5000/examples/iframe.html/');
        browser.switchTo().frame(0);

        var divInsideIframe =  $('#iframeRoot');
        expect(divInsideIframe.isPresent()).toBeTruthy();
    });
});

1 个答案:

答案 0 :(得分:0)

您必须先切换到iframe

browser.switchTo().frame('iframe');

然后尝试查找iframe中的元素

希望这对您有帮助