Selenium-Java: how can I specify page locators dynamically?

First of all; apologies if some of the code-related language I use below doesn’t make a lot of sense as I am relatively new to Java.

I have a Java class that is used to generate random test data, and have recently added a new method generate a certain amount of characters using the Java Faker class. This is to be used initially for asserting error messages where the amount of characters entered is greater than the character limit.

public class GenerateTestData() {

    public static String charactersToBeEntered;
    
    public String characterLimitValidation(int numberOfCharacters) {
    charactersToBeEntered = faker.lorem().characters(numberOfCharacters);
    return charactersToBeEntered;
    }

}

I have a method inside another class for a single webpage which calls this method, however I’d like to move it out into a “common” class so that it can be used by other pages. I’ll call this class FirstPage for the purpose of this post.

public class FirstPage {

    GenerateTestData generateTestData;
    
    static by FIRST_PAGE_INPUT = new By.ById("firstPageLocator");
    
    public void exceedCharacterLimit() {
    $(FirstPage.FIRST_PAGE_INPUT).sendKeys(generateTestData.characterLimitValidation(301));
    }

}

This is used by the FirstPageStepDefinitions class to create the Cucumber step:

public class FirstPageStepDef {

    @When("I exceed the character limit")
    public void iExceedTheCharacterLimit() {
        firstPage.exceedCharacterLimit();
    }

}

The above code works for that lone page, with the test passing since the error message I’ve specified on the feature file is displayed when more than 300 characters is submitted. However, I would like to make the method on the FirstPage class re-usable so that I can call it from other pages in the codebase, i.e. the SecondPage and ThirdPage classes.

As mentioned above, I would ideally like to move that method, and the corresponding step definitions step into classes called CommonPage and CommonStepDef; however, I’m having trouble re-formatting the exceedCharacterLimit method on the CommonPage class (previously on the FirstPage class) so that it allows a dynamic page locator to be provided from the FirstPage, SecondPage and ThirdPage classes.

Essentially, I just need to understand how I can make the $(CommonPage.COMMON_PAGE_INPUT) snippet below dynamic to achieve this:

public class CommonPage {

    GenerateTestData generateTestData;
    
    public void exceedCharacterLimit(int numberOfCharacters) {
    $(CommonPage.COMMON_PAGE_INPUT)
    .sendKeys(generateTestData.characterLimitValidation(numberOfCharacters));
    }

}

I’m guessing it would be something like adding a String called pageLocator to the first line of the method, and declare this afterwards? – i.e.:

public class CommonPage {

    GenerateTestData generateTestData;
    
    public void exceedCharacterLimit(String pageLocator, int numberOfCharacters) {
    String pageLocator = ?
    pageLocator.sendKeys(generateTestData.characterLimitValidation(numberOfCharacters));
    }

}

The only change I’ve made to the method so far is that I’ve replaced the numberOfCharacters value with an int that allows me to specify this for any of the pages that will be using this method, though please do let me know if this doesn’t look correct.

Thank you for any support in advance.

New contributor

cleanuseless is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật