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

本文关键字:未指定 会话 ID InvalidOperationException System wait until 引发 使用 | 更新日期: 2023-09-27 18:29:06

我有很多测试用例,需要等待页面加载。由于加载时间不同,我使用了显式等待。

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有人能帮助我们克服这种情况吗。谢谢

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

当方法中没有有效的驱动程序实例时,

System.InvalidOperationException: No session ID specified occurs

请检查代码中是否正确实例化了Drivers._driverInstance。如果可能,在问题中张贴Drivers类的相关部分。

请参阅另一个SO问题,其中讨论了相同的问题:使用Selenium webdriver+specflow+c#+Pageobject+pagefactory 禁用浏览器javascript

我基本上来自Java,但观察代码和异常,我发现WebDriverWait是在SDDirectPage中定义的,并调用这里。对吗?

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

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

感谢