Thursday, January 12, 2012

FirefoxDriver with custom profile

Use profile manager to create profiles. In Windows machine profiles will be stored in the path

C:\Documents and Settings\user\Application Data\Mozilla\Firefox\Profiles\

FirefoxProfile profile = new ProfilesIni().getProfile("profileName");
FirefoxDriver driver = new FirefoxDriver(profile);

Once firefox window opens check that addon's, bookmark etc are appearing in the newly opened firefox window.

Simulate click using javascript for webdriver

        driver.get("http://www.google.com");
        JavascriptExecutor js =  (JavascriptExecutor)driver;

        WebElement upload = driver.findElement(By.name("btnI"));
        ((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);

By pass certificate validation failure in IE for https pages

if (driver instanceof InternetExplorerDriver)
    driver.navigate().to("javascript:document.getElementById('overridelink').click()");

Wednesday, January 4, 2012

ISuiteListener and ITestListener

Sample usage

public class TestReporter implements ITestListener {

    static int count;

    public void onFinish(ITestContext arg0) {

        System.out.println("Test Finished !!!!!!!!");
    }

    public void onStart(ITestContext arg0) {
        ISuite suite = arg0.getSuite();
        System.out.println("Driver is : ");
        ITestNGMethod[] value = arg0.getAllTestMethods();
        System.out.println("Method Number : "+value.length);
        count = value.length;

    }

    @Override
    public void onTestFailure(ITestResult arg0) {
//    System.out.println("Finished Executing Test: "+arg0.getName()+" Status: Failed"+"\n Reason:"+arg0.getThrowable());
    }

    @Override
    public void onTestSkipped(ITestResult arg0) {
//    System.out.println("Skipped test: "+arg0.getName()+".Reason"+arg0.getThrowable());
    }

    @Override
    public void onTestStart(ITestResult arg0) {
//    System.out.println("Starting Test: "+arg0.getName());
//    System.out.println("\n Starting Test: "+count--);

    }

    @Override
    public void onTestSuccess(ITestResult arg0) {
    System.out.println("Finished Executing Test: "+arg0.getName()+" Status: Success.");
    System.out.println("Class Name : "+arg0.getTestClass());

    Object[] ab = arg0.getParameters();
    System.out.println("Param Set : "+ab.length);
    for(int i = 0 ; i < ab.length ; i++)
        System.out.print(ab[i]+" ");

//    System.out.println("Method Name : "+arg0.getMethod().getMethodName());
    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
//    System.out.println("Test failed but within success percentage");
    }
}


public class SuiteReporter implements ISuiteListener {

    @Override
    public void onFinish(ISuite arg0) {
    System.out.println("Finished executing the suite");
    System.out.println("********Results*******");

    }

    @Override
    public void onStart(ISuite arg0) {
    System.out.println("Starting Execution");
    //Print suiteName
    System.out.println("Suite Name:"+arg0.getName());
    //Print HostName
//    System.out.println("Host Name:"+arg0.getHost());
    //Returns null if it runs locally

    }
}





 <suite name="Sample Suite">
<!--
    <listeners>
    <listener class-name="com.clearspring.qa.tellurium.SuiteReporter" />
    <listener class-name="com.clearspring.qa.tellurium.TestReporter" />
   </listeners>
<test>
</test>
</suite>

Saturday, December 17, 2011

Clear Browser Cache

We can press Control + F5 using webdriver to clear the browser cache.

            Actions actionObject = new Actions(driver);
            actionObject.keyDown(Keys.CONTROL).sendKeys(Keys.F5).keyUp(Keys.CONTROL).perform();

Disabling caching in ChromeDriver

For disabling caching in ChromeDriver we can use the chrome switches with  DesiredCapabilities

DesiredCapabilities dc = DesiredCapabilities.chrome();
                ArrayList<String> switches = new ArrayList<String>();
                switches.add("--proxy-server="+InetAddress.getLocalHost().getHostAddress()+ ":" + Integer.parseInt(port));
                switches.add("--proxy-bypass-list=local.addthis.com");
                switches.add("--disable-application-cache");
                dc.setCapability("chrome.switches", switches);

More details about chrome switches

Maven ant run plugin

Simulate all the tasks of ant script and also used to call external ant targets from a maven pom.


<groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <configuration>
      <tasks>
        <echo message="hello ant, from Maven!" />
        <echo>Maybe this will work?</echo>

        <ant antfile="${basedir}/build.xml">
          <target name="test" />
        </ant>

       <exec dir="${basedir}" executable="cmd"
         failonerror="true">
         <arg line="/K start" />
         <arg line="one.bat" />
       </exec>

      </tasks>
    </configuration>
   </plugin>
Sample Usage : mvn antrun:run