Thursday 6 November 2014

Selenium Web driver in Java tutorial

CSS Selector examples in selenium webdriver


Below table shows commonly used css Selectors in Selenium.


Find all elements with tag input

input
Find all input tag element having  attribute type = ‘hidden’
input[type='hidden']
Find all input tag element having  attribute type = ‘hidden’  and name attribute = ‘ren’
input[type='hidden'][name='ren']
Find all input tag element with attribute type containing ‘hid’
input[type*='hid']
Find all input tag element with attribute type starting with ‘hid’
input[type^='hid']
Find all input tag element with attribute type ending with ‘den’
input[type$='den']

Below example demonstrates how we can use cssSelectors to identify the elements in Java.
To identify the edit box with name attribute as q, we can use below css expression.
input[name='q']
Above css Selector expression will identify the element with tagname as input and of which name attribute’s value is name.


//find element having name attribute as q using css
           driver.findElement(By.cssSelector("input[name='q']")).sendKeys("css");

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Monday 3 November 2014

Unable to select value from drop down in selenium webdriver.

Sometimes you may encounter a scenario where selenium Select class can not be used to select the value from the drop down or list box in any browser like internet explorer, google chrome or firefox.
Sometime situation is more difficult because after selecting the value from drop down, other controls on the page get auto populated.
So basically there are 2 problems.
  1. Selenium unable to select the value from the drop down.
  2. Java script event does not get invoked. So other controls do not get auto populated.
We can solve above issues by below code.

WebElement c = driver.findElement(By.xpath("//select[@name='empType']"));
((JavascriptExecutor) driver).executeScript("arguments[0].value='X';",c);

Above script will select value X from the drop down.

After this you will have to trigger the on change event on the drop down using below code.

((JavascriptExecutor) driver).executeScript(
"var evt = document.createEvent('HTMLEvents');
evt.initEvent ('change', true, true);
arguments[0].dispatchEvent(evt);",c);


This will fire the drop down change event on the web list.
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Saturday 1 November 2014

How to use TestNG with Selenium Webdriver?

TestNG is the next generation testing framework. You can easily integrate your selenium scripts in TestNG.

Please follow below steps to run TestNG tests in Eclipse.

  1. Download TestNG at http://testng.org/doc/download.html
  2. Then Create a TestNG class in any project.
  3. Define test methods in that class using @Test annotation
  4. Right click on the TestNG class and run as TestNG class.
In below example, we have created a TestNG class called NewTest with 2 test methods Test1 and Test2.

package framework;

import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest {
  @Test
  public void Test1() {

   //Test something here.
   Assert.assertNotNull("abc");
  }
  
  @Test
  public void Test2() {
   //Test something here.
Assert.assertNull("jj"); } @BeforeSuite public void beforeSuite(){
      //this method is called once before test execution starts
//here you can write a code to launch the selenium web driver. } @AfterSuite public void afterSuite(){
      //this method is called once after test execution ends.
//here you can write a code to close and quit the selenium web driver. } }

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com 

How to upload files in selenium web driver using AutoIT Script?

Some times you will have to handle the file upload window using selenium web driver.
Selenium does not provide any such way to handle the window pop ups.

You can use AutoIT script to automate this task. AutoIT is a scripting language for Microsoft windows applications. You will have to download and install AutoIT from this url Download AutoIT

Once downloaded, you can write below code in the script file and invoke that file code just when you need to handle the upload window. Semicolon(;) is used to mark the comments in AutoIT scripts.

;below line states Windows controller to wait for the window with title Open to display. Whatever is the name of window, you need to pass it here.

WinWaitActive("Open")

;below line will enter the file location to be uploaded.
Send("C:\Users\sagar\Documents\onion_fennel_bisque.jpg")

;finally we need to click on Ok or press enter to start the upload process.
Send("{ENTER}")

Here is the complete example.

package seleniumtest;

//autoIT
//TestNG
//Grid

//import the required classes
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public  class AutoIT {
 
 public static void main(String[] args) {
  
     WebDriver driver =null;
     //set the driver path
     System.setProperty("webdriver.chrome.driver",
 "F:\\selenium\\csharp\\chromedriver.exe");
     
     System.setProperty("webdriver.ie.driver", 
"F:\\selenium\\IEDriverServer_Win32_2.43.0\\IEDriverServer.exe");
     
     Date dNow = new Date( );
     
  
     //create new driver instance
    driver = new ChromeDriver();
  
     driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
   
     
      try{
       
       
       driver.get("https://www.pdftoword.com/");
       driver.findElement(By.id("file-uploader")).click();

                        //please note that below line calls the AutoIT
 script which will handle the file upload dialog in google chrome browser. 
Also note that we need to provide the path of exe file which
is created after we compile and build the AutoIT script.
Runtime.getRuntime().exec("F:\\selenium\\handlefile1.exe"); //wait for 2 seconds Thread.sleep(5000); }catch(Exception e){ //print exception if any System.out.println(e.getMessage() ); e.printStackTrace(); } finally{ //close the driver driver.close(); //quit the driver. driver.quit(); } } }

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Buy Best Selenium Books

Contributors