使用聚合物 2 刺穿阴影 dom 进行测试自动化(/deep/已弃用) [英] Test automation with polymer 2 piercing the shadow dom (/deep/ deprecated)
问题描述
目前我的组织在 Polymer 1 上使用 Selenium、Protractor 和 Mocha,我们使用 shady dom.对于验收测试,我们使用 /deep/
组合器来穿透 DOM
.对于聚合物 2,不推荐使用 /deep/
组合器.
Currently my org is on Polymer 1 with Selenium, Protractor and Mocha and we use the shady dom. For acceptance tests we use the /deep/
combinator to pierce the DOM
. With polymer 2 the /deep/
combinator is deprecated.
我的问题:在编写验收测试时,如果使用 /deep/
组合子,我应该如何穿透阴影 DOM
已弃用?
My Question: When writing acceptance tests, how am I supposed to pierce the shadow DOM
if the use of the /deep/
combinator is deprecated?
推荐答案
@Dilip Meghwal 答案的简化版:
A simplified version of @Dilip Meghwal's answer:
public static WebElement pierceShadowRoot(String... selectors) {
WebElement element = null;
WebElement shadowRoot = null;
for (String selector : selectors) {
if (element == null) {
element = getDriver().findElement(By.cssSelector(selector));
} else {
shadowRoot = (WebElement) getDriver().executeScript("return arguments[0].shadowRoot", element);
element = (WebElement) shadowRoot.findElement(By.cssSelector(selector));
}
}
return element;
}
举个例子,这里是我如何使用它来定位谷歌浏览器的清除缓存按钮,因为 Chrome 中使用了 Polymer 框架,其他方法不起作用:
As an example, here's how I used it to locate Google Chrome's Clear cache button, where other ways didn't work because of Polymer framework used in Chrome:
WebElement targetElement = pierceShadowRoot("body > settings-ui", "#main", "settings-basic-page",
"#advancedPage > settings-section:nth-child(1) > settings-privacy-page", "settings-clear-browsing-data-dialog",
"#clearBrowsingDataConfirm");
这篇关于使用聚合物 2 刺穿阴影 dom 进行测试自动化(/deep/已弃用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!