0

I am working in automated test using specflow scenario feature files and steps to execute . i got stucked in executing scenario for cancel print dialog box in 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();
        }

it doesn't recognize the cancel button . should i add still , related to web driver?.

because when i click report icon , new instance of chrome created

  • If you want handle this using javascript https://stackoverflow.com/questions/57189281/how-can-i-tell-selenium-to-press-cancel-on-a-print-popup-in-chrome-75?answertab=active#tab-top – Rahul L Aug 07 '19 at 10:15

1 Answers1

1
  1. First of all I would recommend to reconsider your approach as testing browser print dialog is not something you should be normally doint
  2. If you still decide to go to that direction be aware that the "Cancel" button is hidden in Shadow DOM so you will need to identify all ShadowRoot elements, cast them to IWebElement, locate the "interesting" child, repeat

    enter image description here

    Example code:

    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();
    

    The associated expandShadowRoot function:

    private IWebElement expandShadowRoot(IWebElement parent, IWebDriver driver) {
        return (IWebElement) ((IJavaScriptExecutor )driver).ExecuteScript("return arguments[0].shadowRoot", parent);
    }
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks a lot , found this approach after working whole day going through various libraries . – Anurag May 29 '21 at 12:25