Monday 21 January 2013

How to count the number of contents (Values) in the drop down using selenium webdriver?


How to count the number of contents (Values) in the drop down using selenium webdriver?




First we need to store the contents of DDL into one string. Then we need to split that string by line (\n).

Normally it will count "-Select-" as a string. So we need minus 1 from the array length.

 WebElement J = driver.findElement(By.id("ddlType"));
    String text = J.getText(); 
    String[] DDLcount =text.split("\n");
    System.out.println("Count of DDL contents:" +(DDLcount.length-1));


How to upload file using selenium webdriver in java?


How to upload file using selenium webdriver in java?

 If you click browse button, it will open a window for uploading the file.


Already we know, Selenium web driver can handle only web based application. It unable to find the window based application. So, It can't browse the files for uploading.
Then, How we can upload the file??

The simple trick is, we need to specify the path of the file in the browse text box and click upload button

Here is the code:

driver.findElement(By.xpath("xpath of Browse_Textbox")).sendKeys("path_of_file");
driver.findElement(By.xpath("xpath of upload_file_Button")).click();

Friday 18 January 2013

How to run the Webdriver in Internet Explorer?


How to run the Webdriver in Internet Explorer?


1. First you need to download the IE Webdriver:

Here is the Link to Download:

http://code.google.com/p/selenium/downloads/list

2. Use the Below code in your Java Program:


System.setProperty("webdriver.ie.driver", "Path of IEDriver");
driver = new InternetExplorerDriver();


Note:  You need to specify the path with double slash

Eg : E:\\IEDriverServer_x64_2.28.0\\IEDriverServer.exe

How to run the Webdriver in Chrome browser?

How to run the Webdriver in Chrome browser?


1. First you need to download the Chrome Webdriver:

Here is the Link to Download:

http://code.google.com/p/chromedriver/downloads/list

2. Use the Below code in your Java Program:

System.setProperty("webdriver.chrome.driver", "Path of chromedriver.exe");
driver = new FirefoxDriver();

Note:  You need to specify the path with double slash

Eg : "E:\\chromedriver_win_23.0.1240.0\\chromedriver.exe"

Wednesday 16 January 2013

Wait Statement in Selenium Webdriver

Wait Statement in Selenium Webdriver

There are two types of Wait Statement:
1. Implicit Wait
2. Explicit Wait

Implicit Wait:

An Implicit Wait is to tell the Webdriver to wait for a particular seconds

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

In this scenario, Webdriver will wait for 10 seconds.


Explicit Wait:

wait.until(ExpectedConditions.visibilityOf(By.xpath("//*@id='form1']/--------/img")));

In this scenario, Webdriver will wait upto the element is present.

Sending special keys and key event in selenium webdriver

Sending special keys and key event in selenium webdriver

Use the Below Code:

//Sending F5 key  
driver.findElement(By.id("name")).sendKeys(Keys.F5);  
  
//Sending arrow down key   
driver.findElement(By.id("name")).sendKeys(Keys.ARROW_DOWN);  

//sending Escape key  
driver.findElement(By.id("name")).sendKeys(Keys.ESCAPE);
  
//sending pagedown key from keyboard  
driver.findElement(By.id("name")).sendKeys(Keys.PAGE_DOWN);  
  
//sending space key   
driver.findElement(By.id("name")).sendKeys(Keys.SPACE);  
  
//sending tab key  
driver.findElement(By.id("name")).sendKeys(Keys.TAB);  
  
//sending alt key  
driver.findElement(By.id("name")).sendKeys(Keys.ALT);

How to handle the Javascript alert in Selenium Webdriver?

How to handle the Javascript alert in Selenium Webdriver?


Use the Below Code:

Alert alert = driver.switchTo().alert();
alert.accept();                         //For Clicking OK 
//alert.cancel();                       For Clicking CANCEL

How to select a value from the dropdown using selenium Webdriver?

How to select a value in the dropdown using selenium Webdriver?

Use the Below code:

import org.openqa.selenium.support.ui.Select;
                                 
Select droplist = new Select(driver.findElement(By.xpath("//*[@id='ddlrecurrencetype']")));
droplist.selectByVisibleText("Weekly");

How to drag and drop the control using selenium Webdriver

How to drag and drop the control using selenium Webdriver?

Use the following Code:

WebElement draggable = driver.findElement(By.xpath("//*[@id='lblpgmlist']"));
WebElement to = driver.findElement(By.xpath("//*[@id='calendar']/div/div/table/tbody/tr[2]/td[2]"));
new Actions(driver).dragAndDrop(draggable,to).build().perform();


How to maximize the browser using selenium webdriver?

How to maximize the browser using selenium webdriver?


Use this Code:

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



How to take screenshot using selenium Webdriver?

How to take screenshot using selenium Webdriver?


By Using  below code you can take screenshot of current webpage:

driver.get("http://google.com");            
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E://google.png"));