Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to Use FindElement(s) to identify Elements in Selenium WebDriver

The objective of this article  is to understand object Identification in selenium for the basic login Page in the application.Let us assume we have username/Password available for website. In this example, We will click on Link for Login, provide username and Password and click on Login button and validate login is successful.


Since we have to perform action on different objects, we need to identify objects or elements in the Page based on properties of the object. We use findElement or findElements to find a particular element or a list of element satisfying the object description. 

The findElement() method returns a WebElement object based on object description and throws exception if it does not find any element matching the object description. To return a webelement object, below is the syntax:

WebElement elemlink = driver.findElement(By.linkText("log in"));

Once we have the webelement defined, we can perform required action on the element. E.g: clicking a link, inputting text in an edit box or get text of the object.
Methods to work on an object using Selenium

In this example, we find collection of all elements in list elemlnk which have tagName as a, i.e collection of all the links in the page. Once we have the list, we can validate if a particular link is available in the Page by comparing the text or validating for broken links in the page based on http response.List<WebElement> elemlnk = driver.findElements(By.tagName("a"));for (int i=0;i<elemlnk.size();i++)
{String strData = elemlnk.get(i).getText();}So now, since we are familiar with basic of WebElement and WebElements, We can go further with the original problem in the Page which was:In this article, we will click on Link for Login, provide username and Password and click on Login button and validate login is successful.

Code for the problem:public void LoginInSagenda() { try {//finding link in the page for login and clicking on the link. WebElement elemlink = driver.findElement(By.linkText("Log in")); elemlink.click(); new WebDriverWait(driver,40).until(ExpectedConditions.titleContains("Log in"));//once link is clicked, we will verify title of the new page if (driver.getTitle().contains("Log in")) { System.out.println("Title of the Page contains text :" + driver.getTitle();          } else { System.out.println("Title of the Page does not contains text : Log in"); } //define elements on which to perform action. Note we use different ways to identify object in page by using Id, name and xpath in the page WebElement usr= driver.findElement(By.id("Email")); WebElement pwd= driver.findElement(By.name("Password")); WebElement login = driver.findElement(By.xpath("//input[@value='Login']"));// perform required action in the objects identified. usr.sendKeys("abc@xyz.com"); pwd.sendKeys("abcxyz"); login.click();// dynamic wait until a particular condition is met new WebDriverWait(driver,40).until(ExpectedConditions.titleContains("Dashboard"));//once link is clicked, we will verify title of the new page if (driver.getTitle().contains("Dashboard")) { System.out.println("Title of the Page contains text :" + driver.getTitle());   } else { System.out.println("Title of the Page does not contains text : Dashboard");   } } catch (Exception e) { System.out.println(e.getMessage());
}
}

The findElements() method returns a list of WebElements matching the object description. If no elements are found, it returns an empty list. 



How to Launch Selenium WebDriver on different browsers

In this article, we will discuss on how we launch Selenium web driver in different browsers in Java. 

Before we go further with this, We need installing different jar files and making initial set up in Java, Please go through below link and other useful links to set up initial java project in eclipse with selenium jar files added to project build path.


Once we have configured eclipse and added required external libraries in the build path, we can start with a basic test in eclipse to solve following requirement: 
  It is required to perform following action on different browsers
  •         Open www.sagenda.net on browser specified by user
  •         Verify the title of the Page has Sagenda in the text.
  •      Close the browser.
Below is the self explanatory code in java using Selenium to perform the required job:


//These are the class libraries that needs to be imported
import java.io.File;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Iteration {

//Define Webdriver object

 WebDriver driver;

//This is the main method to drive the test flow  

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

//There are two part in the flow for which we created the required methods 

Iteration iter = new Iteration();

//a. Launching the required Browser

iter.LaunchBrowser("chrome");

//b. Providing the url of application and condition to verified in the test. Text in title in this example

iter.homePage("https://sagenda.net", "Sagenda");

}

//Function to launch required browser

public void LaunchBrowser (String strBrowser)

{

//based on the expected browser as provided in argument of method, we will launch the required browser

if (strBrowser.trim().toLowerCase().contentEquals("ie"))

{

//Launch IE browser://Pre-Conditions: Download IE driverServer.exe from the selenium download site and provide location where it is downloaded

 File file = new File("D:\\Selenium\\IEDriverServer.exe");

System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); 

driver = new  InternetExplorerDriver();

}


else if (strBrowser.trim().toLowerCase().contentEquals("firefox"))

{

driver = new  FirefoxDriver();

}

else if (strBrowser.trim().toLowerCase().contentEquals("chrome"))

{

//Pre-Conditions: Download IE driverServer.exe from the selenium download site and provide location where it is downloaded

    System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");

   DesiredCapabilities capabilities = DesiredCapabilities.chrome();

   ChromeOptions options = new ChromeOptions();

   options.addArguments("test-type");

//Provide location where chrome is currently installed.  

    String Chrome_Path = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";

   capabilities.setCapability("chrome.binary",Chrome_Path);

   capabilities.setCapability(ChromeOptions.CAPABILITY, options);

   driver = new ChromeDriver(capabilities);

}

else

{

//In case browser is not in above three , print below message 

System.out.println("This browser is currently not supported by me, Please select from IE/Firefox or chrome");

}

}

//Function to perform required action on the application

public void homePage(String url, String strTextinTitle)
{
try
{
//open the required url 
driver.get(url);
//once the url is launched, verify the title contains the required text.
//Note : In case we require exact match , we will use contentequals instead of contains
if (driver.getTitle().contains(strTextinTitle))
{
System.out.println("Title of the Page contains text :" + strTextinTitle );
}
else
{
System.out.println("Title of the Page does not contains text :" + strTextinTitle );
}
}
catch(Exception e)
{
System.out.println(e.getClass());
}
}
}

Understanding Union, Nested Queries and String function in SQL

In the previous article, we discuss on joins in SQL. In this post, we will start with nested query, and then understand how to use union, string function in SQL, and other topics that are useful for a tester to know.


   Problem 1: Let us start with a simple example. We need to know the name of user belonging to Organisations with org_code in Org003, Org004, Org005 using nested queries?


Answer: In this problem, we need to find the User_Name from User table but we need to create query to identify users which belongs to Organisations Org003, Org004, and Org005. Since User table is linked to organisation table by Org_Id. The Query should do something like. Select users from user table which belongs to Organisation having org_code as Org003, Org004, and Org005. This can be written in SQL using nested queries as follows:

Select User_Name from User where Org_Id in (Select Id from Organisation where Org_Code in (‘Org003’,‘Org004’,’Org005’)).


The above is an example of nested query, the same can be written using join as follows:

Select User_ from User us join Organisation org on us.Org_Id = org.Id where org.User_Code in(‘Org003’,‘Org004’,’Org005’)



Problem 2: Next problem is to find count of Organisation which pay salary to employees greater than 1000?


Answer: Salary is a field in User table and also we have org_Id in User table. And we are only concerned with the count of Organisation with salary greater than 1000. We can get the information from only User Table as:

Select count(distinct Org_Id) as OrganisationCount from User where Salary>1000;



Problem 3: Please explain the concept of union in SQL?


Answer: Union is used to combine data from multiple select queries in a record set. Number and data type of items in the select statement should match together.
e.g.: In above tables,
Select Id, Org_Name from Organisation Union Select Id, User_Name from User will fetch results

Select Id, IsActive from Organisation Union Select Id, User_Name from User will fail as the data type of items in the two select statements is not matching.

Select Id from Organisation Union Select Id, User_Name from User will fail as number of items in the two select statements is not matching.

So to union data from two or multiple select statements, the number of arguments, data type of argument, and order of arguments should match, else the union statement will fail.
Similar to Union is Union All. The difference between union and union All is while Union returns distinct record set ignoring duplicate record rows, Union All returns all the rows.


Problem 4: Suggest some useful string functions in SQL?


Answer: Some of useful SQL functions to work with string are:

UCASE() – Converts the string value into Upper Case.

Select UCASE(Org_Name) from Organisation

lcase() – Converts the string value into Lower Case.

Select lcase(Org_Name) from Organisation

Substring() – returns substring for column from start location and length as provided in argument. Similar to this in SQL Server, In Oracle we can Use Mid function.

Select substring(Org_Name,1,4) AS Org_4 from Organisation;

Use functions Substring and len to order by a specific part of a string

Select Org_Name from Organisation order by substring(Org_Name,len(Org_Name)-1,3)

Use + , we can concatenate multiple string and return the results.