Wednesday, August 17, 2011

Native android app automation – Nativedriver vs Robonium

Nowadays android is the latest thing in the market and many of the companies are working on android app development. With development comes the requirement to test an application too. As we all tester know that automation is one of the important part of testing, and is mainly used to reduce the time taken to execute the testcases. This gives time to testers to concentrate more on exploratory testing. Recently I was going through some group, and found out about some open-source frameworks that makes it possible to automate an Android native application. I went through two of such frameworks and will write about what I felt about them. The two frameworks I am going to talk about is Nativedriver and Robonium. 

Nativerdriver- Is an opensource framework written over Selenium-2.0 Webdriver implementation. As we all know that selenium is a great functional automation tool for web-applications and gives you a cross-browser and cross-platform supports. The support for testing native application has enhanced the value of selenium. Nativedriver can be used when you are actually working along with the development team and has access to the source code of the andorid application that needs to be tested. 

Robonium – Is also a great tool and is written by extending the Internal Android testing framework. This framwork supports a lot of functions which can be used to auotmate your application test. The main advantage of this framework is that , you can automate any application using this without actully having the source code of the app.

Advantages of Nativedriver: 
  • Built over the selenium webdriver hence can be easliy used by existing selenium users.
  • Test are run as a normal Java unit test. And hence other frameworks can also be used for test-case execution or reporting.
  • Ant or maven can also be used for execution of cases.
  • Fast execution and reporting of testing results
Disadvantages of Nativedriver:
  • Need access to the source code of the application that needs to be tested.
  • Sometime difficult to find element as support for finding elements by index is not available

Advantages of Robonium: 
  • Don't require the source code of the application for testing.
  • Elements are easy to identify and work on.
  • Tests are simple to write.
  • Setup is simple.
Disadvantages of Robonium
  • Can only be executed suing eclipse.
  • Slow execution.
  • Typing a normal text is not supported at this point.

Monday, August 8, 2011

Scrolling on pages using Selenium

Somebody recently asked me on how to scroll a page which is dynamically loaded. Something like the Facebook page where the updates gets loaded as you scroll down the page. As I never had such a requirement , I didn't knew a solution to it. 
While searching I found people suggesting the "selenium.focus" function to be used as a solution, but that does not solve the problem I am talking about.  The "selenium.focus" should be used when the element you are looking for gets loaded when the page gets loaded and not when you scroll through the page.
After lot of searching I was unable to get a proper answer to my problem and finally got an idea for implementing it. Its just a 4 to 5 lines of code that can be used in your code for scrolling on the page.
Following is the code:

for (int second = 0;; second++) {
			if(second >=60){
				break;
			}
				driver.executeScript("window.scrollBy(0,200)", "");
				Thread.sleep(1000);
			}

The above code uses the JavaScript method "scrollBy" to scroll on the page. The for loop runs for 60 sec  and calls the scrollBy function every second. This makes the selenium to scroll on the page.
If you have a test where you need to scroll on the page and check whether an element is loaded dynamically or not you can put a isElementPresent like function after the "driver.executeScript" to check visibility of your element.
Following is a test method written in webdriver that you can use to test the above function on the Facebook page:

public class FacebookScrollWebDriver {
RemoteWebDriver driver;
@Before
public void startup(){
	DesiredCapabilities cap=new DesiredCapabilities();
	cap.setJavascriptEnabled(true);
	cap.setBrowserName("firefox");
	try {
		driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
	} catch (MalformedURLException e) {
		e.printStackTrace();
	}
}

@Test
	public void testScroll() throws InterruptedException{
        driver.get("http://www.facebook.com");
        driver.findElement(By.id("email")).click();
        driver.findElement(By.id("email")).clear();
        driver.findElement(By.id("email")).sendKeys("facebook id");
        driver.findElement(By.id("pass")).clear();
        driver.findElement(By.id("pass")).sendKeys("facebook pswd");
        driver.findElement(By.xpath("//input[@type='submit']")).click();
        Thread.sleep(5000);
        driver.switchTo().defaultContent();
//Following is the code that scrolls through the page
        for (int second = 0;; second++) {
            if(second >=60){
                break;
            }
                driver.executeScript("window.scrollBy(0,200)", "");
                Thread.sleep(3000);
            }
}
@After
public void stop(){
	driver.quit();
}
public boolean isElementPresent(RemoteWebDriver driver, By by){
	try{
		driver.findElement(by);
		return true;
	}catch(NoSuchElementException e){
		return false;
	}
}
}

The following code is written for Selenium-1.0 users:
public class FaceScrollSeleniumOne {

	Selenium selenium;
	@Before
	public void startup(){
		selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.facebook.com");
		selenium.start();
	}

	@Test
		public void testScroll() throws InterruptedException{
	        
		selenium.open("/");
		selenium.type("id=email", "facebook id");
		selenium.type("id=pass", "facebook pswd");
		selenium.click("//input[@type='submit']");
	        Thread.sleep(5000);
	//Following is the code that scrolls through the page
	        for (int second = 0;; second++) {
	            if(second >=60){
	                break;
	            }
	            selenium.getEval("window.scrollBy(0,200)");
	               
	                Thread.sleep(3000);
	            }
	}
	@After
	public void stop(){
		selenium.stop();
	}

}