如何验证是否在浏览器中打开了网页

问题描述 投票:0回答:1

我正在自动化基于Windows的桌面应用程序(C#,LeanFT)。 单击按钮,在浏览器中打开一个网页。 如何验证网页是否已打开?

c# automation leanft
1个回答
0
投票

两种方式:

  1. 蛮力 通过描述打开的浏览器,其中包含标题,网址和其他属性,然后attaching。 这种方法的问题是,如果浏览器没有打开,它会抛出一个错误,所以你必须try..catch那个错误 例如: /* * Desktop related logic that opens a browser */ // Use "Attach" to connect a new (or replacement) browser tab with the LeanFT test. try { IBrowser yourPage = BrowserFactory.Attach(new BrowserDescription { Title = "The title of the page", Url = "https://thesitethatwasopened.com" }); } catch (Exception ex) { // the browser was not opened } /* * Rest of the desktop app actions */
  2. 迭代所有打开的浏览器 你仍然需要相同的描述,但这样你就可以根本没有浏览器,这意味着页面没有被打开,或者一个或多个浏览器 - 在任何一种情况下,这都不会引发错误,所以你可以称之为“更清洁”的方式: 例如: /* * Desktop related logic that opens a browser */ // Use "GetAllOpenBrowsers" to get a collection of IBrowser instances that matches the description IBrowser[] yourPages = BrowserFactory. GetAllOpenBrowsers(new BrowserDescription { Title = "The title of the page", Url = "https://thesitethatwasopened.com" }); /* * Rest of the desktop app actions (maybe by making use of yourPages.Count */
© www.soinside.com 2019 - 2024. All rights reserved.