2

I have written 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 generic function?

  • Could you please explain your question? what exactly you want? You have to use Log4j in your script right? – Bharat Mane Jun 10 '16 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 '16 at 9:34
  • How ???????????????? – paul Jul 9 '16 at 14:33
0

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, privacy policy and cookie policy

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