Friday, 24 April 2015

Alternative to sendkeys()

SendKeys() not working? Not to worry...

My computer got crashed and on the prosses of downloading  selenium and eclipse ,I think I messed up with downloading correct version of java compiler or for the reasons I do not know....somehow sending text using sendKeys() seemed to be an impossible task...

Here I came up with an  alternative for it using JavascriptExecutor.
**Thanks to Kritesh **
Simple example for the substitution of sendKeys using Gmail login..



import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class gmail_login {

    /**

     * @param <string>
     * @param args
     * @throws InterruptedException
     */
    public static <sendKeys, string> void main(String[] args) throws InterruptedException
    {
       
      
        WebDriver driver = new FirefoxDriver();
        driver.get("http://gmail.com");
        JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
        WebElement mailid = driver.findElement(By.xpath("//input[@name='Email']"));
        myExecutor.executeScript("arguments[0].value='abcd@gmail.com';", mailid);
        WebElement password=driver.findElement(By.id("Passwd"));
        myExecutor.executeScript("arguments[0].value='efgh';", password);
        WebElement signin=driver.findElement(By.xpath("//input[@type='submit']"));
        signin.click();
        Thread.sleep(10000);
        driver.quit();
    }

}

Tuesday, 19 February 2013

Deleting multiple emails and undoing it.

Once you got to know how to check a box, ,It is easy to click on one or multiple checkboxes.Which enables us to automate options like deleting multiple emails.

Here is the code below:

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.support.ui.Select;

public class deletingMultiplemails {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    WebDriver d1;
    d1=new FirefoxDriver();
    d1.get("https://mail.google.com/");
    d1.findElement(By.id("Email")).sendKeys("Your ID");
    d1.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("Your password");
    d1.findElement(By.id("signIn")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    WebElement checkbox1=d1.findElement(By.id(":mf"));
    checkbox1.click();
    WebElement checkbox2=d1.findElement(By.id(":m5"));
    checkbox2.click();
    WebElement checkbox3=d1.findElement(By.id(":lv"));
    checkbox3.click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      d1.findElement(By.xpath("/html/body/div[5]/div[2]/div/div[2]/div/div[2]/div/div/div/div/div/div/div/div /div/div[2]/div[3]")).click();
    //d1.findElement(By.xpath("//div[@class='oZ-jc T-Jo J-J5-Ji T-Jo-Jp']")).click()
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    d1.findElement(By.xpath("//span[@id='link_undo']")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    d1.quit();
    }

}

Deleting an email and undoing it.

I have tried deleting an email and again undoing it . I was quite successful in it .Check it out..

import java.util.concurrent.TimeUnit;

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.support.ui.Select;

public class deletingAnEmail {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    WebDriver d1;
    d1=new FirefoxDriver();
    d1.get("https://mail.google.com/");
    d1.findElement(By.id("Email")).sendKeys("Your Id");
    d1.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("Your password");
    d1.findElement(By.id("signIn")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// This is for deleting.. 
    WebElement checkbox=d1.findElement(By.id(":mf"));
    checkbox.click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    d1.findElement(By.xpath("/html/body/div[5]/div[2]/div/div[2]/div/div[2]/div/div/div/div/div/div/div/div        /div/div[2]/div[3]")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//This is for undoing..  
    d1.findElement(By.xpath("//span[@id='link_undo']")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    d1.quit();
    }

}

Monday, 18 February 2013

composing an email using selenium web driver

composing an email using selenium web driver

 I tried composing an email from gmail using selenium webdriver.
I found a better way to enter text without switching frames.
Please refer to the below code.



import java.util.concurrent.TimeUnit;

//import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebElement;
//import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;

public class composingAnEmail {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    WebDriver d1;
    d1=new FirefoxDriver();
    d1.get("https://mail.google.com/");
    d1.findElement(By.id("Email")).sendKeys("Your mail Id");
    d1.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("Your password");
    d1.findElement(By.id("signIn")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    d1.findElement(By.xpath("/html/body/div[5]/div[2]/div/div[2]/div/div/div/div[2]/div/div/div/div/div")).click();
    d1.findElement(By.xpath("//textarea[@name='to']")).sendKeys("receiver's Id");
     d1.findElement(By.name("subject")).sendKeys("test mail");
  //I felt this method works more better than switching frames.
    d1.findElement(By.xpath("//iframe[@class= 'Am Al editable']")).click();
    d1.findElement(By.xpath("//iframe[@class= 'Am Al editable']")).sendKeys("Hi ! How are you ?This is my first email using selenium webdriver.");
    d1.findElement(By.xpath("//div[@class='T-I J-J5-Ji Bq nS T-I-KE L3']")).click();
    d1.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    d1.quit();
    }
}