0

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

如您所见,我使用 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]

我的问题是,有什么问题?我检查了 C# 中的 List<> ,但没有发现任何缺少的东西(缺少构造函数参数或类似参数)会导致这样的事情。

4

1 回答 1

0

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

于 2015-04-22T05:12:28.737 回答