Thursday, 30 July 2015

selenium-firefox-profile-for-saving-a-file - Download a file

http://stackoverflow.com/questions/12759256/selenium-firefox-profile-for-saving-a-file


    FirefoxProfile firefoxProfile = new FirefoxProfile();

    firefoxProfile.setPreference("browser.download.folderList",2);
    firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
    firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

    WebDriver driver = new FirefoxDriver(firefoxProfile);//new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);

    driver.navigate().to("http://www.myfile.com/hey.csv");
 
one more:
http://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver
 

Monday, 27 July 2015

Logger in Java - Using this we can generate seperate log files for each test script

package data;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;

public class PrintWriterMethods {
   
    public static String filename="";
    public static PrintWriter pw= null;
    public static File file =null;
    public static String resultsFolder = "src/results/";
   
    public static boolean startLogger()
    {
        boolean flag = false;
        try{
            Date dt= new Date();
            String dt1 = dt.toString().replace("-", "_").replace(":", "_").replace(" ", "");
            System.out.println(dt1);
            filename = "summary"+ dt1 + ".txt";
            file = new File(resultsFolder + filename);
            pw = new PrintWriter(file);                       
            flag = true;
        }
        catch(Exception oExp){
            System.out.println(oExp.getMessage());
        }
        return flag;
    }
   
    public static String getFileName(){
        return filename;
    }
   
    public static void logFailer(String message){
        Date dt= new Date();
        pw.append(dt.toString() + "--FAILURE--" + message + "\r\n");
       
    }
    public static void closeLogFile(){
        Date dt= new Date();
        pw.append(dt.toString() + "--END OF THE SCRIPT--");
        pw.close();
    }
    public static void logSuccess(String message){
        Date dt= new Date();
        pw.append(dt.toString() + "--SUCCESS--" + message + "\r\n");
    }
    public static void logWarning(String message){
        Date dt= new Date();
        pw.append(dt.toString() + "--WARNING--" + message + "\r\n");
    }  
   
}










package data;
public class SubClassTest {

    public static void main(String[] args) {
        PrintWriterMethods.startLogger();
        PrintWriterMethods.logFailer("Print the step Failed..");
        PrintWriterMethods.logFailer("Print the step Failed2..");
        PrintWriterMethods.logSuccess("Print the step Failed3..");
        PrintWriterMethods.logFailer("Print the step Failed4..");
        PrintWriterMethods.logWarning("Print the step Failed5..");
        PrintWriterMethods.closeLogFile();
        System.out.println(PrintWriterMethods.getFileName());       
    }
}



Output: summaryMonJul2717_15_46IST2015.txt

Mon Jul 27 17:15:46 IST 2015--FAILURE--Print the step Failed..
Mon Jul 27 17:15:46 IST 2015--FAILURE--Print the step Failed2..
Mon Jul 27 17:15:46 IST 2015--SUCCESS--Print the step Failed3..
Mon Jul 27 17:15:46 IST 2015--FAILURE--Print the step Failed4..
Mon Jul 27 17:15:46 IST 2015--WARNING--Print the step Failed5..
Mon Jul 27 17:15:46 IST 2015--END OF THE SCRIPT--

Wednesday, 15 July 2015

Selenium and XPath

.//div[a[i[@class='fa fa-info-circle']]]//following-sibling::div[2]

To navigate to path, use: /.. or parent_tag name[and child xpath] 
eg:

1.       .//div[a[i[@class='fa fa-info-circle']]]                parent_tag[]: takes to parent element

2.        //[@class='fa fa-info-circle']/../..                      /..               : takes to parent element.


Consider below is the html source code:
div
      a
         i class="fa fa-info-circle"



----------------------
Contains text using xpath:           $x("//p[@class='help-block']/a[contains(text(),'here')]")
OR
for exact text matching using xpath: $x("//p[@class='help-block']/a[text()='here']")

3. Any tag having specific attribute:

      driver.findElement(By.xpath(".//*[@id='account']/a")).click();
 

Sunday, 5 July 2015

Java Details used in reporting results - Selenium

package com.utilities;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class reporters {
    public static String getTime(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss a");
        String formatDate = format.format(date);
        return formatDate;
    }   
   
    public static String getDate()
    {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd:MMM:YYYY");
        String formatDate = format.format(date);
        return formatDate;
    }
   
    public static String getTimeStamp()
    {
//        java.util.Date today = new java.util.Date();
//        return new java.sql.Timestamp(today.getTime()).toString();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MMM-YYYY HH:mm:ssss a");
        String formatDate = format.format(date);
        return formatDate;
    }
   
    public static String getDateTime()
    {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd:MMM:YYYY HH:mm:ss a");
        String formatDate = format.format(date);
        return formatDate;
    }
   
    public static long currentMilliSeconds()
    {
        return System.currentTimeMillis();
    }
   
    public static void sleep(int seconds)
    {
        try{
            Thread.sleep(seconds);
        }
        catch(Exception oExp)
        {
            System.out.println(oExp.getMessage());
        }
    }
   
    public static String getExecutionTime(long seconds1, long seconds2)
    {   
        long duration = seconds2-seconds1;
        return String.valueOf(TimeUnit.MILLISECONDS.toSeconds(duration));
    }
   
    @SuppressWarnings("finally")
    public static String getIPAddress(){
        InetAddress addr = null;
        try{
            addr = InetAddress.getLocalHost();
            String hostname = addr.getHostName();
            System.out.println("Hostname: "+hostname);
            System.out.println("IP Address: "+addr.getHostAddress());
//            System.out.println("Local Host: "+addr.getLocalHost());
        }
        catch(Exception oExp)
        {
            System.out.println(oExp.getMessage());
        }
        finally{
            return addr.getHostAddress();
        }
    }
   
   
    public static void main(String args[])
    {       
        System.out.println("getTime: "+reporters.getTime());
        System.out.println("getDate: "+reporters.getDate());
        System.out.println("getTimeStamp: "+reporters.getTimeStamp());
        System.out.println("getDateTime: "+reporters.getDateTime());
        long l1= reporters.currentMilliSeconds();
        reporters.sleep(3500);
        long l2 = reporters.currentMilliSeconds();
        System.out.println("getExecutionTime: "+reporters.getExecutionTime(l1, l2));
        System.out.println(reporters.getIPAddress());
    }
   
     
}


getTime: 22:31:43 PM
getDate: 05:Jul:2015
getTimeStamp: 05-Jul-2015 22:31:0043 PM
getDateTime: 05:Jul:2015 22:31:43 PM
getExecutionTime: 3
Hostname: Adi-PC
IP Address: xxx.xxx.x.x
xxx.xxx.x.x

Time in seconds to execute scripts, IP Address and environment detials - JAVA

package test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;



public class SystemEg {

    public static void main(String[] args) throws UnknownHostException, InterruptedException {
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("os.arch"));
       
        InetAddress addr = InetAddress.getLocalHost();
        String hostname = addr.getHostName();
        System.out.println("Hostname: "+hostname);
        System.out.println("IP Address: "+addr.getHostAddress());
        System.out.println("Local Host: "+addr.getLocalHost());
       
       
        Date todaysDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss a");
        String formattedDate = formatter.format(todaysDate);
        System.out.println(formattedDate);
       
//        Calculate execution time in seconds..
        long iStartTime = 0;
        long iEndTime = 0;
        iStartTime = System.currentTimeMillis();
        Thread.sleep(3000);
        iEndTime = System.currentTimeMillis();   
        System.out.println(TimeUnit.MILLISECONDS.toSeconds(iEndTime-iStartTime));       
    }
}

/*
Output:
Windows 7
6.1
amd64
Hostname: IN22222
IP Address: xxx.xxx.x.x
12:23:46 PM
3
*/