Skip to main content

Posts

Showing posts with the label Selenium Webdriver Examples

Different types of waits in Selenium

Synchronization: Waits in Selenium                                      Synchronization: Waits in Selenium Value of Automation testing lies in execution. You automate a test case because you want to execute it multiple times saving your time and manual efforts. But sometimes your test cases failed due to network speed, due to slow rendering of Ajax and JavaScript etc. We need to overcome these challenges in our automated scripts. Suppose, you entered username and password, you clicked on submit button and now you will be navigated to home page. But, here comes the challenge. If you do not synchronize your script, your script will not wait for the next page and you will get NoSuchElementException. WebDriver provides a good to use wait mechanism to solve these problems. There are 3 types of waits that Selenium provide: Implicit Wait Explicit Wait Fluent Wait So, let’s start to know more about th...

POM in Selenium using Java and testNG

===================================================================  Below is the sample code for Page object model with page factory pattren. =================================================================== There are 3 classes as mentioned below: 1) Base Class: which contains all constants like page url, username, passord, filename etc. 2) Page class: which acts like object/webelement repositories like xpath/id/name or webelemnt objects instantiations and functions which have logic to do the operations on webelemnts. 3) test case class: this will actually perform the operations on webelements. Like enter data to textboxes, cliscking on a button, select dropdown values. =================================================================== Base class: below base class have a url, username and password are assigned. =================================================================== package POM; public class Constant {   //URL that need to be test final static String ...

Web Scrapping using Jsoup , Selenium and Java

 Web Scrapping using Jsoup , Selenium and Java In this article we will see how to do web scrapping using Jsoup and Selenium using Java. Jsoup Jar you can download from below links: https://jsoup.org/download http://www.java2s.com/Code/Jar/j/Downloadjsoup160jar.htm Below is the full code to do web scrapping and write the data to text file. In the following example we are navigating to a web which has a drop down with 12 values. For each value we select and clieck on a search button it will navigate to a page for which we have to scrape a data which is spread on multiple page. So we will srcrap all those data present on a multiple page. Navigate back to home page(page from which we have selected the dropdown value and cliecked on a search button) then again select next dropdown value and repeat the same procedure. This will repeat till we reach to the end of a dropdown values i.e. for all 12 dropdown values. Kindly please do comment when you find this as working for you. import java....

Excel to PDF converter in Selenium with java Demo

package excel2pdfDemo; import java.io.FileInputStream; import java.io.*; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.ss.usermodel.*; import java.util.Iterator; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; public class Excel2pdf {          public static void main(String[] args) throws Exception{                 FileInputStream input_document = new FileInputStream(new File("D:\\APCH.xls"));                 // Read workbook into HSSFWorkbook                 HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);                 // Read worksheet into HSS...

How to delete a component in Selenium using java

Sometimes we want to delete a component that we don't needed in order to automate the application. Below is the code that will work for you.   For further queries or any suggestions from your side are most welcome. Thanks! JavascriptExecutor js = null;         if (driver instanceof JavascriptExecutor) {             js = (JavascriptExecutor) driver;         }         js.executeScript("return document.getElementById('ID of a Component which you want to delete').remove();");  

Program to find most recent file in a folder

Below is the function used to find the most recent or last modified file in a directory. Try it and see the result. Enjoy!!! public File getTheNewestFile(String filePath, String ext) {                    File theNewestFile = null;                    File dir = new File(filePath);                    FileFilter fileFilter = new WildcardFileFilter("*." + ext);                    File[] files = dir.listFiles(fileFilter);                    if (files.length > 0) {                        /** The newest file comes first **/        ...

Display a Hidden Button(Component) in Selenium using Java

In some cases if a button is hidden on a web page. We can enable or display that button using following method: Here is the HTML code represents the button is hidden on web page. input type="submit" name="HiddenButton" value="Download" onclick="return checknextpage();" language="javascript" id="HiddenButton" class="button" style="width:100px;display:none;" Here is the java code to display or enable that button:             JavascriptExecutor js = (JavascriptExecutor) driver;    WebElement element = driver.findElement(By.id("HiddenButton"));    js.executeScript("arguments[0].setAttribute('style', 'width:100px')",element);

Switch to window in Selenium using Java

If you want to close the current active window and pass the control to another window using selenium. Below is the java code that will work for you. String currentWindow = driver.getWindowHandle();         // switch to first window that is not equal to the current window         String newWindow = null;         for ( String handle : driver.getWindowHandles()) {             if (!currentWindow.equals(handle)) {                 newWindow = handle;                 break;             }         }         // if there's another window found...         if (newWindow !=...

Read a PDF file and write the content of PDF into text file

Program to read a PDF file and write the content of PDF into text file using itext 5.3.5 library. Here Each page of a PDF is written to a separate text file. Ex. First page of PDF is written to first txt file. Second page of a PDF is written to second text file and so on. First you need to download the library then import it into your project. Here is the source code. Enjoy programming!!! import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.parser.PdfTextExtractor; /**  * This class is used to read an existing  *  pdf file using iText jar.  * @author javawithease  */ public class PDFReadExample {     public static void main(String args[]){                 BufferedWriter bw = null;         FileWriter fw = null;     ...

Create PDF reports in Selenium and Java

Sometimes we need to create PDF reports as we do crystal reports in .Net. Same thing can be achieved through iText library. Following program creates a PDF file and adds a text into it. In order to achieve this you need to download the iText library and import it's jar file into your program. Here is the link to download: iText JAR import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class iTextPDFDemo { public void iTextPDF() throws Exception{ String FILE = "D:/MyiText.pdf"; Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); document.add(new Paragraph("Now I can be able to write into the world of PDF ")); document.close(); } public static void main(String args[]){ iTextPDFDemo get = new iTextPDFDemo(); try { get.iTextPDF(); } catch (Exce...