我正在使用specflow场景功能文件和执行步骤进行自动测试。我陷入了执行chrome中取消打印对话框的情况。
Scenario:User can get report
When I select 'All ' tab
And I click on Report Icon to preview the page
And I click the cancel button
Then the preview page should be closed
[When(@"I select '(.*)' tab")]
public void WhenIChooseTheTab(string tab)
{
string elementId = $"{tab.Replace(" ",string.Empty).ToLower()}_TabAId";
navSteps.WaitUntilElementIsDisplayed(elementId);
this.testContext.Browser.FindElement(By.Id(elementId)).Click();
}
[When(@"I click on Report Icon to preview the page")]
public void ThenIClickOnReportIconToPreviewThePage()
{
this.testContext.Browser.FindElement(By.Id("printReport_AId")).Click();
}
[When(@"I click the cancel button")]
public void WhenIClickTheCancelButton()
{
this.testContext.Browser.FindElement(By.ClassName("cancel-button")).Click();
}
无法识别取消按钮。我应该添加与Web驱动程序相关的still吗?。
因为当我单击报告图标时,创建了chrome的新实例
答案 0 :(得分:0)
如果您仍然决定朝该方向前进,请注意,“取消”按钮已隐藏在Shadow DOM中,因此您需要标识所有ShadowRoot个元素,并将其强制转换为{{ 3}},找到“有趣的”孩子,重复
示例代码:
driver.SwitchTo().Window(driver.WindowHandles[1]);
IWebElement printPreviewApp = driver.FindElement(By.TagName("print-preview-app"));
IWebElement printPreviewAppConten = expandShadowRoot(printPreviewApp, driver);
IWebElement printPreviewSidebar = printPreviewAppConten.FindElement(By.TagName("print-preview-sidebar"));
IWebElement printPreviewSidebarContent = expandShadowRoot(printPreviewSidebar, driver);
IWebElement printPreviewHeader = printPreviewSidebarContent.FindElement(By.TagName("print-preview-header"));
IWebElement printPreviewHeaderContent = expandShadowRoot(printPreviewHeader,driver);
printPreviewHeaderContent.FindElements(By.TagName("paper-button"))[1].Click();
关联的expandShadowRoot
函数:
private IWebElement expandShadowRoot(IWebElement parent, IWebDriver driver) {
return (IWebElement) ((IJavaScriptExecutor )driver).ExecuteScript("return arguments[0].shadowRoot", parent);
}