简体   繁体   English

在Selenium C#中重用Page对象中的属性

[英]Reuse properties in Page Object in Selenium C#

I am considering automated testing using Selenium and C# 我正在考虑使用Selenium和C#进行自动化测试

I am using page object pattern and want to reuse element locator properties. 我正在使用页面对象模式,并且想重用元素定位器属性。 Let me explain with an example: 让我用一个例子来解释:

public class AdminSection
    {
        [FindsBy(How = How.Id, Using = "**Admincity**")]
        public IWebElement City{ get; set; }
    }

public class UserSection
    {
        [FindsBy(How = How.Id, Using = "**usercity**")]
        public IWebElement City{ get; set; }
    }

public class MyPage
    {
//Section could be either admin or User based on the page        
ISection = AdminSection OR UserSection;
    }

Here I have a page where I have a "section", I want to reuse properties like name, city, address,etc. 在这里,我有一个页面,其中有一个“部分”,我想重用诸如名称,城市,地址等属性。 However different pages have different element ids. 但是,不同的页面具有不同的元素ID。 Is there a way to reuse the properties Name & City with different element locators at runtime? 有没有一种方法可以在运行时使用不同的元素定位符重用属性Name&City? use some abstraction? 使用一些抽象?

I want to avoid the duplication I did above by creating two separate classes? 我想通过创建两个单独的类来避免上面所做的重复? I cannot use FindBy XPath as the XPath address is not the same on both pages, plus it will be brittle. 我不能使用FindBy XPath,因为两个页面上的XPath地址都不相同,而且它很脆弱。

Thanks, Manoj 谢谢,Manoj

I would suggest that you don't do this. 我建议您不要这样做。 Treat each page separately even if they have some/all of the same locators. 即使每个页面具有部分/全部相同的定位符,也要分别对待它们。 If pages do have the same locators it's probably a coincidence. 如果页面确实具有相同的定位符,则可能是巧合。 At some point, it's likely that one of the pages will change which will force you to update locators for only that page. 有时,其中一个页面可能会更改,这将迫使您仅更新该页面的定位器。 When you've shared them, you'll then have to split everything or things will get even more complicated. 共享它们后,您将必须拆分所有内容,否则事情将变得更加复杂。

You'll likely spend way more time trying to figure out a way to share them and write them in a such a way that they work on all pages than you would if you just copy/paste the ones that are the same in separate page objects. 与仅复制/粘贴在单独页面对象中相同的页面相比,您可能会花费更多的时间来尝试一种共享它们并以一种在所有页面上都能使用的方式编写它们的方式。

Manoj - Thanks for answering the questions. Manoj-感谢您回答问题。 Based on what you are saying, I might create it this way. 根据您的发言,我可能会这样创建。 It sounds like a SPA where it is a single page application? 听起来像SPA,它是单页应用程序? I would probably still create a different page object class for Admin and User and call each accordingly. 我可能仍会为Admin和User创建一个不同的页面对象类,并分别进行调用。

Then you can build out your specflow steps calling the page and method. 然后,您可以构建调用页面和方法的specflow步骤。

example: 例:

    adminPage

    [FindsBy(How = How.Id, Using = "City")]
    private readonly IWebElement _adminCity = null;

    //methods
    public void TypeAdminCity(string value)
    {
        browser.Type(_adminCity, value);
    }

    UserPage

    [FindsBy(How = How.Id, Using = "userCity")]
    private readonly IWebElement _userCity = null;

    //methods
    public void TypeUserCity(string value)
    {
        browser.Type(_userCity, value);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM