代码看起来很冗长,但这是一个简单的程序 我已经构建了一个控制台应用程序(TakeScreenshots),它将从firefox,chrome和amp;中获取网站截图。即以该顺序&将它们保存在文件夹中。当我手动运行TakeScreenshots.exe时,将保存所有3个屏幕截图。
现在,我已经构建了另一个将执行TakeScreenshots.exe的控制台应用程序(MyApp)。但通过这种方式,只保存firefox截图而不保存其他2.没有例外。它只是说“过程完成”。我想,MyApp并没有等待TakeScreenshots完成。
我该如何解决这个问题。
[TakeScreenshots稍后将被放置在几台远程计算机和由MyApp运行]
TakeScreenshots代码:
private static string[] WebDriversList = ["firefox","chrome","internetexplorer"];
private static void TakeAPic()
{
string url = "http://www.google.com";
string fileNamePrefix = "Test";
string snapSavePath = "D:\\Pics\\";
foreach (string wd in WebDriversList)
{
IWebDriver NewDriver = null;
switch (wd.ToLower())
{
case "firefox":
using (NewDriver = new FirefoxDriver())
{
if (NewDriver != null)
{
CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath);
}
}
break;
case "chrome":
using (NewDriver = new ChromeDriver(WebDriversPath))
{
if (NewDriver != null)
{
CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath);
}
}
break;
case "internetexplorer":
using (NewDriver = new InternetExplorerDriver(WebDriversPath))
{
if (NewDriver != null)
{
CaptureScreenshot(NewDriver, url, fileNamePrefix, snapSavePath);
}
}
break;
}
if (NewDriver != null)
{
NewDriver.Quit();
}
}
}
private static void CaptureScreenshot(IWebDriver driver,string url,string fileNamePrefix,
string snapSavePath)
{
driver.Navigate().GoToUrl(url);
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities;
ss.SaveAsFile(snapSavePath + fileNamePrefix + "_" + capabilities.BrowserName + ".png",
ImageFormat.Png);
}
MyApp代码:
static void Main(string[] args)
{
ExecuteTakeScreenshot();
Console.WriteLine("PROCESS COMPLETE");
Console.ReadKey();
}
private static void ExecuteTakeScreenshot()
{
ProcessStartInfo Psi = new ProcessStartInfo("D:\\PsTools\\");
Psi.FileName = "D:\\PsTools\\PsExec.exe";
Psi.Arguments = "/C \\DESK101 D:\\Release\\TakeScreenshots.exe";
Psi.UseShellExecute = false;
Psi.RedirectStandardOutput = true;
Psi.RedirectStandardInput = true;
Process.Start(Psi).WaitForExit();
}
更新
这是我的错。最初WebDriversPath
被分配了“WebDrivers /”。当我将其更改为实际路径“D:\ WebDrivers \”时,它有效。但我仍然不明白当TakeScreenshots.exe手动运行时它是如何工作的,当它从另一个控制台运行时它不会这样做
答案 0 :(得分:1)
在类似的问题中,我已经成功地等待输入空闲。像这样:
Process process = Process.Start(Psi);
process.WaitForInputIdle();
process.WaitForExit();
你可以试试这个。对我来说,需要使用Adobe Reader打印pdf,而不是在之后的早期关闭它。
示例:
Process process = new Process();
process.StartInfo.FileName = DestinationFile;
process.StartInfo.Verb = "print";
process.Start();
// In case of Adobe Reader the following statement is needed:
process.WaitForInputIdle();
process.WaitForExit(2000);
process.WaitForInputIdle();
process.Kill();