簡體   English   中英

如何在瀏覽器中驗證pdf查看器

[英]how to validate the pdf viewer in browser

我有一個“PDF 視圖”按鈕,單擊后,它會打開另一個新的瀏覽器窗口作為 pdf 查看器,然后我導航到第二個窗口。 在這里,我試圖驗證該頁面上是否顯示了某些 pdf 內容。 我不需要驗證任何 pdf 文本,只需要驗證某些內容可用。 在這里,我嘗試驗證 div 標簽(帶有 id)是否存在並顯示在瀏覽器窗口上,並且由於某種原因它沒有識別該元素。 請幫助我解決我的代碼有什么問題。 任何其他驗證建議或解決方案在這里都會有所幫助。

Title of the browser is : Application V5 - PDF Viewer
URL of the browser is : https://sample.com/preferences/pdfviewer.jsp

相應的html代碼是

<div id="content">
    <embed id="plugin" type="application/x-google-chrome-pdf" src="https://sample.com/servlet/pdfviewer" stream-url="chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/23576f4e-363a-4f60-a63a-b770beb66c41" headers="Cache-Control: private
Connection: Keep-Alive
Content-Disposition: inline; filename=image.pdf
Content-Length: 23721
Content-Type: application/pdf
Date: Thu, 09 Jan 2020 18:53:13 GMT
Expires: Thu, 09 Jan 2020 18:53:18 GMT
Keep-Alive: timeout=5, max=98
Server: Apache
" background-color="0xFF525659" top-toolbar-height="56" javascript="allow" top-level-url="undefined">
</div>

我的腳本有

WebElement pdfViewButton = browser.findElement(By.id("frmResults:btnPdfView"));
pdfViewButton.click();

WebDriverWait wait = new WebDriverWait(browser, 60);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));

Set<String> AllWindowHandles = browser.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
scenario.write("Currently in Parent Window = " + AllWindowHandles.toArray()[0]);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
scenario.write(String.valueOf(AllWindowHandles.size()));

String window2 = (String) AllWindowHandles.toArray()[1];
scenario.write("Switching to Child (Viewer) window = " + AllWindowHandles.toArray()[1]);
browser.switchTo().window(window2);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("content")));
Screenshot.takeScreenshot(BaseSteps.scenario, browser);
WebElement pdfViewer = browser.findElement(By.id("content"));
assertThat(pdfViewer.isDisplayed())
    .overridingErrorMessage("The pdf viewer window is launched, but there is no content is displayed.")
    .isTrue();

browser.switchTo().window(window2).close();

這是錯誤......(錯誤出現在“wait.until”行......

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.id: content (tried for 60 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at Utilities.Common.viewTheContentsWithPDFViewer(Common.java:268)
    at Steps.Steps.Verify_the_pdfviewer_is_launching_with_data_content_for_that_item(Steps.java:157)
    at ✽.And Verify the pdfviewer is launching with data content for that item(Returns.feature:19)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#content"}
  (Session info: chrome=78.0.3904.87)

我遇到過類似的問題。 在我的情況下,元素的屬性正在發生變化,我在 xpath 中使用了 contains。

嘗試檢查是否有框架並使用流暢的等待

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM