将新对象添加到List,然后新存储的对象将被新存储的对象覆盖

时间:2013-10-19 15:51:47

标签: c#

我想请一些帮助!我使用WebDriver编写自动化测试,我有下面的代码。此静态方法的目的是从页面的certian部分收集对象。

正如您所看到的,我使用for循环来修改定位器值,并测试定位器是否显示。如果显示,则启动MenuGroupObject对象,然后将其放入List对象中,该对象是最后的返回值。

请查看代码段下方的输出结果。

public static List<MenuGroupPageObject> GetListOfMenuGroups(IWebDriver webDriver)
    {
        var listOfMenuGroupPageObjects = new List<MenuGroupPageObject>();

        var menuGroupLocatorPattern = MenuBarLocatorHelper.GetMenuBarRootLocator() +
                                      MenuBarLocatorHelper.GetLocatorPatternForMenuGroups();

        for (var i = 1; i < 20; i++)
        {
            var realMenuGroupNameLocator = menuGroupLocatorPattern.Replace(
                MenuBarLocatorHelper.GetThePureMenuGroupLocatorPatternForMenuGroups(), i.ToString(CultureInfo.InvariantCulture));

            if(SafeElementHandler.IsElementPresent(webDriver,By.XPath(realMenuGroupNameLocator)))
            {
                var menuGroupObject = new MenuGroupPageObject(webDriver, realMenuGroupNameLocator);
                listOfMenuGroupPageObjects.Add(menuGroupObject);

                foreach (MenuGroupPageObject listOfMenuGroupPageObject in listOfMenuGroupPageObjects)
                {
                    Console.WriteLine("mainlocators in the bag round: " + i + "... " + listOfMenuGroupPageObject.GetMainLocator());
                }
            }
        }

        return listOfMenuGroupPageObjects;
    }

所以,结果就是这样:

    mainlocators in the bag round: 1... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[1]

mainlocators in the bag round: 2... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[2]
mainlocators in the bag round: 2... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[2]

mainlocators in the bag round: 3... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[3]
mainlocators in the bag round: 3... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[3]
mainlocators in the bag round: 3... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[3]

mainlocators in the bag round: 4... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[4]
mainlocators in the bag round: 4... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[4]
mainlocators in the bag round: 4... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[4]
mainlocators in the bag round: 4... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[4]

mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[5]
mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[5]
mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[5]
mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[5]
mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[5]

您应该注意的是,在每一轮中,已存储的对象将被最新的覆盖。 但是,它应该是这样的:

    mainlocators in the bag round: 1... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[1]

mainlocators in the bag round: 2... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[1]
mainlocators in the bag round: 2... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[2]

mainlocators in the bag round: 3... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[1]
mainlocators in the bag round: 3... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[2]
mainlocators in the bag round: 3... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[3]

mainlocators in the bag round: 4... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[1]
mainlocators in the bag round: 4... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[2]
mainlocators in the bag round: 4... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[3]
mainlocators in the bag round: 4... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[4]

mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[1]
mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[2]
mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[3]
mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[4]
mainlocators in the bag round: 5... //div[@id='contentContainer']/div[@id='container']/div[@id='leftHandMenubar']/ul[5]

我的问题是,出了什么问题?我检查了List&lt;&gt;在c#中我没有发现任何东西,如果它缺少(缺少构造函数参数,或类似)会导致这样的事情。

1 个答案:

答案 0 :(得分:0)

正如我在评论中所写,MenuPageObject对象有一个静态字段。