使用Coded UI测试生成器自动化网站时遭遇FailedToLaunchApplicationException异常
解决Coded UI测试启动Chrome时的FailedToLaunchApplicationException异常
我来帮你排查这个启动Chrome时遇到的FailedToLaunchApplicationException问题,这在Coded UI测试里挺常见的,咱们一步步来定位和解决:
1. 先确认Chrome的路径是否正确
最常见的原因就是指定的Chrome exe路径不对:
- 如果你是64位系统,Chrome可能安装在
C:\Program Files\Google\Chrome\Application\chrome.exe(不带x86),而不是ProgramFiles(x86)目录,先去文件管理器里确认实际路径。 - 检查代码里
this.openchromeParams.ExePath的实际值,建议先直接写死绝对路径测试,比如:ApplicationUnderTest chromeApplication = ApplicationUnderTest.Launch(@"C:\Program Files\Google\Chrome\Application\chrome.exe"); - 路径里的空格不需要额外转义,因为
@符号已经处理了字符串转义。
2. 检查权限问题
Coded UI测试需要足够的权限才能启动外部应用:
- 右键你的Visual Studio,选择以管理员身份运行,再执行测试,避免权限不足导致的启动失败。
- 确认你的测试账号有Chrome安装目录的读写权限,尤其是公司电脑可能有组策略限制,需要确保能访问Chrome的exe文件。
3. 尝试添加Chrome启动参数
有时候Chrome的沙盒模式会干扰启动,试试添加--no-sandbox参数:
ApplicationUnderTest chromeApplication = ApplicationUnderTest.Launch(@"C:\Program Files\Google\Chrome\Application\chrome.exe", "--no-sandbox https://www.facebook.com");
这样还能直接在启动时打开Facebook,省去后续地址栏输入的步骤,也能验证Chrome是否能正常启动。
4. 捕获异常详情定位问题
默认的异常提示可能不够具体,建议在代码里加try-catch,输出更详细的错误信息:
public void openchrome() { try { ApplicationUnderTest chromeApplication = ApplicationUnderTest.Launch(this.openchromeParams.ExePath, this.openchromeParams.AlternateExePath); } catch (FailedToLaunchApplicationException ex) { Console.WriteLine($"启动失败原因:{ex.Message}"); if (ex.InnerException != null) { Console.WriteLine($"内部错误:{ex.InnerException.Message}"); } } }
比如如果是“找不到指定文件”,那就是路径问题;如果是“拒绝访问”,就是权限问题,这样能精准定位。
5. 检查Coded UI与Chrome的兼容性
旧版本的Coded UI测试可能不支持最新版的Chrome:
- 确认你的Visual Studio版本,比如VS2019支持Chrome 70及以上版本,VS2017需要更新到最新补丁才能支持较新的Chrome。
- 如果你的Chrome版本太新,而VS版本较老,可以尝试降级Chrome或者升级Visual Studio到最新版本。
优化后的示例代码
这里给你一个更可靠的启动Chrome并访问Facebook的示例:
public void OpenChromeAndLoginToFacebook() { try { // 替换为你实际的Chrome路径 string chromeExePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe"; // 启动Chrome并直接打开Facebook登录页 ApplicationUnderTest chromeApp = ApplicationUnderTest.Launch(chromeExePath, "--no-sandbox https://www.facebook.com/login"); // 后续的用户名密码输入操作(确保控件已正确识别) WinEdit usernameField = this.UIFacebookWindow.UIUsernameEdit; WinEdit passwordField = this.UIFacebookWindow.UIPasswordEdit; WinButton loginButton = this.UIFacebookWindow.UILoginButton; usernameField.Text = "你的用户名"; passwordField.Text = "你的密码"; loginButton.Click(); } catch (FailedToLaunchApplicationException ex) { Console.WriteLine($"启动Chrome失败:{ex.Message}"); if (ex.InnerException != null) { Console.WriteLine($"内部错误详情:{ex.InnerException.Message}"); } } catch (Exception ex) { Console.WriteLine($"其他错误:{ex.Message}"); } }
内容的提问来源于stack exchange,提问作者Baban Jamal
相关产品推荐
相关产品推荐

