Thursday 12 April 2012

Maximize browser window in Selenium Web Driver

From Selenium 2.21, we can now maximize the browser window, similar to Selenium RC.

 Direct way:

public static void main(String[] args) {
  WebDriver d = new FirefoxDriver();
  d.get("http://www.google.com");
  d.manage().window().maximize();
 
  System.out.println(d.getTitle());
  d.quit();
 }

Indirect way:

we can also maximize the browser window usingt the toolkit utility which query the native operating system directly and is platform independent. In addition to this, the code below will maximize the browser window according to your system's current resolution.

driver.manage().window().setPosition(new Point(0,0));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setSize(dim);
Use the above script in your test setup method where you initiate the browser to get a fully maximized brwoser window that fits your monitor's screen size....
 

Tuesday 10 April 2012

How to handle frame pop ups using Selenium Webdriver

Frames are not exactly treated as pop ups or external windows in Selenium Web driver. There will be a frame id for each frame. We have to invoke this particular frame id to handle the actions related to frames when automationg using selenium 2.0

See the example provided below:

driver.switchTo().frame("companyFlyoutIFrame");  

where "companyFlyoutIFrame" is the id of the frame. To obtain the id, inspect the frame object using the firebug.

The above line is the script used to switch the control to the frame. once the control is switched, you can do the actions on the frame as normal selenium commands.

Once you are done with the frame, it is also important to switch the control back to the main window. To do this you can use the following script:

driver.switchTo().defaultContent();


willl switch the controll back to the main window by closing the frame.

Hope it is informative....
 

Wednesday 4 April 2012

Finding the Class name from Xpath of an Element in Selenium

Sometimes, we may require to find the Class name of an element whoes xpath we know.
We can use the "getAttribute()"  method of Selenium to handle this situvation.

consider the example below:

String Name=webDriver.findElement(By.xpath(//div[@class='hotelRoomCard'][5]/span[2]/button).getAttribute("class");
System.out.println("The class name is:"+Name);

The output will geive you the class name.
This in turn can be used to identify the object with a desired class name.
Hope it was helpfull.... :)