Monday, 11 May 2015

Selenium:How to upload a file using sendkeys() method

package files;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class FileUpload {

    WebDriver driver;
     @Test
      public void dragdrop() throws InterruptedException {
         driver.get("https://www.sendspace.com/");
        driver.manage().window().maximize();
        Thread.sleep(2000);       
        driver.findElement(By.id("upload_file")).sendKeys("C:\\Users\\Ramesh\\Desktop\\blog.txt");
         Thread.sleep(6000);
     }
   
     @BeforeClass
      public void beforeClass() {
          System.setProperty("webdriver.firefox.bin",
                    "C:\\Users\\Ramesh\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
          driver=new FirefoxDriver();
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
         
      }

      @AfterClass
      public void afterClass() {
          driver.quit();
         
      }
   
}

JAVA:Diff bw FINAL,FINALLY AND FINALIZE

final:
1.Final is a keyword applicable for classes,methods and variables
-->When a class declared as a final you cant inherit the class so inheritence is not possible
-->When a method is declared as a final then we cant override that method
-->When a variable declared as a final we cant perform re assignment
2.finally:
-->Finally is a block which is always associates with try catch blocks
-->This block will be executed always whether exception raised or not and wheteher we handled or not.
-->We can use finally block to perform clean up activities like database clocsing connections etc
3.finalize():
-->Finalize() is a method used to perform clean up activities like object destruction
-->Before destroying any useless objects garbage collector calls the finalize method to perform clean up activities
-->Garbage Collector is the assistence of JVM

Friday, 8 May 2015

JAVA:Java basics for selenium

COMING SOON

Selenium:HOW TO WRITE FRAMEWORK

COMING SOON
             COMING SOON
                          COMING SOON
                                        COMING SOON 

Selenium:Validations using asertions using selenium webdriver


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

public class Validations {

    public static void main(String[] args) throws InterruptedException {

        WebDriver d = new FirefoxDriver();
        d.manage().window().maximize();
        d.get("https://www.google.co.in/");
        d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        System.out.println(d.findElement(By.xpath(".//*[@id='hplogo']"))
                .getText());
        String text = d.findElement(By.xpath(".//*[@id='hplogo']")).getText();
        Assert.assertEquals("India", text);
        // Assert.assertTrue(true, text);
        // Assert.assertFalse(false, text);
        System.out.println("hello validations");
        Thread.sleep(6000);
        d.quit();

Selenium:Frame basic example in selenium web driver

package frameexamples;

import java.util.List;
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.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class AutoComplete {

    WebDriver driver;
      @Test
      public void frameOperation() throws InterruptedException {
          driver.navigate().to("http://jqueryui.com/");
          driver.findElement(By.linkText("Autocomplete")).click();
          List<WebElement> l=driver.findElements(By.tagName("iframe"));
          for (WebElement webElement : l) {
            System.out.println(webElement.getText());
        }
          driver.switchTo().frame(0);
          driver.findElement(By.xpath(".//*[@id='tags']")).sendKeys("java");
          Thread.sleep(5000);
          //switch back to default content
          driver.switchTo().defaultContent();
          driver.findElement(By.xpath(".//*[text()='Accordion']")).click();
       
        /*  //index
         // driver.switchTo().frame(0);
          //id or Name
          //in this example Frame is not having ID or Name so this is invalid here
          //driver.switchTo().frame("nameorid");
          //frameElement
          driver.switchTo().frame(driver.findElement(By.className("demo-frame")));
          //type in text box..
          driver.findElement(By.id("tags")).sendKeys("java");
         
          Thread.sleep(5000);
          //switch back to default content
          driver.switchTo().defaultContent();
          driver.findElement(By.linkText("Accordion")).click();*/
          Thread.sleep(5000);
      }
      @BeforeClass
      public void beforeClass() {
          System.setProperty("webdriver.firefox.bin",
                    "C:\\Users\\RAMESH\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
          driver=new FirefoxDriver();
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
         
      }

      @AfterClass
      public void afterClass() {
          driver.quit();
      }
   
}

Selenium:How to identify the frames in selenium web driver

package frameexamples;

import java.util.Iterator;
import java.util.List;
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.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class FrameIdentification {

    WebDriver driver;
      @Test
      public void dragdrop() throws InterruptedException {
          driver.get("http://jqueryui.com/");
          driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
         /*List<WebElement> l= driver.findElements(By.tagName("iframe"));
         for (WebElement webElement : l) {
            System.out.println(webElement.getText());
        }*/
          List<WebElement> l= driver.findElements(By.tagName("iframe"));
          Iterator itr=l.iterator();
          while (itr.hasNext()) {
            WebElement ele=(WebElement) itr.next();
            System.out.println(ele.getText());
           
        }
          Thread.sleep(5000);
         
         
      }
      @BeforeClass
      public void beforeClass() {
          System.setProperty("webdriver.firefox.bin",
                    "C:\\Users\\RAMESH\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
          driver=new FirefoxDriver();
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
         
      }

      @AfterClass
      public void afterClass() {
          driver.quit();
         
      }
   
   
   
}

Selenium:How to read data from excel sheet in selenium webdriver

package exceltest;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Dara {

    /*
     * public static void fileReading(String filepath) throws
     * FileNotFoundException{
     *
     * FileInputStream fis =new FileInputStream(filepath);
     *
     *
     * }
     */
    static Cell c;
    static Row r;

    public static void main(String[] args) throws InvalidFormatException,
            IOException {
        FileInputStream fis = new FileInputStream(
                "C:\\Users\\RAMESH\\workspace\\ramesh_projects\\Excel\\src\\testdata\\abcd.xls");

        // fileReading("C:\\Users\\IN00914\\workspace\\ramesh_projects\\Excel\\src\\testdata\\abcd.xls");
        // fileReading();

        // FileInputStream fis1 =new FileInputStream("");
        Workbook w = WorkbookFactory.create(fis);
        Sheet s = w.getSheet("Sheet1");
        /*
         * r=s.getRow(0); c=r.getCell(0); String
         * celldata1=c.getStringCellValue(); System.out.println(celldata1);
         * c=r.getCell(1); System.out.println(c.getStringCellValue());
         */

        for (int i = 0; i < s.getLastRowNum(); i++) {

            r = s.getRow(i);
            for (int j = 0; j < r.getLastCellNum(); j++) {

                r.getCell(j).getStringCellValue();
                System.out.println(r.getCell(j).getStringCellValue());
            }

        }

        /*
         * r=s.getRow(1); c=r.getCell(0); String
         * celldata=c.getStringCellValue(); System.out.println(celldata);
         * c=r.getCell(1); System.out.println(c.getStringCellValue());
         */
    }

}

Selenium:How to capture screen shot using seleniumm webdriver

package New;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class Screenshots {
    WebDriver driver;

    @Test
    public void screesnshot() throws IOException, InterruptedException {
        driver.get("http://www.bing.com/");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        // search
        driver.findElement(By.id("sb_form_q")).sendKeys("webdriver");
        // Thread.sleep(5000);
        driver.findElement(By.name("go")).click();

        WebElement clicker = driver.findElement(By
                .xpath(".//*[@id='b_results']/li[1]/h2/a"));
        Actions a = new Actions(driver);

        a.contextClick(clicker).sendKeys(Keys.ARROW_DOWN)
                .sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build()
                .perform();

        //giving location where to save screen shots
        String screeLocation = "C:\\Users\\Ramesh\\Desktop\\Aldo_Scripts\\Results\\screenshots\\";

        // captures screen
        File sreen = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(
                sreen,
                new File(screeLocation + new Date().getHours() + "_"
                        + System.nanoTime() + ".png"));
        Thread.sleep(9000);
    }

    @BeforeClass
    public void beforeClass() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterClass
    public void afterClass() {
        driver.quit();
    }

}

Selenium:How to handle web based popups in selenium webdriver

package New;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class PopUps {
    WebDriver driver;
     Alert alert;
  @Test
  public void poupup() {
     
     /* switching to alert
      * Alert alert=driver.switchTo().alert();
      * accepting the alert
      alert.accept();
      dismissing
      alert.dismiss();
      authenticating
      alert.authenticateUsing(null);*/
      try{
      alert=driver.switchTo().alert();
      }
      catch(NoAlertPresentException ex){
          ex.printStackTrace();
      }
      alert.accept();
      alert.dismiss();
      alert.authenticateUsing(null);
  }
  @BeforeClass
  public void beforeClass() {
      driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
     
  }

  @AfterClass
  public void afterClass() {
   
      driver.quit();
  }

}

Selenium:How to handle multiple windows using selenium web driver

package New;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
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.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class Multi_Windows {
    WebDriver driver;
    String winid;
  @Test
  public void multiplewindows() throws InterruptedException {
      driver.get("http://www.bing.com/");
      //driver.findElement(By.linkText("Outlook.com")).click();
      //driver.findElement(By.xpath(".//*[@id='outlook']")).click();
      //driver.findElement(By.xpath(".//*[@id='sc_hdu']/li[10]/a")).click();
     
      //office online
      driver.findElement(By.xpath("//*[@id='sc_hdu']/li[11]/a")).click();
     
      Thread.sleep(5000);
      //driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
      Set<String> element=driver.getWindowHandles();
      Iterator<String> itr=element.iterator();
      while(itr.hasNext())
      {
          winid=itr.next();
          System.out.println(winid);
      }
      driver.switchTo().window(winid);
      Thread.sleep(6000);
      driver.findElement(By.linkText("Products")).click();
      /*//driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
      driver.findElement(By.id("idDiv_PWD_UsernameExample")).sendKeys("hello@gmail.com");
      //driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
      driver.findElement(By.id("idDiv_PWD_PasswordExample")).sendKeys("hello");
    //  driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
      driver.findElement(By.id("idSIButton9")).click();*/
      Thread.sleep(6000);
     
  }
  @BeforeClass
  public void beforeClass() {
      driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
     
  }

  @AfterClass
  public void afterClass() {
      driver.quit();
  }

}

How to perform mouse over by using java script in selenium webdriver

package New;

import java.util.concurrent.TimeUnit;

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;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

import com.thoughtworks.selenium.webdriven.commands.IsElementPresent;

public class MouseOverByJavascript {
    WebDriver driver;

    @Test
    public void mouseoverjavascript() throws InterruptedException {
        driver.get("http://www.flipkart.com/");
        System.out.println("url is opened");
        driver.manage().window().maximize();
        WebElement element = driver.findElement(By
                .xpath("//a[@data-tracking-id='electronics']"));
        System.out.println("identified ");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        String javaScript = "var evObj = document.createEvent('MouseEvents');"
                + "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);"
                + "arguments[0].dispatchEvent(evObj);";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(javaScript, element);
        System.out.println("happened");
        Thread.sleep(8000);
    }

   
    @BeforeClass
    public void beforeClass() {

        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterClass
    public void afterClass() {
        driver.quit();
    }

}

Selenium:How to perform mouse over in selenium web driver

package New;

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.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class Mouse_Over {
    WebDriver driver;
  @Test
  public void mouesover() throws InterruptedException {
      driver.get("http://www.flipkart.com/");
    // WebElement element= driver.findElement(By.xpath("//span[text()='Electronics']"));
      WebElement element= driver.findElement(By.xpath("//a[@data-tracking-id='electronics']"));
     Actions a =new Actions(driver);
     a.moveToElement(element).build().perform();
    // a.clickAndHold(element);
     Thread.sleep(5000);
     
     //WebDriverWait wd=new WebDriverWait(driver, 30);
  }
  @BeforeClass
  public void beforeClass() {
      driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
     
  }

  @AfterClass
  public void afterClass() {
      driver.quit();
     
  }

}

Selenium:How to find the all links in selenium web driver

package New;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class LinksStore {

    public WebDriver driver;

    String s1;
   
     @BeforeClass
     public void openBrowser() throws Exception {
     driver= new FirefoxDriver();
     driver.manage().window().maximize();
     }
     @AfterClass
     public void closeBrowser() throws Exception {
     driver.quit();
     }
     @Test
     public void linksVerifification() throws InterruptedException{
         driver.get("http://www.bing.com/");
         /*List<WebElement> ele=driver.findElements(By.tagName("a"));
         Iterator<WebElement> s=ele.iterator();
         while (s.hasNext()) {
            WebElement e=s.next();
            if(e!=null){
                 s1=e.getText();
                System.out.println("active link is:"+s1);
            }
            else{
                System.out.println("non active links are:"+s1);
            }
           
       
           
        }*/
       
         List<WebElement> ele=driver.findElements(By.tagName("a"));
         for (int i = 0; i < ele.size(); i++) {
             if(ele.get(i)!=null){
               
                 System.out.println(ele.get(i).getText());
                 if(ele.get(i).getText().equalsIgnoreCase("NEWS")){
                    /* driver.findElement(By.xpath("//*[@id='scpt4']/a")).click();
                     Thread.sleep(5000);*/
                     driver.findElement(By.name(ele.get(i).getText())).click();
                 }
                 System.out.println("active link is:"+ele.get(i));
             }
             else{
                 System.out.println(ele.get(i).getText());
                 System.out.println("non active link is:"+ele.get(i));
             }
           
           
        }
     }
   
}

Selenium:How to perform drag and drop operations using selenium web driver

package New;

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.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class DragDrop {
    WebDriver driver;
  @Test
  public void dragdrop() throws InterruptedException {
      driver.get("http://jqueryui.com/");
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      driver.findElement(By.linkText("Draggable")).click();
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      //driver.switchTo().frame("demo-frame");
     driver.switchTo().frame(0);
     // driver.switchTo().frame(By.name("demo-frame"));
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      WebElement dd=driver.findElement(By.id("draggable"));
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      Actions a=new Actions(driver);
      a.dragAndDropBy(dd, 100, 100).build().perform();
      Thread.sleep(2000);
      driver.switchTo().defaultContent();
      driver.findElement(By.linkText("Droppable")).click();
      driver.switchTo().frame(0);
     // WebElement source=driver.findElement(By.xpath("//div[text()='Drag me to my target']"));
     // WebElement dest=driver.findElement(By.xpath("//div[text()='Drop here']"));
     // WebElement source=driver.findElement(By.id("draggable"));
     // WebElement dest=driver.findElement(By.id("droppable"));
      WebElement source=driver.findElement(By.xpath("//div[@id='draggable']"));
      WebElement dest=driver.findElement(By.xpath("//div[@id='droppable']"));
      a.dragAndDrop(source, dest).build().perform();
      Thread.sleep(5000);
  }
  @BeforeClass
  public void beforeClass() {
      driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
     
  }

  @AfterClass
  public void afterClass() {
      driver.quit();
     
  }

}

Selenium:How to handle Check list in selenium web driver

package New;

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;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class CheckList {
    WebDriver driver;
  @Test
  public void checklist1() throws InterruptedException {
      driver.get("http://apsrtc.com/");
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
     // WebElement ele=driver.findElement(By.id("dropdownflag"));
      //driver.switchTo().frame("lang_selection");
      //driver.switchTo().frame(1);
      driver.switchTo().frame("apsrtc.com");
      Thread.sleep(6000);
     // driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      WebElement ele=driver.findElement(By.xpath(".//select[@id='dropdownflag']"));
      Select s =new Select(ele);
      s.selectByIndex(5);
      //s.selectByValue("");
      //s.selectByVisibleText("");
      //s.deselectByIndex(0);
      //s.deselectByValue("");
      //s.deselectByVisibleText("");
      //s.deselectAll();
      Thread.sleep(6000);
     
  }
  @BeforeClass
  public void beforeClass() {
      driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
     
  }

  @AfterClass
  public void afterClass() {
      driver.quit();
  }

}

Selenium:How to handle Auto Suggestions using selenium web driver

package New;

import java.util.List;
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.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class Autosuggestions {
    WebDriver driver;

    @Test
    public void autosuggests() {
        driver.get("http://www.bing.com/");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.findElement(By.id("sb_form_q")).sendKeys("webdriver");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        /*
         * List<WebElement>
         * sug=driver.findElements(By.xpath("//ul[@id='sa_ul']/li"));
         * driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
         * System.out.println(sug.size()); for (int i = 0; i < sug.size(); i++)
         * { System.out.println(sug.get(i).getText());
         *
         * }
         */

        driver.findElement(By.xpath("//ul[@id='sa_ul']/li[3]")).click();
        ;
    }

    @BeforeClass
    public void beforeClass() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterClass
    public void afterClass() {
        driver.quit();

    }

}

Selenium:How to upload a file using Autoit

We all know that, Selenium will not support window based elements. To upload/download we need to use some third party tools.

Below post explains how to upload a file from local drive using AutoIt.
1.Dowload AutoIt from link : http://www.autoitscript.com/site/autoit/downloads/
2.Install it
 (This code is to select a file from window after clicking on browse button from web page, After this we have click on upload button from web page)

WinWaitActive("File Upload")
Send("C:\Temp\Test.txt")
Send("{ENTER}")

Write this code in Notepad and save the file as “Upload.au3”
Right Clicked on the created file “Upload.au3” and click on Compile Script.
You will get “Upload.exe” file

Call this file in Selenium script or in RFT script as below:

Process proc = Runtime.getRuntime().exec("C:\\Temp\\Ramesh Upload.exe");

Sample Selenium Code using AutoIt upload:

public class Upload {
public WebDriver driver;

@Before
public void setUp() throws Exception {
driver= new FirefoxDriver();
driver.manage().window().maximize();
}
@Test
public void testTestScenario1() throws Exception
{
driver.get("http://www.2shared.com/");
driver.findElement(By.id("upField")).click();
Thread.sleep(5000);
Process proc = Runtime.getRuntime().exec("C:\\Temp\\Upload.exe");
driver.findElement(By.xpath("//input[@title='Upload file']")).click();

}
@After
public void tearDown() throws Exception {
driver.quit();
}
}

Selenium:How to perform right click operations



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class RightClick {

   
    WebDriver driver;
      @Test
      public void rightclickoperation() throws InterruptedException {
          driver.get("http://www.bing.com/");
          driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
          //search
         driver.findElement(By.id("sb_form_q")).sendKeys("webdriver");
        // Thread.sleep(5000);
         driver.findElement(By.name("go")).click();
       
         WebElement clicker=driver.findElement(By.xpath(".//*[@id='b_results']/li[1]/h2/a"));
         Actions a=new Actions(driver);
       
         a.contextClick(clicker).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
       
         Thread.sleep(5000);
      }
      @BeforeClass
      public void beforeClass() {
          System.setProperty("webdriver.firefox.bin",
                    "C:\\Users\\IN00914\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
          driver=new FirefoxDriver();
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
         
      }

      @AfterClass
      public void afterClass() {
          driver.quit();
      }
   
}

Selenium:Validations for mobile number

package regularexpressions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Mobile {
   
public static void main(String[] args) {
   
    String num="9177791456";
    String form1="[0/91]?[7-9][0-9]{9}";
    //String form1="[7-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]";
    //String form1="0?[7-9][0-9]{9}";
    //String form1="(0/[+]?91)[7-9][0-9]{9}";here wrong as output
    //Pattern p =Pattern.compile("[0/91]?[7-9][0-9]{9}");  
   
    //String form1="[a-z][A-Z][a-z A-Z 0-9 . _]*@[a-z A-Z 0-9]+([.][a-z A-Z]+)+";
    Pattern p =Pattern.compile(form1);
    Matcher m=p.matcher(num);
   
    if(m.find()&&m.group().equals(num)){
        System.out.println("valid mobile number");
    }
    else
    {
        System.out.println("invalid mobile number");
    }
   
}

}

Selenium:Validations for mail

package regularexpressions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Mailid {
public static void main(String[] args) {
    String mid="scjpramesh@gmail.com";
    //String target="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
   
    String target="^[_A-Za-z0-9-]+@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
   
    Pattern p =Pattern.compile(target);
    Matcher m=p.matcher(mid);
    if(m.find()&&m.group().equals(mid)){
        System.out.println("valid mail id");
    }
    else
    {
        System.out.println("invalid mail id");
    }
}
}

Selenium:Window based popups Handling using sikuli

package sikuli;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SikuliSelenium {

    WebDriver driver;
     @Test
     public void testTestScenario1() throws Exception
     {
        // And now use this to visit Google

         driver.get("http://www.seleniumhq.org/");
         driver.findElement(By.xpath(".//*[@id='menu_download']/a")).click();
         Thread.sleep(4000);
         driver.findElement(By.xpath(".//*[@id='mainContent']/p[3]/a")).click();
       
         //Create and initialize an instance of Screen object  

         Screen screen = new Screen();

         //Add image path 

         Pattern image = new Pattern("C:\\Users\\IN00914\\Desktop\\Payslips\\save.png");

         //Wait 10ms for image

         screen.wait(image, 10);
         //Click on the image

         screen.click(image);

         Thread.sleep(8000);
           }
     @BeforeClass
        public void beforeClass() {

            System.setProperty("webdriver.firefox.bin",
                    "C:\\Users\\Ramesh\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
            driver = new FirefoxDriver();
            driver.manage().window().maximize();

        }

        @AfterClass
        public void afterClass() {
            driver.quit();
        }
}

Selenium:How to Upload a file using ROBOT Class

package fileupload;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

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;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class FileUpload1 {

    WebDriver driver;
    @BeforeClass
    public void beforeClass() {

        System.setProperty("webdriver.firefox.bin",
                "C:\\Users\\Ramesh\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();

    }

    @AfterClass
    public void afterClass() {
        driver.quit();
    }
   
   @Test
    public static void main11() throws AWTException {
    // TODO Auto-generated method stub
    WebDriver  driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("https://www.sendspace.com/");
    WebElement element = driver.findElement(By.xpath("//*[@id='upload_file']"));
    Actions actions = new Actions(driver);
    actions.click(element).build().perform();
    StringSelection stringSelection = new StringSelection("C:\\Users\\IN00914\\Desktop\\CC-BILL.png");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
   
   
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    }
                                                                   
}