1
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

I have a page object model and each page object requires these import statements. It's a hectic task to write the import statement repeatedly for each class file.

Is there a way to reduce line of code and import or use the commonly used imports, by using a single line of code like:

import package.commonlyusedimports;

Were commonlyusedimports class is defined as:

package xxx;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class commonlyusedimports{

}

Note: I tried inheritance, even then parent class imports are not visible to child.

  • Did you think about using class inheritance to approach this – SlightlyKosumi Jun 15 at 6:44
  • Yes it didn't work either – PDHide Jun 15 at 6:45
  • Instead of importing commonlyusedimports , I inherited it , still the import statement of parent is not visible to child – PDHide Jun 15 at 6:46
  • 1
    That's just not how imports work in Java. If you want to do it via inheritance you have to expose the imported objects as properties of the class you're inheriting from. But this isn't really anything to do with QA. – jonrsharpe Jun 15 at 8:10
1

You can inject a factory for the behaviors you need from the packages you are importing*

public interface WebFactory {

   public Element findElement(Locator loc);
   ....
}

The imports would be interfaces as well...

public interface Element {
    public String getText();
    ...
}

And then you can implement these interfaces by biding to Selenium however you want.

public class SeleniumElement implements Element{
        public String getText() {
        ...
        }
}

And then implementing the factory:

public class SeleniumWebFactory implements WebFactory {

   public Element findElement(Locator loc) {
      ....
   }
}

In the end, you just need to inject a factory to your page objects:

@BeforeSuite
....

this.factory = new SeleniumWebFactory();

@Test()
....
Page page = new Page(this.factory)

This way, the page object can call for generic behavior such as WebFactory.findElement and Element.getText without explicitly requiring them.

* Which is a good practice, because currently you are binding all your code to Selenium. You can invert this dependency, so that Selenium would be unable to screw you.

  • More than hectic , I wanted to know whether this the right approach. Just making sure I am not making a code base that is not reusable as the code lines increases – PDHide Jul 17 at 19:04
1

It's a hectic task to write the import statement repeatedly for each class file.

Is it really hectic? This would make most developers maniacs if this was true. Learn how to use your IDE to help you speed up the adding of the correct imports.

Bulk add imports for Java: https://crunchify.com/java-simple-way-to-import-all-missing-packages-at-once/

  • More than hectic , I wanted to know whether this the right approach. Just making sure I am not making a code base that is not reusable as the code lines increases – – PDHide Jul 26 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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