Monday, 7 September 2015

How to add quotes for string

String expErrorMsg3="Flexible Format";
String expTxt="\""+expErrorMsg3+"\" ";
syso(expTxt);

Output="Flexible Format"

Wednesday, 5 August 2015

Get toolTip on pie charts using selenium

package gooGle;

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

public class Read_PieChart_ToolTip {

 WebDriver driver;

 @BeforeTest
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://yuilibrary.com/yui/docs/charts/charts-pie.html");
 }

 @Test
 public void pieChart(){
  //Locate pie chart elements based on different colors.
  WebElement VioleteColor = driver.findElement(By.xpath("//*[contains(@class,'yui3-svgSvgPieSlice')][@fill='#66007f']"));
  WebElement GreenColor = driver.findElement(By.xpath("//*[contains(@class,'yui3-svgSvgPieSlice')][@fill='#295454']"));
  WebElement GreyColor = driver.findElement(By.xpath("//*[contains(@class,'yui3-svgSvgPieSlice')][@fill='#e8cdb7']"));
  WebElement LightVioleteColor = driver.findElement(By.xpath("//*[contains(@class,'yui3-svgSvgPieSlice')][@fill='#996ab2']"));
  WebElement BrownColor = driver.findElement(By.xpath("//*[contains(@class,'yui3-svgSvgPieSlice')][@fill='#a86f41']"));
 
  //locate tooltip pie chart.
  WebElement ToolTip = driver.findElement(By.xpath("//div[contains(@id,'_tooltip')]"));

  //Click on pie chart parts and get tooltip values.
  System.out.println("-X-X-X-X-X-X-X-X- Violete Part -X-X-X-X-X-X-X-X-");
  VioleteColor.click(); 
  System.out.println(ToolTip.getText());
  System.out.println(); 
 
  System.out.println("-X-X-X-X-X-X-X-X- Grey Part -X-X-X-X-X-X-X-X-");
  GreyColor.click();
  System.out.println(ToolTip.getText());
  System.out.println();
 
  System.out.println("-X-X-X-X-X-X-X-X- Light Violete Part -X-X-X-X-X-X-X-X-");
  LightVioleteColor.click();
  System.out.println(ToolTip.getText());
  System.out.println();
 
  System.out.println("-X-X-X-X-X-X-X-X- Green Part -X-X-X-X-X-X-X-X-");
  GreenColor.click();
  System.out.println(ToolTip.getText());
  System.out.println();
 
  System.out.println("-X-X-X-X-X-X-X-X- Brown Part -X-X-X-X-X-X-X-X-");
  BrownColor.click();
  System.out.println(ToolTip.getText()); 
 }
}

Work with mouse right click operations

package gooGle;

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.Test;

public class Right_Click {
  @Test
public void TestClick() throws Exception{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
Actions act=new Actions(driver);
act.contextClick(driver.findElement(By.xpath("//*[@id='lga']"))).perform();  //  ////*[@id='addlang']/a[6]
}
@Test
public void rightClickAndSelectOption()
{
      WebDriver driver1=new FirefoxDriver();
      driver1.navigate().to("http://www.google.com");
     
      driver1.manage().window().maximize();
     
      WebElement oWE=driver1.findElement(By.linkText("About"));
     
      Actions oAction=new Actions(driver1);
      oAction.moveToElement(oWE);
      oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
}
}


Tuesday, 4 August 2015

Zoom In and zoom out the page using keyboard

package gooGle;

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;

public class Zoom_In_Out {
 static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
    driver=new FirefoxDriver();
driver.get("http://www.google.com");
Thread.sleep(1000);
WebElement html = driver.findElement(By.tagName("html"));
//Zoom in
html.sendKeys(Keys.chord(Keys.CONTROL,Keys.ADD));
System.out.println("zooming in");
Thread.sleep(1000);
//Zoom out
html.sendKeys(Keys.chord(Keys.CONTROL,Keys.SUBTRACT));
Thread.sleep(1000);
System.out.println("zooming out");
}
}

Split string at only Integer using regular expresstion and java code

Scenario: Split string at only Integer.
Ex 1: Abc 123
Ex 2: abc def 123
Ex 3: abc swe def rde 12 12

Here, want to print only string not integer.

Code:

public class SplitAtInteger {
    public static void main(String[] args) {
       
//ex: String str1="abc 123";
String str="Abc def 12";
String[] part = str.split("(?<=\\D)(?=\\d)");
System.out.println(part[0]);

}
}

Output 1: Abc def
Output 2: abc 

+++++++

Scenario:
Text=abcd1234;
Here, i want split
abcd
1234
We can achive this 2 ways:


 package java_interview_logics_practice;

public class SplitStringInteger {

    //way 1
  
    public static void main(String[] args) {
        String text="abcd1234";
        String[] part = text.split("(?<=\\D)(?=\\d)");
        System.out.println(part[0]);
        System.out.println(part[1]);

    }
   
    /*
    //way 2
    public static void main(String[] args) {
        String text="abcd1234";
        String number = "";
        String letter = "";
        for (int i = 0; i < text.length(); i++)
        {
              char a = text.charAt(i);
              System.out.println("aaa : " + a);
              if (Character.isDigit(a)) {
                  number = number + a;

            } else {
                  letter = letter + a;

            }
     }
     System.out.println("Alphates in string:"+letter);
     System.out.println("Numbers in String:"+number);

        }

    */

}
 

Thursday, 30 July 2015

Download files using autoIt and firefoxProfiles(javaScript) and Robot keys

Download file using autoIt and firefoxProfiles(javaScript)

Autoit:

1) Download autoIT and extract
2) In 'SciTE Script Editor' write the below code and save(save with .au3 format) in specific location.

WinWait("[CLASS:#MozillaDialogClass]","",8)
Send("{ALTDOWN}s{ALTUP}")
Sleep(5000)
Send("{ENTER}")

3) Compile the script, and it will generate .exe application.

Eclipse:
public class Download_AutiIt_pass {

    WebDriver d=new FirefoxDriver();
    @Test
    public void downloadAutoIT() throws IOException
    {
        d.get("http://only-testing-blog.blogspot.in/2014/05/login.html");
        d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        d.findElement(By.xpath("//a[text()='Download Text File']")).click();
        Runtime.getRuntime().exec("D:\\Automation_workspace\\AutoIT\\download_keyboard_autoIT.exe");
           //give the path of .exe(after compile :step 3)   
    }
}
output:
It will downloads the file

  Download file using firefoxProfiles:

selenium webdriver do not have any feature to handle this save file dialogue.
 But yes, Selenium webdriver has one more very good feature by which you do not need to handle that dialogue
and you can download any file very easily. We can do It using webdriver's Inbuilt class FirefoxProfile and Its
 different methods

Code:
package testcases;

import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
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.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;

public class Download_File_pass {

    WebDriver d=new FirefoxDriver();
    @Test
    public void downloadAutoIT() throws IOException, AWTException
    {
        FirefoxProfile pro=new FirefoxProfile();
        pro.setPreference("browser.download.folderList",2);
        pro.setPreference("browser.download.manager.showWhenStarting",false);
        pro.setPreference("browser.download.dir","D:\\Automation_workspace\\AutoIT\\downloaded_files");
        pro.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
           WebDriver d=new FirefoxDriver(pro);
        d.get("http://www.ox.ac.uk/admissions/graduate/applying-to-oxford/after-you-apply/accepting-your-offer");
        d.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
        d.manage().window().maximize();
        d.findElement(By.xpath("//*[@id='block-ds-extras-oxweb-ds-page-content-right']/div/div/div/div[2]/a")).click();
         WebElement element = d.findElement(By.xpath("//*[@id='block-menu-block-9']/h2/a"));
            ((JavascriptExecutor) d).executeScript("arguments[0].scrollIntoView(true);", element);

            d.findElement(By.xpath("//a[text()='Cancellation form']")).click();                       
    }
        }
output:
it will download the file 

+++++++Other way+++++
 package interview;

import java.util.concurrent.TimeUnit;

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

public class Auto_it {

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

           
        FirefoxProfile profile=new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", "D:\\selenium_NEW_PRACTICE");// give path where u want to download file
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/x-pdf,text/csv,application/zip");
     
        WebDriver driver=new FirefoxDriver(profile);

   
        driver.get("http://seleniumhq.org/download/");
        driver.manage().timeouts().implicitlyWait(9000,TimeUnit.SECONDS);
        driver.findElement(By.xpath("html/body/div[1]/div[2]/div[2]/p[6]/a")).click();
        System.out.println("hiiii");
        driver.close();
   
    }
   

}


+++++Using robot keys++++++++++
driver21.findElement(By.xpath("//*[@id='csvbutton']")).click();
      Thread.sleep(20000);
      Robot robot = new Robot();
      robot.keyPress(KeyEvent.VK_ALT);
      robot.keyPress(KeyEvent.VK_S);
      robot.keyRelease(KeyEvent.VK_S);
       robot.keyRelease(KeyEvent.VK_ALT);
       robot.keyPress(KeyEvent.VK_ENTER);
       System.out.println("File is downloaded");

Scroll up/down using javaScript


package interview_practice;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ScrollDown_JavaScript {
     WebDriver driver;
@BeforeTest
 public void setup() throws Exception {
      driver = new FirefoxDriver();
      driver.manage().window().maximize();
     }
//@Test(priority=0) //working fine
public void Scroll_Page_usingJavaScript()
{
    driver.get("http://google.com");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
     //To scroll down web page by 600 pixels In x(vertical) direction. 
      //You can y parameter to scroll page In horizontal direction
    JavascriptExecutor javascript = (JavascriptExecutor) driver;
    javascript.executeScript("window.scrollBy(0,600)", "");
     //To scroll up web page by 300 pixels In x(vertical) direction.
      javascript.executeScript("window.scrollBy(0,-300)", "");
   
    }
//@Test(priority=1) //working fine
public void Scroll_totalDown_usingJavaScript() throws AWTException
{
    driver.get("http://googlet.com");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    //Scroll down to bottom of the page.
      JavascriptExecutor javascript = (JavascriptExecutor) driver;
      javascript.executeScript("window.scrollTo(0, document.body.scrollHeight)", ""); 
   
    }

//Scroll till element.
//@Test(priority=2)  //working fine
public void Scroll_till_element_usingJavaScript() throws AWTException
{
    driver.get("http://google.com");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    JavascriptExecutor je = (JavascriptExecutor) driver;
    WebElement element = driver.findElement(By.xpath("//div[@id='dragdiv']"));
    je.executeScript("arguments[0].scrollIntoView(true);",element);
   
    }
@Test(priority=3)   //pending
public void Scroll_window_usingRobotKeys() throws AWTException
{
    driver.get("http://google.com");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
   

   
    }


}