Wednesday, September 26, 2012

How to maximize window of the browser?


How to maximize window of the browser?

We can maximize browser window in multiple ways...

By Using WebDriverBackedSelenium, by using Robot class and by webdriver.

1. WebDriverBackedSelenium:
        The very first method which is given in their documentation is using windowMaximize() command of selenium instance.

selenium = new WebDriverBackedSelenium(driver, url);


selenium.windowMaximize();

2. Using Robot class:  We can use robot class to invoke keyboard action to maximize browser window.

Robot robot = new Robot();
// Press ALT and SPACE Keys

robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
Thread.sleep(1000);
//Press down arrow keys to move to select Maximize option in menu

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(100);
//Press enter to invoke the Maximize menu option

robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}

3. Using WebDriver and dimensions:

public static void maximizeWindow(){

        Toolkit t = Toolkit.getDefaultToolkit();
        org.openqa.selenium.Dimension screenResolution = new   org.openqa.selenium.Dimension((int)t.getScreenSize().getWidth(), (int)t.getScreenSize().getHeight());

       driver.manage().window().setSize(screenResolution);
}



4. WebDriver:

driver.manage().window().maximize();

























Tuesday, September 18, 2012

What is Defect Density?


DEFINITION
Defect Density is the number of confirmed defects detected in software/component during a defined period of development/operation divided by the size of the software/component.

ELABORATION
The ‘defects’ are:
  • confirmed and agreed upon (not just reported).
  • Dropped defects are not counted.
The period might be for one of the following:
  • for a duration (say, the first month, the quarter, or the year).
  • for each phase of the software life cycle.
  • for the whole of the software life cycle.
The size is measured in one of the following:
  • Function Points (FP)
  • Source Lines of Code
DEFECT DENSITY FORMULA











USES
  • For comparing the relative number of defects in various software components so that high-risk components can be identified and resources focused towards them.
  • For comparing software/products so that quality of each software/product can be quantified and resources focused towards those with low quality.

Sunday, September 16, 2012

How to pass parameters in selenium RC using TestNG

How to pass parameters in selenium RC using TestNG


We can parametrize our test cases using TestNG in Selenium RC in two ways

Pass the data from testng.xml file...

1. using @parameters

public class first_class {
    private Selenium selenium;
    public String url = "http://google.com/";   

    @Parameter ({ "url" })
    @Test   
    public void Test(String url) throws MalformedURLException { 
        System.out.println("In Test Methos");
        selenium.open(url); 
        System.out.println(selenium.getTitle() + "in in Test Method");
        selenium.close();
    }
    }


2. using @DataProvider


import org.openqa.selenium.server.RemoteControlConfiguration;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.*;
import org.testng.annotations.*;

import java.io.*;
import java.sql.*;



public class TestExcel extends SeleneseTestBase {

@DataProvider(name="DP1")
public Object[][] createData(){
Object[][] retObjArr = {{"testuser1","password1"},
{"testuser2","password2"},
{"testuser3","password3"},
{"testuser4","password4"},
{"testuser5","password5"},
};
return(retObjArr);
}


private SeleniumServer seleniumServer;
Selenium selenium;

@BeforeClass
public void setUp()throws Exception{

RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.trustAllSSLCertificates();
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.yahoo.com");
seleniumServer.start();
selenium.start();
}

@Test (dataProvider = "DP1")
public void testEmployeeData(String username, String password){
selenium.open("https://login.yahoo.com/config/mail?.src=ym&.intl=us/");
selenium.type("username", username);
selenium.type("passwd",password);
selenium.click(".save");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Hi,"+username));
selenium.click("_test_sign_out");
selenium.waitForPageToLoad("30000");

}
@AfterTest
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumServer.stop();

}

Saturday, September 15, 2012

How to take screenshot of a page using webdriver?

How to take screenshot of a page using webdriver?

 Here is a sample code to get the screenshot of the current browser window using webdriver ...

The file name with the specific path should mentioned. Either it should be dynamic or we can hard-code the file name ( every time we run this code, the file will be overriden).


    public static void snapshot() throws IOException{
       
        File fi = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(fi, new File("file name with the path should be mentioned here... "));
    }

It is very simple and we call this method when ever we need (Pass or Fail cases) to take the screenshots of the pages.

Hope this is helpful.. :)

How to maximize window size of current window using webdriver?

How to maximize window size of current window  using webdriver?

Generally when we start the browsers, it will open based on its previous state. Some times it is customized to certain size (width and height) manually. So in order to make the browser opens in a maximized window, we need to use Toolkit class.

Maximize window option will allows the user to see the complete application under test.

Here is the snippet to achieve the maximized window using Webdriver...

Here driver is WebDriver object for Firefox or IE driver.

WebDriver driver = new Firefoxdriver();

public static void maximizeWindow(){
        Toolkit t = Toolkit.getDefaultToolkit();
        org.openqa.selenium.Dimension screenResolution = new org.openqa.selenium.Dimension((int)t.getScreenSize().getWidth(), (int)t.getScreenSize().getHeight());
        driver.manage().window().setSize(screenResolution);
   
    }

Saturday, September 1, 2012

How to drag and drop elements using webdriver?

How to drag and drop elements using webdriver?
 
If we want to drag and drop from one element to other element.

Below is the small snippet code....

WebDriver driver  = new FirefoxDriver();

driver.get("URL");

Actions builder = new Actions(driver);

builder.clickAndHold().moveToElement(ele).release().build().perform();

How to mouse hover using WebDriver Backed selenium?

How to mouse hover using WebDriver Backed selenium?

Create a webdriverbackedselenium object and mouse move to the specific id and click on the on any specific id.

Below is the small snippet code for mouse move or mouse hover using webdriver backed selenium.


Using WebDriverBacked Selenium:

        WebDriverBackedSelenium seleniumDriver = new WebDriverBackedSelenium(driver,"URL");
       
        seleniumDriver.mouseMove("Specify id ");
        seleniumDriver.click("Specify id");


how to mouse hover on any element using webdriver

how to mouse hover on any element using webdriver?

Mouse over using WebDriver little different. And also the given solution may not work with the latest versions of FF 12+. We need to wait for the latest version of the selenium jar.


//driver = new InternetExplorerDriver();
// driver = new FirefoxDriver();
driver.get("URL");


Using Actions:

        Actions builder = new Actions(driver);
        WebElement ele = driver.findElement(By.xpath("Specify xpath"));
        builder.moveToElement(ele).build().perform();
        WebElement ele2 = driver.findElement(By.xpath("Specify xpath"));
        ele2.click();