Explore the Latest in Tech & Automation: Android, iOS, Mobile Testing with Appium, Web Automation using Selenium, Python & Java Programming, Robot Framework, Shell & PowerShell Scripting, QA Tools, Spring Boot Development, Agentic AI Innovations, Free Software Resources, and Expert Tips on Designing Scalable Automation Frameworks.
Showing posts with label Selenium Webdriver. Show all posts
Showing posts with label Selenium Webdriver. Show all posts
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 casec:\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]
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;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.util.Properties;
publicclassReading_PropertyFile {
/* * @author : Qahumor Reading Properties file in Java */publicstaticvoidmain(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.
// 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"));
}
}
/* * @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.
importjava.util.List;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.firefox.FirefoxDriver;publicclassFrameHandlingInSelenium{publicstaticvoidmain(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.
*
*/
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.
importjava.io.File;importjava.io.IOException;importjava.text.DateFormat;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.apache.commons.io.FileUtils;importorg.openqa.selenium.OutputType;importorg.openqa.selenium.TakesScreenshot;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.firefox.FirefoxDriver;publicclassScreenshot_Demo_Selenium{/** * @param args<!-- ?xml version="1.0" encoding="iso-8859-1"? --> */publicstaticvoidmain(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();}}
/* * How to find the highlighed text on webpage. * Programming language : JAVA * * We can achieve this with the help og JavaScript indow.getSelection().toString() function * */
String hldText =(String)js.executeScript("return window.getSelection().toString();"); System.out.println("Highlighted Text on WebPage is : "+ hldText.trim());
/* * 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 */
// 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());
//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);
}elseif(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 "); }