Import user defined class files


#1

Hello:
I am attempting to create a helper class where I can define specific user defined methods for use in my tests. I am having trouble getting the class file to import. So here is my structure:
In src.test.resources, I have created a class named HelperMethods.java and created a public static void method in this file. I then try to import my HelperMethods file into my test file and by using import test.resources.HelperMethods; in my header for my individual test and it can’t resolve and I get red. However, when I go back to my file HelperMethods.java, I show that I am getting one reference that I indeed created in my test file which was HelperTest.Maximize(); (and clicking on that I see one reference in a popout window). We can’t figure out why this isn’t resolving in the import section. Can you assist?


#2

Hi @micheleaz
Seems like you are trying to import your class from outside the default (uitest) project package. This could be a bit tricky as you would have to include other package as a dependency in the pom.xml…
Best way is to move your class file to a folder inside the uitest package, (for example uitest.helpers) and then import it from there.
Regards


#3

Hi @smatijas:
Here is what I have now-- I have my class in src.test.java.uitest.Helpers.HelperMethods.java. I can’t get driver to resolve inside of HelperMethods.java and I can’t get the import to work inside of a test file. Please see screen shots. Thanks!
Structure1


#4

Hello @micheleaz!
Here you have the wrong package declaration(uitest.test) for the file that is located in the uitest.Helpers folder in your case. So you would have to change the declaration to package uitest.Helpers;
If you click on the red underlined uitest.test on line1, there should be a light-bulb icon that will automatically suggest you this quick fix, and if not, you can just change it manually.

Afterward, you have to import the selenium WebDriver and create your Maximize method. The error on line 13 is probably due the “Cannot make a static reference to the non-static field or method” so this method shouldn’t be static.

Here is an example:

package uitest.Helpers;

import org.openqa.selenium.WebDriver;

public final class HelperMethods {

    private WebDriver driver;

    public HelperMethods(WebDriver driver) {
        this.driver = driver;
    }

    public void Maximize() {
        driver.manage().window().maximize();
    }
}

Now in your test file, import the helper file, instantiate it HelperMethods help = new HelperMethods(driver); and use your Maximize() method when needed.