Sunday 29 December 2013

Selenium - How to highlight Element

It is always a good practice to highlight element before performing an operation. This helps us to know where the Selenium is currently focused or which element it is being executed.

There is no native way to do this, but because Selenium allows to execute JavaScript, it can be accomplished by  executing JavaScript method. Code is as follows -

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HighLightElement {

WebDriver driver = new FirefoxDriver(); //Launch firefox browser

@Test
   public void highlightTest() throws InterruptedException {
driver.get("https://www.facebook.com/"); // open facebook site
driver.manage().window().maximize(); // maximize browser
WebElement emailField = driver.findElement(By.xpath("//*[@id='email']")); //get
                reference to email text field
Thread.sleep(2000); //Stop program execution for 2 seconds
highlightElement(emailField); // call to highlight method


/** Method which executes Java Script code to highlight page object */
public void highlightElement(WebElement element) { 
   for (int i = 0; i < 2; i++) 
  { 
JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: red; border: 5px solid red;"); 
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); 
     } 

}

Saturday 28 December 2013

What is the difference between check point and output value


                   Checkpoint
                      Output Value
·        A checkpoint is a verification point that compares a current value for a specified property with the expected value for that property.
·        When the test is run, HP QTP compares the expected results of the checkpoint to the current results.
·        If the results do not match, the checkpoint fails & results of the checkpoint can be viewed in the Test Results window.
·        When Checkpoint is added, HP QTP adds a checkpoint with an icon in the test tree and adds a Checkpoint statement in the Expert View.
·        Checkpoint syntax - Window("Flight Reservation").WinComboBox("Fly To:").Check CheckPoint("Fly To:")
·        Insert -> Checkpoint -> Select the type of Checkpoint OR Right click the object in the Active Screen and select the type of checkpoint to be added.


·        An Output Value is used to retrieve the current value of any object in the application and stores it in a specified location. It can output values from text strings, table cells, databases, and XML documents. Unlike Checkpoints, no PASS/FAIL status is generated.
·        An output value is a value captured during the test run and entered in the run-time Data Table for use at another point in the test run.
·        When you create an output value, the test retrieves a value at a specified point during the test run and stores it in a column in the current row of the run-time Data Table.
·        When the value is needed later in the test run as input, HP QTP retrieves it from the Data Table.
·        You can output the property values of any objects.
·        Insert -> Output Value->Select the type of output value OR Right click the object in the Active screen and select the type of output value to be added.


Thursday 26 December 2013

Selenium - How to read URL of a link

Selenium code to get URL is as follows -

    1. Find the link as a Web Element using findElement() method

                      WebElement Lnk_URL =  driver.findElement(By.id("Lnk"));

    2. Get URL value using  "href" attribute
                      
                       String URL = Lnk_URL.getAttribute("href");

Monday 23 December 2013

What are the drawbacks of QTP

As of QTP version 10
·        Huge Tests in QTP consume lots of memory and increase CPU utilization.

·        Since QTP stores results in HTML file (and not txt) the result folder sometimes becomes big.

What is HP QTP Test Batch Testing tool

HP Quick Test Professional Test Batch Runner runs several tests in succession. Once the scripts are added in the tool, it will automatically open the scripts and start executing them one after the other.
·        To enable Test Batch Runner to run tests, you must select [Allow other HP products to run tests and components] in the Run pane of the Options dialog box.
·        Test Batch Runner can be used only with tests located in the file system. If you want to include tests saved in Quality Center in the batch run, you must first save the tests in the file system.
·        You can stop a test batch run at any time by clicking the Stop button.

To set up and run a test batch:
1.      From the Start menu, select Programs > Quick Test Professional > Tools > Test Batch Runner. The Test Batch Runner dialog box opens.
2.      Click the Add button or select Batch > Add. The Open Test dialog box opens.
3.      Select a test you want to include in the test batch list and click Open. The test is added to the list.
4.      If you want to save the batch list, click the Save button, or select File > Save, and enter a name for the list. The file extension is .mtb.
5.      When you are ready to run your test batch, click the Run button or select Batch > Run.