将新对象添加到列表中,则已存储的对象将被新对象覆盖
本文关键字:对象 新对象 存储 覆盖 列表 添加 | 更新日期: 2023-09-27 18:36:00
我想请一些帮助!我使用 WebDriver 编写自动化测试,我有下面的代码。此静态方法的目的是从页面的某个部分收集对象。
如您所见,我使用 for 循环来修改定位器值,并测试定位器是否显示。如果显示它,那么我启动 MenuGroupObject 对象并将其放入一个列表对象中,该对象是最后的返回值。
请查看代码片段下方的输出结果。
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# 中检查了列表<>我没有发现任何如果缺少(缺少构造函数参数等)会导致这样的事情
正如我在评论中所写,MenuPageObject 对象有一个静态字段。