MSBLOGS: April 2016

Pages

Monday, April 25, 2016

WebDriver: Script for opening a link in new tab.

[java]

WebElement link = getMyDriver().findElement(By.xpath("//*[@id=’link’]"));
Actions newTab = new Actions(getMyDriver());
newTab.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
Thread.sleep(5000);

//handle windows change
String base = getMyDriver().getWindowHandle();
Set<String> set = getMyDriver().getWindowHandles();

set.remove(base);
assert set.size() == 1;
getMyDriver().switchTo().window((String) set.toArray()[0]);

//close the window
getMyDriver().close();
getMyDriver().switchTo().window(base);

// handle windows change and switch back to the main window
Thread.sleep(2500);
for (String winHandle : getMyDriver().getWindowHandles()) {
getMyDriver().switchTo().window(winHandle);
}



First create a WebElement to the link. We need an Action chain to press Shift key and then click on the link which will be opened in a new tab of the same widow.
Now we have to handle the window change.
1. First set the main window to a String with getWindowHandle().
2. To get all the windows handle as a list use  "getWindowsHandles()"
Now we remove the base window from the list and assert that the list size is 1.
After this we need to switch to the new window with the WebDriver:
getMyDriver().switchTo().window((String) set.toArray()[0]);

To close the window and switch back to the main window, use the following steps:
getMyDriver().close();
getMyDriver().switchTo().window(base);

Just for sure it was switched to the main window use the following window handles:
for (String winHandle : getMyDriver().getWindowHandles()) {
getMyDriver().switchTo().window(winHandle);
}

Steps to open a link in a new tab and switch to it:

WebElement link = getMyDriver().findElement(By.xpath("//*[@id=’link’]"));
Actions newTab = new Actions(getMyDriver());
newTab.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).click(link).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).build().perform();
Thread.sleep(5000);

//handle windows change
String base = getMyDriver().getWindowHandle();
Set<String> set = getMyDriver().getWindowHandles();

set.remove(base);
assert set.size() == 1;
getMyDriver().switchTo().window((String) set.toArray()[0]);

//close the window and sitch back to the base tab
getMyDriver().close();
getMyDriver().switchTo().window(base);


If we want to open the link in a new tab than we need to use the following action chain:
newTab.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT) .click(link).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).build().perform();

It press CTRL+SHIFT keys then click to the link.
The other steps are similar with the steps we used in “switch-to-new-window” code above.

Monday, April 18, 2016

QA Automation: What is Data Driven Framework in Automation?

Data Driven Framework:

In this Framework , while Test case logic resides in Test Scripts, the Test Data is separated and kept outside the Test Scripts.Test Data is read from the external files (Excel Files, Text Files, CSV Files, ODBC Sources, DAO Objects, ADO Objects) and are loaded into the variables inside the Test Script. Variables are used both for Input values and for Verification values. Test Scripts themselves are prepared either using Linear Scripting or Test Library Framework.

Demonstration of Data Driven Framework:
Creating multiple Google user accounts is the task we will try to accomplish using this framework.
In the example earlier, we hard coded the first name, last name, user id details etc. into our code while trying to create an account. We will have to separate the code from the data if we have to achieve data driving aspect for this script.
The data should come from a source that is not the program itself.
Typically the data input can be anything:
  1. MS Excel files
  2. Data base
  3. Text files
  4. XML files….etc.
Excel files are the ones that get used most often. The very fact that each action in QTP comes integrated with a data sheet of its own explains why that’s so.
You can use one or more data sources for a single script. The excel sheet that comes with the action can be used or you can use an external excel sheet too. Basically a data sheet can be any relevant external file.

For example:

This is the code that we need to data drive:
1. Browser("Gmail: Email from Google").Page("GoogleAccounts").WebEdit("FirstName").Set "ms"
2. Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("LastName").Set "v"
3. Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("GmailAddress").Set "DataTest"



After parameterizing the hardcoded values it will look like :

1.   Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("FirstName").Set DataTable("G_First_Name", dtGlobalSheet)
2.   Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("LastName").Set DataTable("G_Last_Name", dtGlobalSheet)
3.   Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("GmailAddress").Set DataTable("gmail_address", dtGlobalSheet)


Now the data can be kept in csv, xml or any other excel file.

Parameterization is a process for creating Data Driven Framework. As we can see in Data driven framework the data are kept separately from the script. Similarly we can also add the Output data we are verifying using the script.