2
votes

I have a print button in my application. This print button opens a PDF in a new window. On top of this window is the Print icon that can be clicked to open a Chrome's print preview window where one can select "Save as PDF" option to save the PDF file.

I would like to test this flow in selenium.

--- I click the Print button on my webpage. This opens a new Chrome window with the PDF

driver.findElement(By.cssSelector("ID")).click();

--- I switch to the newly opened Chrome window

driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());

I can see in the Logs that selenium is indeed switching to the new window.

--- I try to get the element of the new Chrome window opened

driver.findElement(By.cssSelector("viewer-pdf-toolbar"))

This is where the issue is. Selenium is unable to interact with this newly opened window and cant find this element. When I inspect the newly opened window I see the following DOM structure -

<body> 
    <viewer-pdf-toolbar id="toolbar"></viewer-pdf-toolbar>
    <div id="sizer" style="width: 735px; height: 1092px;"></div>
    <viewer-password-screen id="password-screen"></viewer-password-screen>
    ...
    ..
    .
</div>

The element I'm looking for does exist in the DOM structure and I can access it via Console but I cant access it using Selenium. You can view this DOM by opening any PDF file in Chrome browser and inspecting the window. Any ideas why this is happening?

Note : When the new window loads, for a second or two it shows "Printing, Please wait" and then the PDF loaded. Not sure if this information is relevant to the problem here.

Additional Information -

In debug mode, after the Driver has switched to the new PDF window I evaluated - driver.getCurrentUrl() - this URL is the correct URL as displayed in the address bar of the preview window. This means that my driver is on the correct window.

But when I execute driver.getPageSource - I get the following output

<html>
<head>
</head>
<body style="height: 100%; width: 100%; overflow: hidden; margin:0px; background-color: rgb(82, 86, 89);">
<embed name="XXXXXXD474B71424EC89403FB4FA75CF" style="position:absolute; left: 0; top: 0;" width="100%" height="100%" src="about:blank" type="application/pdf" internalid="XXXXXXD474B71424EC89403FB4FA75CF"></body>
</html>

So the driver sees this as the page source whereas when I do an inspect on the same window I see a different DOM. Why is this happening? Is Chrome replacing the <embed> tag with the final DOM? How can selenium be made aware of this change?

Please help!

1
did you really want to check the flow or just want to download the pdf? For that you can use DesiredCapabilities.mkhurmi
I have to check the flow and interact with this windowArchit Arora
@ArchitArora have you made any progress on this? how did you resolve it?Doug Clark

1 Answers

0
votes

Induce WebDriverWait() and wait for visibilityOfElementLocated() and following css selector.

new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#toolbar")));

Update:

new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body")));