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.
cleanuseless is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.