0

I am using Faker class to create random values in Selenium Webdriver automation. Below is the class for your reference.

public class NewUserFormAction {

    Faker faker = new Faker();
    public  String name = faker.name().firstName();
    public  String Surname = faker.name().lastName();

    public static void Execute(WebDriver driver,List<HashMap<String,String>> map) throws Exception{

        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.elementToBeClickable(NewUserFormPage.gender));
        NewUserFormPage.gender.click();
        NewUserFormPage.customer_firstname.sendKeys(name);
        NewUserFormPage.customer_lastname.sendKeys(Surname);

    }
}

when I am executing this code I am getting exception at compilation at below steps with error can not make static method reference to non static data member.

NewUserFormPage.customer_firstname.sendKeys(name);
NewUserFormPage.customer_lastname.sendKeys(Surname);

Here I understand the faker instance is create on public class which is non static and it is being called in Execute method which is static. Can you please help me how to solve this issue ?

1 Answer 1

0

You are referencing non-static fields (name and surename) from a static context (execute method), which doesn't work. Have a look at e.g. this question on Stack Overflow.

To solve this problem, you can either make the fields static too or create a NewUserFormAction object. Personally, I would prefer the latter. Check out this code, where I also applied some conventions:

public class NewUserFormAction {

    private Faker faker = new Faker();
    private String firstName = faker.name().firstName();
    private String lastName = faker.name().lastName();

    public static void execute(WebDriver driver, List<Map<String,String>> map) throws Exception{
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(NewUserFormPage.gender));

        NewUserFormAction action = new NewUserFormAction();

        NewUserFormPage.gender.click();
        NewUserFormPage.customerFirstName.sendKeys(action.firstName);
        NewUserFormPage.customerLastName.sendKeys(action.lastName);
    }
}
2
  • Thanks for the reply.. 1. Already tried making field static but it was causing an issue. when we are executing this action class in a series of steps since Faker class in the API itself is mentioned as Static. I liked the second approach implemented the same. Thanks for your help Mar 6, 2020 at 10:19
  • @HemantVarhekar if the answer solved your problem, could you please accept it? Otherwise, feel free to ask for clarification.
    – beatngu13
    Mar 6, 2020 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.