Sunday, 13 September 2015

Selenium - Capture videos

Configuration

Step 1 : Navigate to the URL - http://www.randelshofer.ch/monte/index.html and download the screen recorder JAR.

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

import org.apache.commons.io.FileUtils;

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

import org.monte.media.math.Rational;
import org.monte.media.Format;
import org.monte.screenrecorder.ScreenRecorder;

import static org.monte.media.AudioFormatKeys.*;
import static org.monte.media.VideoFormatKeys.*;


public class webdriverdemo
{
   private static ScreenRecorder screenRecorder;
   public static void main(String[] args) throws IOException, AWTException
   {
      GraphicsConfiguration gconfig = GraphicsEnvironment
         .getLocalGraphicsEnvironment()
         .getDefaultScreenDevice()
         .getDefaultConfiguration();
      
      screenRecorder = new ScreenRecorder(gconfig,
         new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey,
         MIME_AVI),
         new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,
         ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
         CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
         DepthKey, (int)24, FrameRateKey, Rational.valueOf(15),
         QualityKey, 1.0f,
         KeyFrameIntervalKey, (int) (15 * 60)),
         new Format(MediaTypeKey, MediaType.VIDEO,
         EncodingKey,"black",
         FrameRateKey, Rational.valueOf(30)), null);
      
      WebDriver driver = new FirefoxDriver();
      
      // Start Capturing the Video
      screenRecorder.start();
      
      // Puts an Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
      // Launch website
      driver.navigate().to("http://www.calculator.net/");
      
      // Maximize the browser
      driver.manage().window().maximize();
      
      // Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();
      
      // Click on Percent Calculators
      driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();
      
      // Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");
      
      // Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");
      
      // Click Calculate Button
      driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();
      
      // Get the Result Text based on its xpath
      String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();

      File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(screenshot, new File("D:\\screenshots\\screenshots1.jpg"));

      // Print a Log In message to the screen
      System.out.println(" The Result is " + result);
      
      // Close the Browser.
      driver.close();
      
      // Stop the ScreenRecorder
      screenRecorder.stop();
   }
}

Log4j file work.

Jar : log4j-1.2.17.jar to be add.
log4j.xml  :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

   <appender name="fileAppender" class="org.apache.log4j.FileAppender">
      <param name="Threshold" value="INFO" />
      <param name="File" value="NewGenerated
LogFile.log"/>
      <param name="Append" value="flase" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}  [%c] (%t:%x) %m%n" />
      </layout>
   </appender>

   <root>
   <level value="INFO"/>
      <appender-ref ref="fileAppender"/>
   </root>
  
</log4j:configuration>


Log4j.java
package testing;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.junit.Test;

public class Log4j {
    static final Logger logger = LogManager.getLogger(Log4j.class.getName());
   
    @Test
    public void logTest(){
        DOMConfigurator.configure("log4j.xml");
       
        logger.info("# # # # # # # # # # # #  # # # # # # # # # # # # # ");
        logger.info("TEST Has Started");
    }
}

Selenium - Find all Links

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class getalllinks {

   public static void main(String[] args){
  
      WebDriver driver = new FirefoxDriver();
      driver.navigate().to("http://www.calculator.net");
      java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
      System.out.println("Number of Links in the Page is " + links.size());
     
      for (int i = 1; i<=links.size(); i=i+1) {
         System.out.println("Name of Link# " + i - + links.get(i).getText());
         }
      }
   }

Selenium - Multi Select Action

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

public class webdriverdemo {

   public static void main(String[] args) throws InterruptedException {
  
      WebDriver driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

      driver.navigate().to("http://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx");

      //driver.manage().window().maximize();
      driver.findElement(By.id("ContentHolder_lbSelectionMode_I")).click();
      driver.findElement(By.id("ContentHolder_lbSelectionMode_DDD_L_LBI1T0")).click();
      Thread.sleep(5000);
     
      // Perform Multiple Select
      Actions builder = new Actions(driver);
      WebElement select = driver.findElement(By.id("ContentHolder_lbFeatures_LBT"));
      List<WebElement> options = select.findElements(By.tagName("td"));
     
      System.out.println(options.size());
      Action multipleSelect = builder.keyDown(Keys.CONTROL).click(options.get(2)).click(options.get(4)).click(options.get(6)).build();
     
      multipleSelect.perform();
      driver.close();
   }
}

Selenium - Drag & Drop

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

public class webdriverdemo {

   public static void main(String[] args) throws InterruptedException{
  
      WebDriver driver = new FirefoxDriver();
      //Puts a Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     
      // Launch website
      driver.navigate().to("http://www.keenthemes.com/preview/metronic/templates/admin/ui_tree.html");
      driver.manage().window().maximize();
     
      WebElement From = driver.findElement(By.xpath(".//*[@id='j3_7']/a"));
      WebElement To = driver.findElement(By.xpath(".//*[@id='j3_1']/a"));
     
      Actions builder = new Actions(driver);
      Action dragAndDrop = builder.clickAndHold(From).moveToElement(To).release(To).build();
     
      dragAndDrop.perform();
      driver.close();
      }
   }