使用wait.until throws System.InvalidOperationException:未指定会话ID

时间:2016-01-06 13:21:54

标签: c# firefox selenium selenium-webdriver

我有很多测试用例,我需要等到页面加载完毕。我正在使用显式等待,因为加载时间会有所不同。

WebDriverWait _wait = new WebDriverWait(Drivers._driverInstance, new TimeSpan(0, 2, 0));
_wait.Until(D => D.Title);

它抛出无会话ID异常。我有一个表格需要花时间加载页面,所以我尝试使用

 _wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("table")));

即使这会引发同样的错误。当我单独运行每一个并运行所有这些测试时,测试会通过,因为套件会引发此异常。

private static void InitialUpload(string filename)
    {
        SDDirectPage.filePath = filename;
        SDDirectPage.filename = Path.GetFileNameWithoutExtension(SDDirectPage.filePath);
        SDDirectPage.UploadButton.Click();
        Drivers._driverInstance.SwitchTo();
        SDDirectPage.FileReference = SDDirectPage.filename;
        SDDirectPage.UploadTheFile();

        //Check whether 404 occurred or the uploading file is smooth
        if (Drivers._driverInstance.Title == "404 - File or directory not found.") //A bug at the moment, it uploads corrupted files most of the times.
        {
            Assert.Fail("404 error occurred. File might be corrupted or file mightnot be in the specified location..!");
            return;
        }
        else
        {
            Drivers._driverInstance.SwitchTo().ParentFrame();
          //  SDDirectPage._wait.Until(D => D.Title);
            SDDirectPage._wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("table")));

            Assert.AreEqual(SDDirectPage.filename + " - SmartDebit Front End Portal", Drivers._driverInstance.Title);
        }
    }

这是我获得例外的功能。在一些测试用例中,我有Assert.AreEqual,我在那里比较标题。在这些测试用例中, driver.Title 引发了同样的错误。

以下是例外:

Test FullName:  SDTestAutomation.SDDirectPage_Tests.FixInvalidRows_Search
Test Source:    c:\Git\AutomationTest\automationtest\AutomationTest\SDTestAutomation\SDDirectPage_Tests.cs : line 249
Test Outcome:   Failed
Test Duration:  0:02:19.2199494

Result Message: 

Test method SDTestAutomation.SDDirectPage_Tests.FixInvalidRows_Search threw exception: 
System.InvalidOperationException: No session ID specified
Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByTagName(String tagName)
   at OpenQA.Selenium.By.<>c__DisplayClass1a.<TagName>b__18(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
   at OpenQA.Selenium.Support.UI.ExpectedConditions.<>c__DisplayClass13.<ElementIsVisible>b__12(IWebDriver driver)
   at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
   at SDTestAutomation.SDDirectPage_Tests.InitialUpload(String filename) in c:\Git\AutomationTest\automationtest\AutomationTest\SDTestAutomation\SDDirectPage_Tests.cs:line 483
   at SDTestAutomation.SDDirectPage_Tests.FixInvalidRows_Search() in c:\Git\AutomationTest\automationtest\AutomationTest\SDTestAutomation\SDDirectPage_Tests.cs:line 250

这是驱动程序类:

 public class Drivers
{
    static string path = @"C:\SmartDebit\SmartDebitTestAutomation\SmartDebitFramework\DriverResources\";

    public static IWebDriver _driverInstance { get; set; }

    public static void Initialize(string browser)
    {
        if (browser == "FF")
        {
            _driverInstance = new FirefoxDriver();
            _driverInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5000));
        }

        if (browser == "IE")
        {
            _driverInstance = new InternetExplorerDriver(path);
            _driverInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5000));
        }

        if (browser == "Chrome")
        {
            _driverInstance = new ChromeDriver(path);
            _driverInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5000));
        }

    }
}

初始化浏览器实例的代码:

[ClassInitialize]
    public static void BrowserInstance(TestContext t)
    {
        loginPage = new LoginPage();
        loginPage.Init("FF");
        loginPage.Goto("url of the application");
        Assert.AreEqual("Login Page", Drivers._driverInstance.Title, "Login page titles doesn't match");
    }

    [TestInitialize]
    public void Init()
    {
        try
        {
            Login();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception:" + ex);
            Assert.Fail("ValidLogin() test failed in HomePage_Tests.cs");
            loginPage.QuitBrowser();
        }
    }

    private static void Login()
    {
        loginPage.LoginName = "username";
        loginPage.Password = "password";
        loginPage.LoginButton();
        SDDirectPage._wait.Until(d=>Drivers._driverInstance.Title);
        Assert.AreEqual("Home Page - Front End Portal", Drivers._driverInstance.Title, "Home page title doesn't match");
        Assert.IsTrue(HomePage.loggedInUserText.Contains("username"));
    }

我使用的是Firefox 43.0.2 有人可以帮助克服这种情况。 感谢。

2 个答案:

答案 0 :(得分:0)

System.InvalidOperationException: No session ID specified occurs当您在方法中没有有效的驱动程序实例时。

请检查代码中是否已Drivers._driverInstance正确实例化。如果可能,请在问题中发布Drivers类“相关部分。

请参阅另一个问题,讨论相同的问题:Disabling browser javascript with Selenium webdriver + specflow + c# + Pageobject + pagefactory

答案 1 :(得分:0)

我基本上是从Java开始,但是观察代码和异常,我在SDDirectPage中定义了WebDriverWait,并在此处调用。右边?

   SDDirectPage._wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("table")));

当您调用定义的等待方法时,您不会在此处传递webdriver实例。所以我期待切换到这个方法驱动程序失去会话。为了确认,您是否可以通过注释此等待行来尝试执行相同的操作(并且我在Java中使用等同于Thread.sleep(5000)的东西一次以检查此问题)

由于