Showing posts with label Selenium Webdriver. Show all posts
Showing posts with label Selenium Webdriver. Show all posts

Sunday, September 17, 2017

Maven : How to configure Maven into your Windows Machine

Maven is a powerful tool that is used for projects build and dependency management. 
Let see how you can configure Maven into you Windows machine.

Step1 : Download the Maven binary from you offical site. 



Step2 : Extract the binary and save it in some location.
        Let say c:\users\qahumor\maven

Step3 : Now we have to set Environment Variable for Maven.
        Right-click on My Computer ->Properties -> Advanced System Settings.
        Under Advanced Tab -> Click on "Environment Variables".


Step4 : Then click on New in System Variables.

Step5: Provide Variable name = M2_Home and Variable value = path where you saved &
       extract the maven binary ,in this case c:\users\qahumor\maven
Step6 : Select Path variable in System Variables and click on Edit 
        (be careful while changing anything).

Step7 : Go to the end and enter value like - ;%M2_Home%\bin
        (make sure to put semi-colon incase if its not present) and hit OK.

Step8 : To check whether Maven is configured properly.
        Open command prompt and type these commands

       -> mvn -version  [To see the version of maven into your pc]
				&
       -> mvn clean  [This is command to clean the project , you will see Build Failure ,
                      this is expected as you don't have POM.xml file as of now]
		
Thank you!!

Java : How to read data from properties file

How to read data from properties file


The Properties class represents a persistent or defined set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object.

The Properties file is highly used in automation. Let see with an example.

package com.qahumor;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

public class Reading_PropertyFile {

 /*
  * @author : Qahumor Reading Properties file in Java
  */

public static void main(String[] args) {

// Create an Object of Properties class.
Properties prop = new Properties();

// Get the path of properties file which you have created in your project.
// System.getProperty("user.dir") - help to read the root directory of your project.
// Change the below path accordingly.
String path = System.getProperty("user.dir") + "/src/config/test.properties";

// Print the path of file for Debug purpose
System.out.println("Debug : Properties file path :" + path);

try {

// Reading the File input stream
 FileInputStream fs = new FileInputStream(path);

// Load the file into memory
 prop.load(fs);
   
} catch (Exception e) {
 e.printStackTrace();
}

// Read the value from Property file - test.properties
System.out.println("\nVersion of the Property file is : " 
    + prop.getProperty("version"));

}

}

Output:

Version of the Property file is : 3.1.4




@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

To create a property file:
1. You can open a notepad
2. Type below content in it.
    version=3.1.4
    login=admin
    pswd=xxxx
3. Save it name "test.properties" in double quotes.
4. You can download the sample property file from Link


Thank you!!





Saturday, September 2, 2017

How to create Firefox profile on Windows OS

How to create Firefox profile


Sometime we need to create different profile other than default profile 
of Firefox for new users or for any troubleshooting or for web automation.

To do this Firefox provide -P option to create new profile. 

Let's see how to achieve this.

You can watch Youtube video or follow the steps mentioned below






Step1: Make sure you close all the running firefox instance on your system.


Step2: Now press Windows+R to open RUN window.


Step3: Now type " firefox -P "  or "firefox.exe -P" and hit enter.



Step4: It will open "Firefox - Choose user Profile "  window.


Step5: Click on "Create Profile" option , it will open "Create Profile Window"
       then click  on Next button.


Step6: Provide the new profile name and choose the folder(if you would like to
       place the profile in some custom folder) and click on Finish.


Step7: Now you can see the newly created profile . Select and click on 
       "Start Firefox" button and navigate to folder (either default or custom
       folder) where you have placed the profile, you can see lots of files 
       got created in it.   






You can subscribe to my Youtube channel @ QAHUMOR


That's it , let me know if you have any comments.


Sunday, November 29, 2015

Selenium WebDriver : How to handle frame in webpage using selenium automation.

/*
 * @author : P programs
 * Selenium WebDriver : How to handle frame in webpage using selenium automation. 
 *
 */

An iframe is nothing but a webpage within a webpage.
Multiple iframe can be present in a webpage . To work or extract data from frame is not straight forward.We can't directly access iframe using code i.e we need to switch to frame before extracting any data or perform any operation.
We can use driver.switchTo().frame("pass_frame_index or frame_name") function to jump to particular frame in webpage.
Here is a self explanatory example in Selenium web driver.



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 FrameHandlingInSelenium {

 public static void main(String[] args) {

  
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_iframe_height_width");
  
// First of all we need to get the list of frames present in page. we can use iframe tagname as a locator. 
  List<WebElement> pageframeList =driver.findElements(By.tagName("iframe"));

// For debugging purpose , print the size of available frames.  
  System.out.println("Total list of frames ina page. size = " + pageframeList.size()); 

// Once you identified the desired frame where you want to retrieve data, switch to that frame   
  driver.switchTo().frame("iframeResult");

// In this example, we are again going to inner frame to retrieve data.
// Get the list of all frames inside the outer frame.  
  List<WebElement> frameIn =driver.findElements(By.tagName("iframe"));
  System.out.println("Inner frame size :-" +frameIn.size());

// Either you can switch to frame using frame name or frame index.Since in this example only one frame is present
// inside.so we are directly switching using index 0.   
  driver.switchTo().frame(0);

// Now print the text inside the frame 0 using valid xpath.  
  System.out.println(driver.findElement(By.xpath("html/body/h1")).getText());
  
  driver.quit();  
 }

}

Selenium WebDriver : How to take screenshot and save it to particular location with timestamp.

/*

Selenium WebDriver : How to take screenshot and save it to particular location with timestamp.

 *
 */

This is a simple utility program which helps to capture a screenshot in Web automation using Selenium WebDriver.

User can enhance this program to save output file with their own requirement of timestamp and the file format in which they want to save.



import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Screenshot_Demo_Selenium {

 /**
  * @param args<!-- ?xml version="1.0" encoding="iso-8859-1"? -->
  */
 public static void main(String[] args) throws IOException {

// Create browser instance, to know how to set and invoke different browser click here:
  WebDriver driver = new FirefoxDriver();  
  
  driver.get("https://twitter.com");   // provide the URL to browse
  driver.manage().window().maximize();  // maximize the browser
  
  //Java class which formats date and time in a given manner
  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
  Date date = new Date();
  
  String fileName = (dateFormat.format(date)).toString();
  System.out.println(fileName);
  
  
  //Using below function TakesScreenshot which is type-casted to driver whose output type is File.
  File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
  
  //Using copyFile function which take 2 parameters , sourceFile and destination file( where you can provide the path with the file extension )
// System.getProperty("user.dir") - provide the current directory path.
  FileUtils.copyFile(sourceFile, new File(System.getProperty("user.dir")+"\\screenshots\\"+fileName+".jpg"));
  
  //quit() is a selenium function which close and quits the driver.
  driver.quit();
  
 }

}

Sunday, June 7, 2015

Selenium WebDriver : How to find the highlighed text on webpage.





/*
* How to find the highlighed text on webpage.
* Programming language : JAVA
*
* We can achieve this with the help og JavaScript indow.getSelection().toString() function
*
*/
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class HighlightedTextOnWebPage {

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

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = null;

driver.get("https://google.com");
Thread.sleep(5000);
// Try to highlight any text on webpage using the mouse.


if (driver instanceof JavascriptExecutor) {
js = (JavascriptExecutor)driver;
}

String hldText = (String)js.executeScript("return window.getSelection().toString();");
System.out.println("Highlighted Text on WebPage is : " + hldText.trim());

}
}

Selenium WebDriver : How to handle PopUp and close it


/*
* Pop-up handling in Selenium WebDriver
* Programming language : JAVA
*
* Here we will browse Rediff site and close the pop-up .
* We will print the pop-up window URL and the main window URL
*/

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PopUpHandling {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();

// for chrome browser setting
//System.setProperty("webdriver.chrome.driver", "D:/setup/chromedriver.exe");
//WebDriver driver = new ChromeDriver();

driver.get("http://www.rediff.com/");

// Since the Window Handles are unique , we will use java.util.Set to store as it does allow the duplicate values.
// Windows Handles is of type String, so we store the value of window handle as String in Set
Set<String> winHandle = driver.getWindowHandles();

// just to check the number of total windows , in this case its 2
System.out.println("Total number of Windows : "+ winHandle.size());

// Iterator is use to iterator through Set , we can use for loop also
Iterator<String> it = winHandle.iterator();

String parentWindow = it.next();


String childWindow = it.next();

// switch to child window / pop-up window
driver.switchTo().window(childWindow);

System.out.println("child URL/ Title : " + driver.getCurrentUrl() );
driver.close(); // close the child /popUp window

driver.switchTo().window(parentWindow);
System.out.println("parent URL/ Title : " + driver.getCurrentUrl() );

}

}

Selenium WebDriver : Example to set and invoke different browsers through WebDriver


/*
* Example to set and invoke different browser in Selenium WebDriver
* Programming Language : JAVA
*/
import java.util.Scanner;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class SettingDifferentBrowserinSelenium {

static WebDriver wd = null;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter browser which you want to invoke : ");
String browserName = sc.next();
openBrowser(browserName);

}


public static void openBrowser(String browserName){

if(browserName.equalsIgnoreCase("Mozilla")){
wd = new FirefoxDriver();
System.out.println("Browser invoked : " +browserName);

}else if(browserName.equalsIgnoreCase("Chrome")){

//for chrome browser you have to set the path
System.setProperty("webdriver.chrome.driver", "/path_to_chormedriver/chromedriver.exe"); // D:/setup/chromedriver.exe
wd = new ChromeDriver();
System.out.println("Browser invoked : " +browserName);

}else if(browserName.equalsIgnoreCase("Chrome")){

//for ie browser you have to set the path , use the IEDriverServer.exe based on your OS architecture x64bit or x86bit
System.setProperty("webdriver.ie.driver", "/path_to_iedriver/IEDriverServer.exe"); // D:/setup/IEDriverServer.exe
wd = new InternetExplorerDriver();
System.out.println("Browser invoked : " +browserName);

}else{

System.out.println("You can either invoke the default browser or display"
+ " a message to provide valid broswer name ");
}

}

}

Robot Framework : Sample code to verify if Robot Framework is installed properly ? - part2

Sample code to verify if Robot Framework is installed properly ? 1. Create a file name test.robot and save it. *** Test Cases *** Sample ...