Showing posts with label SeleniumTesting. Show all posts
Showing posts with label SeleniumTesting. Show all posts

Read/Write .xls and .xlsx with one set of code using POI - WorkbookFactory

As we discussed in earlier post Read/Write excel using Apache POI,
we have to use:
For handling .xls fileswe use HSSF (Horrible SpreadSheet Format) related POI classes.
For handling .xlsx files, we use XSSF (XML SpreadSheet Format) related POI classes.


On this post, how we can perform excel operations with one set code for both excel formats .xls and .xlsx.
For this we have a class provided from Apache POI WorkbookFactory, which auto detects appropriate kind of Workbook (HSSFWorkbook or XSSFWorkbook) depending on the excel formats .xls or .xlsx.
Let's jump to the code implementation

Internet Explorer IE11 - F12 (debugging) not working

There is a situation where, you installed IE11 freshly and try to open the debugging window by clicking F12, but shows error instead the HTML content,

here is the fix:

Download and install the below respective patch, restart and click on F12 to get the html content.

For 32bit system:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=45134

For 64bit system:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=45154

Hope this helps!

Download files in Chrome browser using selenium WebDriver

There are situations where we need to download files from browser, and save in a specified folder on hard disk.

Find below code to download files in Chrome browser:

public class DownloadXL {
       public static void main(String[] args) {
       System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
       String downloadFilepath = "c:/download";

  HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
  chromePrefs.put("profile.default_content_settings.popups", 0);
  chromePrefs.put("download.default_directory", downloadFilepath);
  ChromeOptions options = new ChromeOptions();
  HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
  options.setExperimentalOption("prefs", chromePrefs);
  options.addArguments("--test-type");
  DesiredCapabilities cap = DesiredCapabilities.chrome();
  cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
  cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
  cap.setCapability(ChromeOptions.CAPABILITY, options);
  driver = new ChromeDriver(cap);  
                driver.get("http://www.seleniumhq.org/download/");
                driver.findElement(By.linkText("32 bit Windows IE")).click();
        }
} 

Execute JavaScript in selenium to perform operations on HTML elements

Now we know how to identify html elements using only selenium or execute javascript in selenium,

Now let's see how we can perform actions on different web elements like
textbox - how we can enter text
Button - click
How to get attribute of a html element...

For input buttons
//js.executeScript("return document.getElementsByName('commit')[0].click();")
//or
//element = (WebElement) js.executeScript("return document.getElementsByName('commit')[0];");
//((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
//or
//element.click();

Execute javaScript (locators) in selenium to identify HTML element

We have seen how to identify web elements using selenium locators...there are situations where selenium alone can't able to identify web elements, there we can execute javaScript commands in selenium...

JavaScript has it's own locators to identify web elements:

getElementById - Returns single element that matches the ID.

Play with scroll bars on a page in selenium and javaScript

There are some situations in which we want to scroll to a particular element or scroll up or down across the browser.

Selenium do not have it's own methods to perform this action, so we can use javascript to play on scroll lbars.

Let's see some of the ways:

All about JavaScriptExecutor in Selenium

As per the selenium official link on Selenium RC, core selenium is basically a javascript program (set of functions) which interprets and executes selenium commands using browser's built in javascript interpreter.

Sometimes we observe web controls doesn't react well to selenium commands, there we can directly use / execute javascript commands using webdriver,

ways to drag n drop elements using selenium

There are several web applications that provides services for drag an element and drop on certain element on browser.

Some examples:
Can customize menu bar items by drag child elements on parent element.
Drag and drop files/folders in some hierarchy manner.

which one to use? driver.close() / .quit()

There are different ways to close browser[s] using selenium after tests are run.

Depending on scenarios, if single or multiple browsers are opened, we can close or quit browser.

let's see the usage of close, quit and dispose methods.


Selenium v2.47.0 - features added

Selenium framework leaunched it's updated version 2.47.0

Download link:

and here is below some of the main features of this version:

Selenium with Autoit (no installation) for win UIs

 As most of us know, Autoit is a tool to automate basic navigations for non browser / windows applications.

There are situations in selenium automation, we get window dialog or we can say non browser related dialog which can not be automated with selenium. e.g Upload a file, download a file etc..

We will see how we can integrate Autoit with Selenium without installing Autoit.


Selenium HTML result reporting using ExtentReports 2.X

While working with Selenium automation testing, we use different 3rd party frameworks (TestNG, Junit, MBunit, Nunit) in combination with Java or .NET to report the result at the end of the test execution or we use our own dashboard/html kind of reports,

While browsing different ways of reporting, got web links of ExtentReports, most of the links talk about the version 1.x,


Connect to DataBase in Java and Selenium Implementation

For initial understanding on database and SQL queries,
refer MySQL Basics

To automate database testing, we will use selenium with java language.

JDBC (Java DataBase Connectivity) is an API (using Java language) to connect and play with database with SQL queries.


Firefox profile and preferences in selenium

Firefox profile is basically the personal settings of the firefox browser.

Firefox profile contains information like your homepage, bookmarks, browser settings, history, saved passwords.

Profile is basically a specific folder stored locally in your hard drive other than your firefox installation folder.

We will see how we can set Firefox profile manually and then call in code or directly we can set the Firefox profile in code itself.


Selenium - ways to handle dropdown menus

Every web Page has at least one drop down menu bar, the bar may be horizontal or vertical.

Each item in the menu bar is a link corresponds to another page.

Menu bar have root items and in each root items we have one or more sub menus/items.

Actions on Root elements of a menu bar can be done as normal links, but sub elements are ( are hidden) only visible if we mouse hover to their corresponding root element,


selenium implicit + Explicit + fluent waits

Every browser application has it's own delay in loading the elements when we open a web application / navigate from one page to another / perform some action.

Loading depends on different criteria like internet bandwidth, technology used in the application, server capacity, no of users accessing the browser app etc...

While executing tests in different machines/environments, we need to make sure our script or code should wait till the elements load/present on the web page to perform some action upon them...

Selenium provides different ways to wait for an element on web page...

let's see them one by one -


TestNG @Test attributes - threadPoolSize, invocationCount, timeOut

Let's see invocationCount implementation

public class InvocationCnt {
 
      @Test(invocationCount=5)
      public void test1() throws InterruptedException
      {
   System.out.println("Thread ID: "+Thread.currentThread().getId());  
      }
}

OutPut:
Thread ID: 1
Thread ID: 1
Thread ID: 1
Thread ID: 1
Thread ID: 1
above method ran 5 times, a single thread will be assigned to run the method one by one..

Now let's see with threadPoolSize implementation

TestNG @Test attributes - AlwaysRun

alwaysRun is a testNG attribute which makes a test method run always even if it's dependent method is not run...

let's see the code implementation

public class AlwaysRun {
     WebDriver driver;
     @Test
     public void test1()
 {
    driver = new FirefoxDriver();
    driver.get("http:\\qavalidation.com");
    Assert.assertTrue(driver.getTitle().contains("Testing"));
 }
     @Test(dependsOnMethods = {"test1"}, alwaysRun=true)
     public void test2()
 {
     driver.findElement(By.linkText("Selenium Tutorial!")).click();
 }
}

Observe the output, even though test1 did not run, test2 executed.

TestNG @Test attributes - dependOnMethods

There are situations when we want to run our tests depends on the prior test execution like
if a particular test is run fine, then execute this test or else skip the test execution...
Basically we are creating inter dependencies in test execution.

Examples
Launch browser with URL, verify if title matches, then login
In Gmail, if login successful, then verify if Inbox link present or not
If report generated, then print the report

Let's see the code implementation

TestNG @Test attributes - enabled property

There are situations where we need to skip one or more @Test method in testNG,
for this TestNG provides enabled property to skip the execution of methods...

enabled = false (skip the method run)
enabled = true (execute method), it's by default set to true,

NOTE: if we do not mention enabled property, it's set to true

Let's see the code implementation