Showing posts with label Selenium Webdriver. Show all posts
Showing posts with label Selenium Webdriver. Show all posts

Monday, 16 February 2015

Mouse Hover in Selenium Webdriver

Mouse Hover In Selenium Webdriver : Go to the url: http://www.guru99.com/ and mouse hover on 'Testing' and click on 'Selenium' , below I'm adding the code kindly execute it.


package GuruMouseHover;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class Guru {

    public static void main (String[] args)
    {

        try{

            WebDriver driver = new FirefoxDriver();         
            driver.get("http://www.guru99.com");
            WebElement element = driver.findElement(By.linkText("Testing"));
            Thread.sleep(2000);
            Actions action = new Actions(driver);
            action.moveToElement(element).build().perform();

//Click on Selenium
            driver.findElement(By.xpath(".//*[@id='rt-header']/div/div[2]/div/ul/li[2]/div/div/ul/li[3]/a")).click();
      
        driver.close();
      
        }
        
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
  
  
}

Tuesday, 27 January 2015

Handling Multiple Tabs in Selenium Webdriver

Hi, In this post I'm gonna explain you how to handle multiple tabs in selenium webdriver, well it is quite easy to handle it but remember selenium webdriver will open new window instead of new tab.

For Handling multiple tabs I'm going to use URL:http://irctc.co.in/, kindly execute below mentioned code.

Code:

package NewTabtest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.ArrayList;

public class NewTab {

    public static void main(String[] args) {
        try{
           
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.irctc.co.in");
           
            driver.manage().window().maximize();
           
            driver.findElement(By.xpath(".//*[@id='bluemenu']/ul/li[4]/a")).click();
           
            ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
    

          //Switching to new tab 'Tourist Trains'.
            driver.switchTo().window(tabs2.get(1));
           
            String me = "Tourist Trains";
           
            String mee = driver.findElement(By.xpath(".//*[@id='mainpannel']/div/h1")).getText();
           
            if(mee.equals(me))
            {
                System.out.println("Matches");
            }
           
            else
            {
                System.out.println("Doesn't matches");
            }

           //Closing the second tab 'Tourist Trains'
            driver.close();

          //Switching back to Home Tab
            driver.switchTo().window(tabs2.get(0));
           
            String se = "View All";
           
            String see = driver.findElement(By.xpath("html/body/div[1]/div/div[10]/div/div/div[1]/span/a")).getText();
           
           
            if(see.equals(se))
            {
                System.out.println("Matches");
            }
           
            else
            {
                System.out.println("Doesn't matches");
            }
           
            driver.close();
        }
   
    catch (Exception e)
    {
    }
    }
}


So this is how we handle multiple tabs, i hope you guys understood this post..:)

Sunday, 25 January 2015

Selecting a date from DatePicker

Hi Friends, in this post I'm gonna explain you how to select a date from datepicker using selenium webdriver, well it sounds easy but it is not yet easy as you think because mostly datepicker has dynamically changing IDs so coding is not straight forward here.

Just visit the URL:http://demos.telerik.com/kendo-ui/datetimepicker/index and you will see the datepicker as shown below


Now suppose you want to select a date and time like: 8/8/2016 5.00 PM then follow the below code and you will be able to select that value..:)

Code:

package NewTabtest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;




//Select date 8/August/2016 5.00 PM
public class DatePickerProgram {

   
    public static void main(String[] args) {
       
        try{
           
//Just Put value into variable and Print for the name sake
            String tobeenterdate = "8/8/2016 5:00 PM";
           
            System.out.println(tobeenterdate);
           
            WebDriver driver = new FirefoxDriver();
           
            driver.get("http://demos.telerik.com/kendo-ui/datetimepicker/index");
           
            driver.manage().window().maximize();


//Applying Explicit wait

            WebDriverWait wait = new WebDriverWait(driver, 300);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='to-do']/span/span/span/span[1]")));
           
            driver.findElement(By.xpath(".//*[@id='to-do']/span/span/span/span[1]")).click();
           
            Thread.sleep(2000);
           
            driver.findElement(By.xpath(" //a[text()='January 2015']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //a[text()='2015']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //td/a[text()='2016']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //td/a[text()='Aug']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //td/a[text()='8']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(".//*[@id='to-do']/span/span/span/span[2]")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //li[text()='5:00 PM']")).click();
           
            Thread.sleep(3000);
           
//Fetching the value selected from datepicker for comparison.
            String getdatepicker = driver.findElement(By.xpath(".//*[@id='datetimepicker']")).getAttribute("value");
           
            System.out.println(getdatepicker);
           
//Comparing actual value with expected value
            if (tobeenterdate.equals(getdatepicker))
            {
                System.out.println("DatePicker value is correct");
            }
            else
            {
                System.out.println("DatePicker value is incorrect");
            }
           
            driver.close();
        }
        catch (Exception e)
        {
           
        }
                
        }
}

Well, Execute the above code and it will run perfectly and moreover you can modify the values mentioned in the code according to your requirements..cheers..:)

Friday, 9 January 2015

Gmail Login Program using data driven framework

Hi Friends, in my last post i explained how to write simple Gmail login program, in this post I'm going to explain you how to write Gmail login program using Data Driven framework, in short I'm gonna read Gmail login values from Excel file(.xls), please follow the below steps carefully.

1) In Eclipse, Create a new project, then package then one class.

2) Add respective jar files.

3) Then download jxl jar files from the website:http://www.java2s.com/Code/Jar/j/Downloadjxljar.htm

4) In the above mentioned website download zip file with a name "jxl/jxl.jar.zip( 672 k)".

5) Once you download then extract zip file at an appropriate place, and after extracting then you will see one folder with a name "jxl.jar", now open that you will see one jar file with a name "jxl.jar".

6) Now you need to add 'jxl.jar' into your project , please see below steps.
    a) Right click on your project then select properties.
    b)Properties window will open now click on 'Java Build Path' then on 'libraries'.
    c) In Libraries click 'Add External JARs' and select 'jxl.jar' from appropriate place and click ok, this is how you have to add 'jxl.jar' into your project.

7) Create .xls file and place it at an appropriate place so that you can use that in your program, below I'm attaching screenshot and this is how you need to enter values in excel file.





8) Once you are done with all above steps, then start writing program, below I'm pasting the code.

package GmailLoginExcel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.FileInputStream;






// Please include these two
import jxl.Sheet;
import jxl.Workbook;

public class GmailLogin {
   
    public static void main(String[] args) {
        try{
           
                                                                           //eg: ("D:\\Gmail\\GmailLogin.xls")
            FileInputStream fi=new FileInputStream("Enter path of excel file");
            Workbook w=Workbook.getWorkbook(fi);
            Sheet s=w.getSheet(0);
           
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.gmail.com");
           
            driver.manage().window().maximize();
            //Wait
            WebDriverWait wait = new WebDriverWait(driver, 300);
           
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='Email']")));
           
          
            //Read data from excel sheet
            String s1 = s.getCell(0,0).getContents();
            String s2 = s.getCell(0,1).getContents();
           
         
           
           
            driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys(s1);
           
           
            driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys(s2);
           
           
            driver.findElement(By.xpath(".//*[@id='signIn']")).click();   
           
            Thread.sleep(19000);
           
            driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a/span")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(".//*[@id='gb_71']")).click();
           
            System.out.println("Success");
           
            Thread.sleep(3000);
           
            driver.close();
           
       
        }
        catch (Exception e)
        {
        }
        }

}

9) Above code will run perfectly, now modify above code according to your requirements..cheers..:)




Simple gmail login program

Hi All,

If you are new to selenium webdriver and want to execute simple program then below i'm writing code for simple Gmail login.please see

//Name of the package
Package GmailLogin

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Gmail {

    public static void main (String[] args)
    {
        try{

WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");
        driver.manage().window().maximize();

WebDriverWait wait = new WebDriverWait(driver, 300);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='Email']")));

driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("Enter email id");

driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("Enter password");
       
        driver.findElement(By.xpath(".//*[@id='signIn']")).click();

//Applying sleep in order to wait till Gmail mail page appears
Thread.sleep(19000);

driver.close();

}
            catch(Exception e)
        {
            System.out.println(e);
        }
    }
   
}


Please execute above mentioned program, it will run perfectly.:)