动态 id 的 PageObject 模型?C#

想知道是否可以为对象分配一个动态的 id?


下面的示例代码:


    int index = 0;

    foreach (var row in rows)

    {


        if (randNumber == row.FindElement(By.Id($"r-number-{index}")).Text)

        {

            row.FindElement(By.Id($"view-record-{index}")).Click();

            return;

        }

        index++;

    }

By.Id($"r-number-{index}")).Text在多个地方和多个规范流程步骤中使用,因此能够将其转换为页面对象会很棒。


RandPage.RandNumber+index+简单来说,我最需要的是什么。


有人知道任何可能的方法吗?


梦里花落0921
浏览 96回答 2
2回答

大话西游666

你可以试试这样的。private IList<IWebElement> ListOfElements => Driver.FindElements(By.XPath("//p[contains(@id,'r-number-')]"));//this finds all the p elements on the page with an id that contains r-number-&nbsp; &nbsp; public void ClickEachOne()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // click each element in that list&nbsp; &nbsp; &nbsp; &nbsp; foreach (var element in ListOfElements)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element.Click();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

哈士奇WWW

我建议在 PageObject 构造函数中添加参数,这将是索引。然后您可以为该索引创建页面并使用它。例如:public class MyPage&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private int _index = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public MyPage(int index)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_index = index;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public MyMethod()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do something with the _index here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP