Tuesday, 13 May 2014

Limitation of Using @FindBy Annotation In PageFactory And How to overcome it

Example Of PageFactory:
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.PageFactory

////PageLogin.java
public class PageLogin
{
   @FindBy(id="userName")
   private WebElement PageLogin_txtUserName;
   @FindBy(id="password")
   private WebElement PageLogin_txtPassword;
   @FindBy(id="btnSubmit")
   private WebElement PageLogin_btnSubmit;

 //instance initialization block
{
//pseudo singleton method to retrieve WebDriver Instance
WebDriver wd=WebDriverInstance.getWebDriverInstance();
wd.get("http://localhost:8080/Project/login.html);
PageFactory.initElements(wd,this);
}
//instance initialization block ends
public String doLogin(String userName,String password)
{
   PageLogin_txtUserName.sendKeys(userName);
   PageLogin_txtPassword.sendKeys(password);
   PageLogin_btnSubmit.click();

   return WebDriverInstance.getWebDriverInstance().getTitle();

}

}
//class definition PageLogin.java ends
////testLogin.java
PageLogin login=new PageLogin();
login.doLogin("A","B");  //#1
login.doLogin("C","D"); //#2
In step #1 and #2, driver will look for userName and password every time on page, when ever we call login.doLogin, which will degrade the performance.
The solution should be once userName/password is found/located, it should be stored in memory.
PageFactory provides this solution. @CacheLookup is solution.
So, above code can be enhanced to
@FindBy(id="userName")
@CacheLookup
   private WebElement PageLogin_txtUserName;
@FindBy(id="password")
@CacheLookup
   private WebElement PageLogin_txtPassword;
@FindBy(id="btnSubmit")
@CacheLookup
   private WebElement PageLogin_btnSubmit;

This will result locators(Referred WebElements) to be cached.

Sunday, 11 May 2014

What is difference between textToBePresentInElement(locator,String) vs textToBePresentInElementValue(locator,String) in WebDriver

WebDriver provides implicit wait and explicit wait. In explicit wait also, we can use predefined wait conditions and we can create our own conditions using anonymous class and implementing apply method.

So, now  textToBePresentInElement(locator,String) and textToBePresentInElementValue(locator,String) are predefined by ExpectedCondition which is used to set explicit wait.

So, we can use :
WebDriverWait wait=new WebDriverWait(driver,10);
//create WebDriverWait instance with parameters passed - 1. WebDriver instance, 2. maximum wait in case ExpectedCondition fails.
wait.until(ExpectedCondition.textToBePresentInElement(By,"Looking4TextOnElement");//This verifies if text is present on element By.
wait.until(ExpectedCondition.textToBePresentInElementValue(By,"Looking4TextOnValueAttribute");//This verifies if text is present in value attribute of element By.

How To Call JMeter Using Ant

Custom task to be created using taskdef. taskdef is meant to add custom tasks which in not inbuilt provided by ant. The tool has to provide class with task which can be integrated with ant.

<taskdef
    name="jmeter"
    classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>

Once task in name of jmeter is created which is mapped to class name "org.programmerplanet.ant.taskdefs.jmeter.JMeterTask", then jmeter can be used with ant.

To execute jmeter script "jmx" file and specify output log "jtl" file and to pass other arguments to jmeter, we can use following approach.

<jmeter
    jmeterhome="c:\jakarta-jmeter-1.8.1"
    testplan="${basedir}/loadtests/JMeterLoadTest.jmx"
    resultlog="${basedir}/loadtests/JMeterResults.jtl">
    <jvmarg value="-Xincgc"/>
    <jvmarg value="-Xmx128m"/>
    <jvmarg value="-Dproperty=value"/>

</jmeter>