Tuesday, 17 May 2016

Handling page scroll & Actions & Slider bar using selenium

package linkedIn_Interview;

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

import javax.imageio.ImageIO;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.OutputType.*;

import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.screentaker.ViewportPastingStrategy;
public class Question1 {

    static WebDriver d;
    @BeforeMethod
    public void setUp()
    {
        System.setProperty("webdriver.chrome.driver", "D:\\selenium_NEW_PRACTICE\\jars\\chromedriver_win32\\chromedriver.exe");
        d=new ChromeDriver();
        d.manage().window().maximize();
    }
    @Test
    public void verifyLinkedInExam() throws InterruptedException, IOException
    {       
        d.get("http://www.ia.ca");
        getScreenshot();
        //take the screenshot of the entire home page and save it to a png file
        //Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(d);
        //ImageIO.write(screenshot.getImage(), "PNG", new File("D:\\selenium_NEW_PRACTICE\\screenshots\\home"+System.currentTimeMillis()+".png"));
        d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        d.findElement(By.xpath("//span[contains(text(),'Loans')]")).click();
        d.findElement(By.xpath("//a[contains(text(),'Mortgages')]")).click();
        String mortgateTtl=d.getTitle();
        Assert.assertEquals(mortgateTtl, "Mortgage - Mortage rates | iA Financial Group");
        System.out.println("Verify title is pass : ");
        JavascriptExecutor js=(JavascriptExecutor)d;
        js.executeScript("window.scrollBy(0,700)", "");
        System.out.println("page is scrolling to down : " );       
        d.findElement(By.xpath("//a[contains(text(),'Calculate your payments')]")).click();
        JavascriptExecutor js1=(JavascriptExecutor)d;
        js1.executeScript("window.scrollBy(0,700)", "");
        System.out.println("page is scrolling to down : " );
        WebElement slider = d.findElement(By.xpath("//div[contains(@class,'slider slider-horizontal')]"));
        int width=slider.getSize().getWidth();
        System.out.println("printing size width of slider : " + width);
       
        Actions act=new Actions(d);
        act.click(slider).build().perform();
        Thread.sleep(1000);
        for(int i=0;i<2;i++)
        {
            act.sendKeys(Keys.ARROW_RIGHT).build().perform();           
        }
        for(int i=0;i<1;i++)
        {
        d.findElement(By.xpath("//*[@id='MiseDeFondPlus']")).click();
        }
        Select years=new Select(d.findElement(By.xpath("//*[@id='Amortissement']")));
        years.selectByVisibleText("15 years");       
        Select timeFrequency=new Select(d.findElement(By.xpath("//*[@id='FrequenceVersement']")));
        timeFrequency.selectByVisibleText("weekly");
        d.findElement(By.xpath("//*[@id='TauxInteret']")).clear();
        WebElement interestRate=d.findElement(By.xpath("//*[@id='TauxInteret']"));
        interestRate.sendKeys("5");
        d.findElement(By.xpath("//*[@id='btn_calculer']")).click();
        Thread.sleep(1000);
        String weekPayment=d.findElement(By.xpath("//*[@id='paiement-resultats']")).getText();
        System.out.println("printin weekely payment info : " + weekPayment);
        Assert.assertEquals(weekPayment, "$ 836.75");
       
    }
    @Test
    public void getScreenshot()
    {
        //take the screenshot of the entire home page and save it to a png file
                Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(d);
                try
                {
                    ImageIO.write(screenshot.getImage(), "PNG", new File("D:\\selenium_NEW_PRACTICE\\screenshots\\home"+System.currentTimeMillis()+".png"));
                }
                catch(IOException e)
                {
                    System.out.println(e.getMessage());
                   
                }
    }
   
    @AfterMethod
    public void tearDown()
    {
        d.close();
        System.out.println("closing bowser : ");
    }
    }

Get entire page screenshot in selenium


 - Download the ashot.jar file

package linkedIn_Interview;

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

import javax.imageio.ImageIO;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.OutputType.*;

import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.screentaker.ViewportPastingStrategy;
public class Question1 {

    static WebDriver d;
    @BeforeMethod
    public void setUp()
    {
        System.setProperty("webdriver.chrome.driver", "D:\\selenium_NEW_PRACTICE\\jars\\chromedriver_win32\\chromedriver.exe");
        d=new ChromeDriver();
        d.manage().window().maximize();
    }
    @Test
    public void verifyLinkedInExam() throws InterruptedException, IOException
    {       
        d.get("http://www.ia.ca");
        getScreenshot();  //add this method at where u want take screenshot
        //take the screenshot of the entire home page and save it to a png file
        //Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(d);
        //ImageIO.write(screenshot.getImage(), "PNG", new File("D:\\selenium_NEW_PRACTICE\\screenshots\\home"+System.currentTimeMillis()+".png"));
        d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
           @Test
    public void getScreenshot()
    {
        //take the screenshot of the entire home page and save it to a png file
                Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(d);
                try
                {
                    ImageIO.write(screenshot.getImage(), "PNG", new File("D:\\selenium_NEW_PRACTICE\\screenshots\\home"+System.currentTimeMillis()+".png"));
                }
                catch(IOException e)
                {
                    System.out.println(e.getMessage());
                   
                }
    }
   
    @AfterMethod
    public void tearDown()
    {
        d.close();
        System.out.println("closing bowser : ");
    }
    }

Monday, 16 May 2016

Working on window based popups using robot keys with Selenium

package interview;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
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 Window_Popups_Handling {

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

        WebDriver driver=new FirefoxDriver();
               
        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");
        Robot robot=new Robot();
        Thread.sleep(2000);
          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");

       
       
       
        //driver.close();
   
    }
   

}

How to select alternate Check box and select all check boxes using selenium

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;
public class CheckBox_Alternate_click
{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.flipkart.com/mobiles/apple~brand/pr?sid=tyy%2C4io&otracker=ch_vn_mobile_filter_Top+Brands_Apple");
driver.manage().window().maximize();
Thread.sleep(2000);
//List<WebElement> chkboxes = driver.findElements(By.xpath("//input[@type='checkbox']"));////input[contains(@class,'compare-checkbox')]
List<WebElement> chkboxes = driver.findElements(By.xpath("//input[contains(@class,'compare-checkbox')]"));//
for(int i = 0; i<chkboxes.size(); i=i+2)
{
   
chkboxes.get(i).click();
System.out.println("clicking check boxes : ");
}
driver.quit();
}
}

++++++++++++++++++++++++++++++++
public void verifyHandlingCheckBox()
{
    driver.get("http://toolsqa.com/automation-practice-form/?firstname=&lastname=&sex=Female&photo=&continents=Asia&submit=");
    //get and click select check boxes
      List<WebElement> checkBox=driver.findElements(By.cssSelector("input[name=profession]"));
   
    for(int i=0;i<checkBox.size();i++)
    {
         checkBox.get(i).click();
    }
    int checkedCount = 0;
    int uncheckedCount = 0;
    for(int i=0;i<checkBox.size();i++)
    {
        System.out.println(i + " : check box is selected" + checkBox.get(i).isSelected());
        if(checkBox.get(i).isSelected())
        {
            checkedCount++;
        }
        else
        {
            uncheckedCount++;
        }
    }
    System.out.println("number of selected checkbox: "+checkedCount);
    System.out.println("number of unselected checkbox: "+uncheckedCount);
}