在运行TFS 2018构建代理的构建计算机上,Selenium测试无法与IE11一起运行

时间:2018-03-14 23:47:35

标签: c# selenium internet-explorer tfs tfs2018

我已经尝试了一段时间,在我们的构建机器上使用IE 11进行selenium测试。我们的构建机器使用IE11运行Windows 10,我们使用TFS 2018.构建代理配置为使用自动登录交互运行。我的测试使用IE,Chrome和Firefox在我的本地计算机上运行。 Chrome和Firefox也适用于构建机器,只是IE我无法上班。我已根据以下文档添加了注册表项并检查了所有其他设置:

https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver

我正在进行的测试只是一个基本样本。

[TestClass]
public class MySeleniumTests
{
    private TestContext testContextInstance;
    private IWebDriver driver;
    private string appURL;

    public MySeleniumTests()
    {
    }

    [TestMethod]
    [TestCategory("Chrome")]
    public void TheBingSearchTest()
    {
        driver.Navigate().GoToUrl(appURL);
        driver.FindElement(By.Id("sb_form_q")).SendKeys("VSTS");
        driver.FindElement(By.Id("sb_form_go")).Click();
        driver.FindElement(By.XPath("(//ol[@id='b_results']/li/h2/a/strong)[3]")).Click();
        Assert.IsTrue(driver.Title.Contains("VSTS"), "Verified title of the page");
    }

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }

    [TestInitialize()]
    public void SetupTest()
    {
        appURL = "http://www.bing.com/";

        string browser = TestContext.Properties["browser"].ToString();
        switch (browser)
        {
            case "Chrome":
                driver = new ChromeDriver();
                break;
            case "Firefox":
                driver = new FirefoxDriver();
                break;
            case "IE":
                driver = new InternetExplorerDriver();
                break;
            default:
                driver = new ChromeDriver();
                break;
        }

    }

    [TestCleanup()]
    public void MyTestCleanup()
    {
        driver.Quit();
    }
}

每次使用正确的网站启动浏览器实例,但无法继续执行以下错误:

2018-03-14T20:53:53.1180620Z Started InternetExplorerDriver server (32-bit)
2018-03-14T20:53:53.1180620Z 3.11.0.0
2018-03-14T20:53:53.1180620Z Listening on port 53215
2018-03-14T20:53:53.1180620Z Only local connections are allowed
2018-03-14T20:53:54.8474473Z Failed   TheBingSearchTest
2018-03-14T20:53:54.8474473Z Error Message:
2018-03-14T20:53:54.8474473Z  Test method Web.Tests.MySeleniumTests.TheBingSearchTest threw exception: 
2018-03-14T20:53:54.8474473Z OpenQA.Selenium.NoSuchElementException: Unable to find element with css selector == #sb_form_q
2018-03-14T20:53:54.8474473Z Stack Trace:
2018-03-14T20:53:54.8474473Z     at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
2018-03-14T20:53:54.8474473Z    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
2018-03-14T20:53:54.8474473Z    at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
2018-03-14T20:53:54.8474473Z    at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(String id)
2018-03-14T20:53:54.8474473Z    at OpenQA.Selenium.By.<>c__DisplayClass16_0.<Id>b__0(ISearchContext context)
2018-03-14T20:53:54.8474473Z    at OpenQA.Selenium.By.FindElement(ISearchContext context)
2018-03-14T20:53:54.8474473Z    at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
2018-03-14T20:53:54.8474473Z    at Web.Tests.MySeleniumTests.TheBingSearchTest() in E:\BuildAgents\BuildAgent1\_work\16\s\Web.Tests\SeleniumBingTest.cs:line 29
2018-03-14T20:53:54.8474473Z

我现在已多次检查所有内容并且也在尝试不同的事情,但我没有取得任何进展。还有其他人有过类似的问题吗?

---编辑03/15 ---

根据DebanjanB的建议添加延迟后,我现在得到以下异常。它说窗户已关闭,但实际上仍然是开着的。这种行为太奇怪了。我相信我缺少一些基础设施配置,这与运行测试的TFS测试代理相关,但我无法弄清楚它可能是什么。

2018-03-15T14:20:21.5983187Z Starting test execution, please wait...
2018-03-15T14:20:22.5350340Z Started InternetExplorerDriver server (32-bit)
2018-03-15T14:20:22.5350340Z 3.9.0.0
2018-03-15T14:20:22.5350340Z Listening on port 63267
2018-03-15T14:20:22.5350340Z Only local connections are allowed
2018-03-15T14:20:23.2691823Z Skipped  LoginFirstTimeTest
2018-03-15T14:20:54.6453061Z Failed   TheBingSearchTest
2018-03-15T14:20:54.6453061Z Error Message:
2018-03-15T14:20:54.6453061Z  Test method Web.Tests.MySeleniumTests.TheBingSearchTest threw exception: 
2018-03-15T14:20:54.6453061Z OpenQA.Selenium.WebDriverTimeoutException: Timed out after 30 seconds ---> OpenQA.Selenium.NoSuchWindowException: Currently focused window has been closed.
2018-03-15T14:20:54.6453061Z Stack Trace:
2018-03-15T14:20:54.6453061Z     at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
2018-03-15T14:20:54.6453061Z    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
2018-03-15T14:20:54.6453061Z    at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
2018-03-15T14:20:54.6453061Z    at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(String id)
2018-03-15T14:20:54.6453061Z    at OpenQA.Selenium.By.<>c__DisplayClass16_0.<Id>b__0(ISearchContext context)
2018-03-15T14:20:54.6453061Z    at OpenQA.Selenium.By.FindElement(ISearchContext context)
2018-03-15T14:20:54.6453061Z    at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
2018-03-15T14:20:54.6453061Z    at SeleniumExtras.WaitHelpers.ExpectedConditions.<>c__DisplayClass19_0.<ElementToBeClickable>b__0(IWebDriver driver)
2018-03-15T14:20:54.6453061Z    at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
2018-03-15T14:20:54.6453061Z --- End of inner exception stack trace ---
2018-03-15T14:20:54.6453061Z     at OpenQA.Selenium.Support.UI.DefaultWait`1.ThrowTimeoutException(String exceptionMessage, Exception lastException)
2018-03-15T14:20:54.6453061Z    at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
2018-03-15T14:20:54.6453061Z    at Web.Tests.MySeleniumTests.TheBingSearchTest() in E:\BuildAgents\BuildAgent1\_work\16\s\Web.Tests\SeleniumBingTest.cs:line 31
2018-03-15T14:20:54.6453061Z 

1 个答案:

答案 0 :(得分:0)

错误说明了一切:

2018-03-14T20:53:54.8474473Z  Test method Web.Tests.MySeleniumTests.TheBingSearchTest threw exception: 
2018-03-14T20:53:54.8474473Z OpenQA.Selenium.NoSuchElementException: Unable to find element with css selector == #sb_form_q

从错误堆栈跟踪可以清楚地看出,驱动程序无法找到标识为的元素:

driver.FindElement(By.Id("sb_form_q"))

正如您所提到的,测试使用IE,Chrome和Firefox在本地计算机上运行。 Chrome和Firefox也适用于构建计算机,只是IE无法开始工作可能会因为不同的 Web浏览器以不同方式呈现 HTML DOM 而发生。

解决方案

解决方案是使用WebDriverWaitExpectedConditions诱导ElementToBeClickable,如下所示:

wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("sb_form_q"))).SendKeys("VSTS");