Thursday 13 June 2013

Getting total no.of Checkboxes/Textboxes/Dropdowns/iframes/Links on a web page using Webdriver



In one of the Previous Post we had details about WebDriver findElements() method. This is very useful to find the total no.of  desired elements on a webpage .

Here are some of the examples :

Getting total no.of Links on a Webpage :
Here is the sample code to get the total no.of links on facebook registration page:
  1. driver.get("http://facebook.com");  
  2. List <webelement> totalLinks=driver.findElements(By.tagName("a"));  
  3. System.out.println("total links "+totalLinks.size());  
  4. </webelement>  

Getting total no.of checkboxes on a Webpage :
Here is the sample code to get the total no.of checkboxes on facebook registration page:
  1. driver.get("http://facebook.com");  
  2. List<webelement> checkboxes=driver.findElements(By.xpath("//input[@type='checkbox']"));  
  3. System.out.println("total checkboes "+checkboxes.size());  
  4. </webelement>  



Getting total no.of dropdown menus on a Webpage :
Here is the sample code to get the total no.of dropdown menus on a facebook registration page:
  1. driver.get("http://facebook.com");  
  2. List<webelement> dropdown=driver.findElements(By.tagName("select"));  
  3. System.out.println("total dropdown lists "+dropdown.size());  
  4. </webelement>  



Getting total no.of textboxes on a Webpage :
Here is the sample code to get the total no.of textboxes on facebook registration page:
  1. driver.get("http://facebook.com");  
  2. List <webelement> textboxes=driver.findElements(By.xpath("//input[@type='text'[@class='inputtext']"));  
  3. System.out.println("total textboxes "+textboxes.size());  
  4. </webelement>  

Here is the sample code to get the total no.of textboxes on hotmail registration page:
  1. driver.get("https://signup.live.com");  
  2. List <webelement> totalTextboxes=driver.findElements(By.xpath("//input[@type='text']"));  
  3. System.out.println("total textboxes "totalTextboxes.size());  
  4. </webelement>  

Getting total no.of iframes on a Webpage :
Here is the sample code to get the total no.of iframes :
  1. driver.get("https://www.facebook.com/googlechrome/app_158587972131");  
  2. List <webelement> totaliFrames=driver.findElements(By.tagName("iframe"));  
  3. System.out.println("total links "+totaliFrames.size());  
  4. </webelement>  

Working with the different types of web elements

In this post we will see how to work with different webelemts.

By default , selenium defines predefined functions which we can use on different types of webelemts.
Here are some of the predefinied functions:

                driver.findElement(By.id("WebelemntId")).clear();
driver.findElement(By.id("WebelemntId")).isEnabled();
driver.findElement(By.id("WebelemntId")).isDisplayed();
driver.findElement(By.id("WebelemntId")).submit();
driver.findElement(By.id("WebelemntId")).sendKeys("test");
driver.findElement(By.id("WebelemntId")).isSelected();
driver.findElement(By.id("WebelemntId")).getAttribute("");
driver.findElement(By.id("WebelemntId")).getLocation();
driver.findElement(By.id("WebelemntId")).getTagName();
driver.findElement(By.id("WebelemntId")).getText();
driver.findElement(By.id("WebelemntId")).getSize();

All the functions canot be used for every webelement.

Say  "sendKeys()" method is sending text to a webelement .This method can be used with textboxes but we can't use it on images , links. These are all basic things which we will learn through experience.

Here are the samples , on how to achieve basic functionality of different webelements using webdriver functions:

Textboxes :Send text
Sending text to Textboxes can be done by using "sendKeys()" method. Here is how it works:
  1. driver.findElement(By.id("textBoxId")).sendKeys("sending text");  
RadioButtons :Select an option
Selecting an option from Radio button can be done by using "click()" method.Here is how it works:
  1. driver.findElement(By.id("radioButtonId")).click();  
Hyperlinks :Click on links
Clicking on link can be done by using "click()" method.Here is how it works:
  1. driver.findElement(By.id("linkId")).click();  
Checkboxes :Check the checkboxes
Selecting options from Checkboxes can be done by using "click()" method.Here is how it works:
  1. driver.findElement(By.id("checkBoxId")).click();  
Drop-down List :Select an option
Selecting an option from Dropdown list  can be done by using  "sendKeys()" method.Here is how it works:
  1. driver.findElement(By.id("dropdownListId")).sendKeys("SelectOption1");  
Textarea :Send text
Sending text to Textboxes can be done by using "sendKeys()" method.Here is how it works:
  1. driver.findElement(By.id("textAreaId")).click();  
Button :click on it.
Submitting button can be done by using either "click()" or "submit()" mrthods.Here is how it works:
  1. driver.findElement(By.id("butonId")).click();  

5 different ways to refresh a webpage using Selenium Webdriver

Here are the 5 different ways, using which we can refresh a webpage.There might be even more :)

There is no special extra coding. I have just used the existing functions in different ways to get it work. Here they are 
1.Using sendKeys.Keys method

  1. driver.get("https://accounts.google.com/SignUp");  
  2. driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);  

2.Using navigate.refresh()  method

  1. driver.get("https://accounts.google.com/SignUp");    
  2. driver.navigate().refresh();  

3.Using navigate.to() method

  1. driver.get("https://accounts.google.com/SignUp");    
  2. driver.navigate().to(driver.getCurrentUrl());  

4.Using get() method

  1. driver.get("https://accounts.google.com/SignUp");    
  2. driver.get(driver.getCurrentUrl());  

5.Using sendKeys() method

  1. driver.get("https://accounts.google.com/SignUp");   
  2. driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");  


Wednesday 12 June 2013

MouseOver commands for Webdriver...

Try with below code.

         Actions builder = new Actions(driver);
        WebElement xpathElement = driver.findElement(By.xpath("xxxxxx"));
        builder.moveToElement(xpathElement).build().perform();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.findElement(By.xpath("xxxx")).click();

Monday 10 June 2013

Taking Screenshot using Selenium/Web Driver


 

Hello Guys,

 

I would like to discuss about Taking a Screenshot while running 
Selenium scripts. It is really required for you to take a screenshot
 at least of your failed test cases in the Test suite. 
It can save a lot of time, while reviewing the test result.

So let start.... Taking a screenshot in Selenium can be done in couple of ways. 
1) Using Selenium API.
2) If you are using Java binding for script language then Using Java Robot.

We will see both the implementation below. Lets start with Selenium API.

1) Using Selenium API


 
@Test
public void TakingScreenShotUsingSeleniumAPI(){
 try {
 WebDriver driver= new InternetExplorerDriver();
 driver.get("http://google.co.in");
 String fileName=getClass().getSimpleName();
 File screenshot = ((TakesScreenshot)driver)
                  .getScreenshotAs(OutputType.FILE);
 FileUtils.copyFile(screenshot, new File(fileName));
 driver.quit();
  } catch (Exception e) {
 e.printStackTrace();
    }
 }


2) Using Robot class of Java


 
@Test
public void TakingScreenShotUsingRobot(){
 try {
   WebDriver driver= new InternetExplorerDriver();
   driver.get("http://yahoo.co.in");
   String fileName=getClass().getSimpleName();
   Toolkit toolkit = Toolkit.getDefaultToolkit();
   Rectangle screenSize = new Rectangle(0,0,toolkit.getScreenSize()
                    .width,toolkit.getScreenSize().height);
   Robot robot = new Robot();
   BufferedImage bfIimage = robot.createScreenCapture(screenSize);
   ImageIO.write(bfIimage, "png", new File(fileName));
   driver.quit();
  } catch (Exception e) {
      e.printStackTrace();
     }
 }

}


Now you can use any of the above method to take screenshot, 
Robot screenshot may not work if the PC is locked, 
So you need to decide which will be good for you to use.


Thanks!!

WebDriver Wait Commands:

ImplicityWait: 

webdriver driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

driver.get("www.seleniumtwo.blogspot.com");

 

PageLoadTimeout: 

driver.manage().timeouts().pageLoadTimeout(20,SECONDS);

 

setScriptTimeout

driver.manage().timeouts().setScriptTimeout(20,SECONDS);

Friday 7 June 2013

Scroll down method in WebDriver

Here i am adding simple code for Scroll down method.we can scroll down and up methods in a 3 ways....

Using JavaScriptExecutor:

  JavascriptExecutor jsx = (JavascriptExecutor)driver;

  jsx.executeScript("window.scrollBy(0,450)", "");

 

 Using Actions Class

Actions action=new Actions(driver);
       action.sendKeys(Keys.DOWN).perform();
       action.sendKeys(Keys.DOWN).perform();
       action.sendKeys(Keys.DOWN).perform();
       action.sendKeys(Keys.DOWN).perform();
     
   {OR}

Actions action=new Actions(driver);
                  action.keyDown(Keys.CONTROL).sendKeys(Keys.PAGE_DOWN).perform();

   {OR}

With out Using Actions Class

 for(int i=0;i<20;i++)
                    {
                             driver.findElement(By.tagName("body")).sendKeys(Keys.DOWN);
                    }