0

In my JAVA+cucumber framework I have the flexibility to change the base URL by passing the parameter -Dwebdriver.base.url= in CLI. I want similar flexibility to set @DefaultUrl also.

To achieve this - I will be using a properties file, if this property file is present use it else use default ones.

But question is: How to pass URL from properties file in @DefaultUrl annotation? Page class which is extending base class doesnt understand the properties variable.

I wanted to do it like :

@DefaultUrl(properties.getProperty("homepage"))
public class homePage extends BasePage {
// TO DO 
}

E.g.

@DefaultUrl("http://jira.mycompany.org")
@NamedUrls(
  {
    @NamedUrl(name = "open.issue", url = "http://jira.mycompany.org/issues/{1}")
  }
)
public class JiraIssuePage extends PageObject {
    ...
}

I have below function in base class to read properties file:

public static String value(String propKey) {
    if (prop == null) {
        prop = new Properties();
        try {
            FileInputStream fis = new FileInputStream(
                    System.getProperty("user.dir") + "properties.properties");
            properties.load(fis);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String pvalue = properties.getProperty(propKey);
    if (properties.containsKey(propKey) == false) {
        logger("key not correct")
    } else {
        return pvalue ;
    }
    return pvalue;
}
paul
  • 4,333
  • 16
  • 71
  • 144
  • The value passed must be a compile time constant. Refer this - https://stackoverflow.com/questions/1458535/which-types-can-be-used-for-java-annotation-members – Grasshopper Jan 24 '19 at 10:25
  • I knew it, but wasnt 100% sure. Is their any other way I can achieve what I want? Want to make page Urls dynamic. – paul Jan 24 '19 at 10:53
  • U can use the annotation as a marker. Use reflection api calls check if annotation is present then access the properties file to get the url etc. this cld be in the constructor. – Grasshopper Jan 24 '19 at 12:26

0 Answers0