【发布时间】:2017-09-21 08:01:07
【问题描述】:
我正在尝试创建一个简单的自动化任务。现在我想登录网页。当我在 Visual Studio 2017 中运行测试时,这一切正常。但是,这需要在 Windows 系统中定期作为 exe 执行。当我运行可执行文件时,它在显示后挂起:
启动 ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a)
在端口 9515
只允许本地连接。
我知道这只是与此问题相关的大多数 SO 帖子的信息,而不是错误,例如 - When running WebDriver with Chrome browser, getting message, "Only local connections are allowed" even though browser launches properly (注意:我的浏览器没有启动)
如何将测试代码作为可执行文件运行? 作为可执行文件运行时,我是否必须编写额外的代码来调用测试?
namespace RevuSeleniumAutomation
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.PhantomJS;
using System;
[TestClass]
public class RevuAutomater
{
private string baseURL = "http://example.com/";
private RemoteWebDriver driver;
private string browser;
public TestContext TestContext { get; set; }
[TestMethod]
[TestCategory("Selenium")]
[Priority(1)]
[Owner("Chrome")]
public void AutomateSite()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl(this.baseURL);
driver.FindElementById("UserName").SendKeys("user");
driver.FindElementById("Password").SendKeys("12345");
}
[TestCleanup()]
public void MyTestCleanup()
{
driver.Quit();
}
[TestInitialize()]
public void MyTestInitialize()
{
}
}
}
The cmd window message after launching exe
感谢您的宝贵时间。
【问题讨论】:
-
您是否在 Visual Studio 中运行并在同一台机器上作为 EXE 运行?如果是这样,它应该在 Visual Studio 中运行,因为两者都使用相同的 chrome 和 chromedriver.exe。
-
问题是 chrome 驱动程序挂起,您需要在再次调用 exe 之前退出所有 chromedriver 进程。示例 cmd 命令可用作代码的第一部分:“taskkill /F /IM chromedriver.exe /T”,需要在 cmd 上运行
标签: selenium selenium-webdriver selenium-chromedriver