我正在自动化一个基于Windows的桌面应用程序(C#,LeanFT(。
单击按钮,在浏览器中打开一个网页。
如何验证网页是否已打开?
两种方式:
-
蛮 力
通过描述打开的浏览器,该浏览器具有标题,URL和其他属性,然后附加到该浏览器。
这种方法的问题在于,如果浏览器没有打开,它将抛出错误,因此您必须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 */
-
遍历所有打开的浏览器
您仍然需要相同的描述,但这样您就可以完全没有浏览器,这意味着页面未打开,或者一个或多个浏览器 - 在任何一种情况下,这都不会引发错误,因此您可以将其称为"更干净"的方式:例如:
/* * 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 */