4

I have written a selenium framework using page object design pattern. To make it better I am also using PageFactory. Example:

BasePage.java

@FindBy(xpath= FinalLocators.userName)
WebElement uname;

@FindBy(xpath= FinalLocators.userpass)
WebElement userpwd;

sendkeys.uname("johndoe");
sendkeys.userpwd("kitty");

FinalLocators.java

public static final String userName=//input[text()='username']
public static final String userpass=//input[text()='password']

Now, I want loggers (log4j) to give me:

Element uname not found. I want the WebElement name to be in logs not the xpath declared in FinalLocators.

Edit: In non PageFactory projects it is written something like this:

public void sendData(String xpathKey){
    try{            
    driver.findElement(By.xpath(OR.getProperty(xpathKey))).sendKeys("abc");
    logger.info("Found "+xpathKey);
    }catch(Exception e){
    logger.info("wait started for "+xpathKey);

            }
    
}

But in PageFactory based framework how to write such a generic function?

3
  • Could you please explain your question? what exactly you want? You have to use Log4j in your script right? Jun 10, 2016 at 13:49
  • You would need to implement your own PageFactory with your own implementation of FieldDecorator interface that decorates original DefaultFieldDecorator: delegates calles to it and logs possible problems.
    – dzieciou
    Jul 9, 2016 at 9:34
  • How ????????????????
    – paul
    Jul 9, 2016 at 14:33

1 Answer 1

1

Totally agree with you @dzieciou. I have experienced that, delegates might be the culprit.

It would be great if one could suggest us the actual solution for generic logging with BasePage.java and FinalLocators.java

@paul - Below middle way/workaround I found and working well for me:

  • Remove or stop using FinalLocators.java [ explanation:Something is not working fine with using both FieldDacorator and FinalLocators ]
  • Instead of initializing final string under FinalLocators.java, shift those directly in BasePage.java

  • Basic code snippet

@FindBy(xpath="//input[text()='username']")

WebElement uname;

@FindBy(xpath="//input[text()='password']")

WebElement userpwd;

// Here you can directly get access to your actual defined Element under own PageFactory

sendkeys.uname("johndoe");

sendkeys.userpwd("kitty");

Using this I am able to fine Element uname in log. Give a Try !

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.