Sunday, July 6, 2014

QTP : Important operations

QTP : Important operations





DATASHEET:


GLOBAL SHEET
A
B
C



3





1







LOCAL SHEET
A
B
C



a





b





c






To : Fetch only those cells from localsheet which  has been mentioned in Global sheet .ie., fetch only 3 and 1 cell in the same order from local sheet .
Output: ca
 
rowcount = DataTable.GetSheet("Global").GetRowCount  
for i=1 to rowcount step 1
DataTable.GetSheet("Global").SetCurrentRow(i)      
val=DataTable.Value("A","Global")                              '
DataTable.GetSheet("Local").SetCurrentRow(val)
msgbox (DataTable.Value("A","Local")
next

WEBTABLE
r=Browser("Google").Page("title:=.*").WebTable(“name:= TTable").RowCount 
c=Browser("Google").Page("title:=.*").WebTable(“name:=TTable").ColumnCount(r)
strData= Browser("Google").Page("title:=.*").WebTable(“name:=TTable").GetCellData(1,1)
Set ChldItm = Browser("Google").Page("title:=.*").WebTable(“name:=TestTable,"index:=0").ChildItem(1,1,"micclass",index)

if(ChldItm .GetRoproperty("micclass")<>"Page") then
.....
....
end if

EXCEL:
Set xl = createobject("excel.application")
xl.Visible = True
Set Wb= xl.Workbooks.Open("C:\qtp1.xls")
Set  ws=Wb.Worksheets("Sheet1")
Row=ws.UsedRange.Rows.Count

data = ws.cells(1,1).value

wb.saveas" "
wb.save
wb.close
xl.quit

DATABASE
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open"Driver={SQL Server};server=MySqlServer;uid=MyUserName;pwd=MyPassword;database=pubs"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects'Release objects
Set rs= nothing
Set con= nothing

FILES , FOLDERS
Set fso=createobject("Scripting.FileSystemObject")

Set ctrl__folder=fso.GetFolder(Sourcefolder)
Set  sub_folder=get_folder.SubFolders
Set  sub_files =get_folder.Files

for each i in sub_folder
               sub_folder.name
next

TEXT FILE
Set txt=fso.CreateTextFile("C:\qtptest.txt")
Set ctrl_file=fso.getfile("C:\qtptest.txt")
Set txt_file=ctrl.openastextstream(1)

Do while txt_file.AtEndOfStream <> true
Msgbox  txt_file.ReadLine 
Loop
----------------------------------------------------------------------------
·        mid("ABC",1,2)     'answer=AB
·        instr(1,"ABC","B") 'answer=2
·        len("ABC")     'answer=3
·        ubound(a,2)   ' a(2)(3) ---answer=3

Sunday, June 29, 2014

Selenium : Read ,Write and Edit Excel Sheet (Spread Sheet)

Excel Selenium : Read ,Write and Edit Excel Sheet (Spreadsheet)-- Final

Download the latest binary zip file below link and jars into your project
http://poi.apache.org/download.html


Code below(unlike jxl this should work for both xls and xlsx files) :

Workbook wb = WorkbookFactory.create(new FileInputStream("C:\\Users\\DPK\\Desktop\\test.xls"));
Sheet sheet = wb.getSheet("Sheet1");

System.out.println(sheet.getRow(0).getCell(0).getRichStringCellValue()); // Read Cell
sheet.createRow(1).createCell(1).setCellValue(1212); //---Write into a new cell
sheet.getRow(0).getCell(0).setCellValue("333"); //----Edit existing Cell
FileOutputStream fileOut = new FileOutputStream("C:\\Users\\DPK\\Desktop\\test.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("ok");

Saturday, June 28, 2014

Selenium :Headless Browser testing using HtmlUnitDriver and PhantomJS inWebdriver



Headless Browser testing using HtmlUnitDriver and PhantomJS inWebdriver

Headless Browser is a Web Browser without a GUI (Graphical User Interface). It access Web Pages but doesn't show them to any human being. Headless Browser should be able to parse JavaScript.

Html Unit Driver:

HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true); //to enable Javascript

To emulate a specific browser :

                   HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
 
The other uses a broader capabilities mechanism: 

                   HtmlUnitDriver driver = new HtmlUnitDriver(capabilities);
 
 

PhantomJS

PhantomJS is a Headless Webkit with JavaScript API. PhantomJS is an optimal solution for Headless Website Testing, Screen Capture, Access and manipulate webpages with the standard DOM API, or with usual libraries like jQuery, Network Monitoring PhantomJS comes with in-built GhostDriver.

Note : PhantomJS is not a Test framework, it is used only to LAUNCH the tests via a suitable Test Runner(GhostDriver) for webdriver.


Configure PhantomJS :
1.     PhantomJSDriver-1.0.x.jar can also be downloaded and configured in Eclipse manually.
2.     Add the following imports to your code:

import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;


3. Replace the object, "driver" specifying "FirefoxDriver" with "PhantomJSDriver".
4. Use
WebDriver driver = new PhantomJSDriver();

5. Run Test.

Note:
You can also do the following:
1. Download phantomjs.exe
2. Extract the phantomjs-1.8.x-windows.zip folder and locate phantomjs.exe file to C: / folder
-use the following code instead of the above:

DesiredCapabilities ph_cap = new DesiredCapabilities();
ph_cap.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe");
WebDriver driver = new PhantomJSDriver(ph_cap);

PhantomJS | Screen Capture

DesiredCapabilities ph_cap  = new DesiredCapabilities();
ph_cap.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://phantomjs.exe");
ph_cap .setCapability("takesScreenshot", true);
driver = new PhantomJSDriver(ph_cap );  
baseUrl = "http://www.xyz.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


@Test
public void test01() throws Exception {

driver.get(baseUrl + "/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\sample.jpeg"),true);
 

}


Sources :