没有这样的会议。 PageFactory无法初始化元素(C#)

时间:2014-09-08 14:54:12

标签: c# selenium webdriver

我求你帮忙!

我使用PageFactory初始化我的PageOjbect。一个PageObject由两个不同的类(也是PageObects)调用。当我从第一节课中调用它时没有问题。但是当我从第二个类调用它时,弹出“没有这样的会话”错误。

我已经意识到,当PageFactory没有时间找到我页面的元素时,我意识到我有这个错误。我试图缓存所有元素,但它没有解决问题。

所以,有我的问题类:

public class OrderSettingsPage : DriverCover
{
    public OrderSettingsPage(IWebDriver driver) : base(driver)
    {
        PageFactory.InitElements(Driver, this);
    }

    public string HowToDeliver
    {
        get; set;
    }

    [FindsBy(How = How.TagName, Using = "h1"), UsedImplicitly]
    private IWebElement _mainHeadline;

    public string MainHeadline
    {
        get { return _mainHeadline.Text; }
    }

    [FindsBy(How = How.Id, Using = "Surname"), UsedImplicitly]
    private IWebElement _surnameTextBox; 

    public IWebElement SurnameTextBox
    {
        get { return _surnameTextBox; }
    }

}

显示测试步骤的一段代码:

        IWebDriver chrome = new ChromeDriver();
        MainFunctions load = new MainFunctions(chrome);
        load.NavigateToMainPage();
        load.IfNotLoggedThenLogin();
        load.NavigateToUrl(load.BaseUrl + "books/create");

        BookThemeChoosingPage bookThemeChoosing = new BookThemeChoosingPage(chrome);

        BookFormatChoosingPage bookFormatChoosing = bookThemeChoosing.ContinueWithoutTheme();

        bookFormatChoosing.ChooseFormat();

        OrderSettingsPage orderSettings = bookFormatChoosing.Continue();

        //There is my neck pain!)
        orderSettings.SetOrderSettings(howToPay, howToDeliver);

还有bookFormatChoosing.Continue()方法:

    public OrderSettingsPage Continue()
    {
        WaitAjax();
        _continueButton.Click();

        WaitAjax();
        IList<IWebElement> elements = Driver.FindElements(By.Id("loginButtonIdInBox"));
        MainFunctions load = new MainFunctions(Driver);

        if (elements.Count > 0)
        {
            load.LoginInPopUpWindow();
            WaitAjax();
        }

        WaitForPageLoading();

        int idOfAlbum = DetermineId();
        NavigateToUrl(load.BaseUrl + "order/book/" + idOfAlbum);

        return new OrderSettingsPage(Driver);
    }

那么,出了什么问题?所有元素都显示在页面上,如下所示的步骤不会导致描述的问题:

        IWebDriver chrome = new ChromeDriver();
        MainFunctions load = new MainFunctions(chrome);
        load.NavigateToMainPage();
        load.IfLoggedThenLogOut();

        switch (typeOfProduct)
        {
            case ProductType.Cup:
            {
                load.NavigateToUrl(load.BaseUrl + "souvenir/create?productClass=1");
            }
            break;

           ........       
        }

        SouvenirConstructorPage constructor = new SouvenirConstructorPage(chrome);

        OrderSettingsPage orderSettings = constructor.PlaceOrder();

        //The same method doesn't couse any problem
        orderSettings.SetOrderSettings(howToPay, howToDeliver);

Nunit中的错误:

System.InvalidOperationException : no such session
  (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64) (NoSuchDriver)

Selenium v​​.2.42 Chrome v.37.0.2062.103 Chrome驱动程序v 2.10

1 个答案:

答案 0 :(得分:1)

我的问题的答案是:永远不要在基类中使用析构函数。 在其他人继承的类中有代码行,例如:

 ~DriverCover
 {
      driver.Dispose();
      driver.Quit();
 }