YONGFEIUALL

izheyi.com


  • Home

  • Archives

  • Categories

  • Tags

  • About

  • Search

QTP Recovery Scenario Specification

Posted on 2014-10-09 | In QTP |

When we run script under our new framework, we found that will missing the “FrameworkErrorHandler.vbs” in Recovery Scenario.

Issue:
For the better transplantation, we use the relevant path for all items in new framework, but once we generate the QRS file, the relevant path will change to absolute path, and we can’t modify this path any more.

Solution:
Now that this file is change to absolute path, and need to in keeping with relevant path in framework, I thought a way to specify a path, and generate the “FrameworkErrorHandler.vbs” dynamically:

Before we run the test cases, copy the “FrameworkErrorHandler.vbs” in framework to root directory of C disk.

1
2
3
Set fso = CreateObject( "Scripting.FileSystemObject" )
fso.CopyFile oParentFolder + "FrameworkErrorHandler.vbs", "c:\FrameworkErrorHandler.vbs"
fso.GetFile("c:\FrameworkErrorHandler.vbs").Attributes = 0 'set file attribute to nomal

No matter you put the framework in anywhere of your PC, QRS file can find and read the custom recovery scenario function file successfully.

Run VBS file with command line and pass parameter

Posted on 2014-10-09 | In VBS |

VBS Code:

1
2
3
4
5
Set args = WScript.Arguments
If args.Count = 1 Then
test= WScript.Arguments(0)
MsgBox "test " & name
End If

Save this code in test.vbs file.

Command Code:

1
wscript.exe test.vbs helloworld

The result will be “test Helloworld”.

定制元素属性检查,并写到ReportNG中

Posted on 2014-09-20 | In Selenium Webdriver |

QTP 和Selenium 都会有这种要检查某一个控件元素属性的情况,比如去检查一个Button的显示文字是什么?

为了更方便的书写程序,并优美的显示到HTML测试报告中,做了以下几个小小的封装,只是小小的尝试,让大家做个参考,抛砖引玉了。。

脚本实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void verifyAttribute(WebElement we, String weName, String propertyName, String expectValue)
{
String actualValue = we.getAttribute(propertyName);

log("INFO", "Verify {" + propertyName + "} attribute for element [" + weName + "]");

if (actualValue.equals(expectValue)){
Reporter.log("<TABLE border='1'><TR><TD>Actual Value</TD><TD>"+actualValue+"</TD></TR><TR><TD>Expected Value</TD><TD>"+expectValue+"</TD></TR><TR><TD>Checkpoint Status</TD><TD style='background-color:green'><b>Passed</b></TD></TR></TABLE>");

}
else {
Reporter.log("<TABLE border='1'><TR><TD>Actual Value</TD><TD>"+actualValue+"</TD></TR><TR><TD>Expected Value</TD><TD>"+expectValue+"</TD></TR><TR><TD>Checkpoint Status</TD><TD style='background-color:red'><b>Failed</b></TD></TR></TABLE>");
handleFailure("Error Case Research: ");
}

Assert.assertEquals(actualValue, expectValue);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void log(String logType, String logInfo){
System.setProperty("org.uncommons.reportng.escape-output", "false");
System.setProperty("com.testng.reporter.escape-output", "false");

switch(logType){
case "INFO":
Reporter.log(logTime + "&nbsp;&nbsp;&nbsp;<font color='gray'><strong>INFO&nbsp;&nbsp;&nbsp;</strong></font>" + logInfo);
break;
case "PASS":
Reporter.log(logTime + "&nbsp;&nbsp;&nbsp;<font color='green'><strong>PASS&nbsp;&nbsp;&nbsp;</strong></font>" + logInfo);
break;
case "FAIL":
Reporter.log(logTime + "&nbsp;&nbsp;&nbsp;<font color='red'><strong>FAIL&nbsp;&nbsp;&nbsp;</strong></font>" + logInfo);
break;
}
}

这里只是个例子,要区别一下getAttribute和getText。

Read more »

Selenium如何处理Table

Posted on 2014-08-08 | In Selenium Webdriver |

由于webdriver中没有专门的table类,所以我们需要简单的封装出一个易用易扩展的Table类来帮助简化代码。

以下是我之前用Java语言来实现的一个简单的封装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

/**
* <b>Name:</b> WebTable</br> <b>Description: </b>This class is used to handling
* web table</br>
*
* @author <i>Yongfei Hu</i>
*/

public class WebTable {

private WebElement webTable;
StartingSelenium ss = new StartingSelenium();

public WebTable(WebElement webTable) {
this.webTable = webTable;
}

public WebElement getWebTable() {
return webTable;
}

/**
* <b>Name: getTableRows</b> <b>Description:</b> This method is to get table
* rows list
*
* @return tableRows
*/
private List<WebElement> getTableRows() {
List<WebElement> tableRows = webTable.findElements(By.tagName("tr"));
return tableRows;
}

/**
* <b>Name: getTableColumns</b> <b>Description:</b> This method is to get
* table columns list
*
* @param currentRow
*
* @return tableColumns
*/
private List<WebElement> getTableColumns(WebElement currentRow) {
List<WebElement> tableColumns = currentRow.findElements(By
.tagName("td"));
return tableColumns;
}

/**
* <b>Name: getRowCount</b> <b>Description:</b> This method is to get the
* table row count
*
* @return rowCount
*/
public int getRowCount() {
int rowCount = getTableRows().size();
Reporter.log("Rows in the table: " + rowCount, true);
return rowCount;
}

/**
* <b>Name: getColumnCount</b> <b>Description:</b> This method is to get the
* table column count
*
* @return columnCount
*/
public int getColumnCount() {
WebElement headerRow = getTableRows().get(0);
List<WebElement> tableColumns = getTableColumns(headerRow);
int columnCount = tableColumns.size();
Reporter.log("Column in the table: " + columnCount, true);
return columnCount;
}

/**
* <b>Name: getCellText</b> <b>Description: </b> This method is to get the
* specific cell data
*
* @param row
* @param column
* @return cellText
*/
public String getCellText(int row, int column) {
WebElement currentRow = getTableRows().get(row);
List<WebElement> tableColumns = getTableColumns(currentRow);
return tableColumns.get(column).getText();
}

/**
* <b>Name: getRowByCellText</b> <b>Description:</b> This method is to get
* the row number by the cell data
*
* @param cellText
*
* @return rowNumber
*/
public int getRowByCellText(String cellText) {
int rowCount = getRowCount();
int colCount = getColumnCount();
int getRow = 0;
boolean flag;

for (int i = 0; i < rowCount; i++) {
flag = false;

for (int j = 0; j < colCount; j++) {
if (getCellText(i, j).equals(cellText)) {
getRow = i;
flag = true;
Reporter.log("The text: " + cellText + " in row number: "
+ getRow, true);
break;
}
}
if (flag) {
break;
}
}
return getRow;
}

/**
* <b>Name: clickLinkInCell</b> <b>Description: </b> This method is to click

* link in the specific cell
*
* @param row
* @param column
* @param index
*
*/
public void clickLinkInCell(int row, int column, int index) {
WebElement currentRow = getTableRows().get(row);
WebElement currentCell = getTableColumns(currentRow).get(column);
currentCell.findElements(By.tagName("a")).get(index).click();
Reporter.log("Clickin a link on the row: " + row, true);
}

/**
* <b>Name: clickLinkInCell</b> <b>Description: </b> This method is to click
* link in the specific cell
*
* @param row
* @param column
* @param index
* @param linkName
*
*/
public void clickLinkInCell(int row, int column, int index, String linkName) {
WebElement currentRow = getTableRows().get(row);
WebElement currentCell = getTableColumns(currentRow).get(column);
WebElement we = currentCell.findElements(By.tagName("a")).get(index);
ss.clickUsingJS(we, linkName);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

dataProvider实现数据驱动

Posted on 2014-07-10 | In Selenium Webdriver |

废话不多讲,直接进主题,怎么实现用Excel配置测试数据,用dataProvider来调用测试数据。

jxl目前来看只支持.xls格式的文件,所以我们采用Apache POI来实现对.xlsx的操作,详细信息请参见:POI

数据准备

创建数据文件,并写入内容

Test Data

此处我们只是做个实验,把文件放在了C盘下,在实际的项目中可以放在整个项目中。

Read more »

Selenium Highlight页面元素

Posted on 2014-06-01 | In Selenium Webdriver |

大家都知道QTP的对象高亮显示功能特别强大, Selenium Webderiver也可以实现此功能。

高亮显示有时候对Debug还是相当有用的。

解决脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// <Summary> 
/// Highlight WebElement
/// </Summary>
public void highlightElement(WebDriver driver, WebElement element) {
if(element.isDisplayed())
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("element = arguments[0];" +
"original_style = element.getAttribute('style');" +
"element.setAttribute('style', original_style + \";" +
"background: yellow; border: 2px solid red;\");" +
"setTimeout(function(){element.setAttribute('style', original_style);}, 1000);", element);
}
else
{
System.out.println("Can't idnetify the object: " + element);
}
}

调用脚本

1
2
3
driver.get("http://ww.baidu.com");
WebElement button = driver.findElement(By.id("su"));
hilightElement(driver, button);

结果显示

Highlight Result

ReportNG替换TestNG默认结果报告

Posted on 2014-05-23 | In Selenium Webdriver |

TestNG默认的报告虽然内容挺全,但是展现效果却不太理想,不易阅读。因此我们想利用ReportNG来替代TestNG默认的report。

什么是ReportNG呢?这里不多说,请直接参见ReprotNG

要替换默认报告,我们需要做以下操作:

Read more »

Prop.Properties配置测试应用的环境和其他配置项

Posted on 2014-05-20 | In Selenium Webdriver |

prop.propertiesfile contains important info that needs to be changed before the test is run, such as: Browser type (browser =), Product (test-prod). Depend your test setup; you may not need the test-env and other configurations.

定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Browser type
# 1 => FireFox
# 2 => IE
# 3 => Chrome
# 4 => Safari
BrowserType = 2

# Test server and login information
BaseURL = http://www.site.com/
UserName =
UserPwd =

# UI actions' timeout in millisecond
Timeout = 30000

提取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class GlobalSetting
{

public static Properties prop = getProperties();

public static String baseURL = prop.getProperty("BaseURL");
public static int browserType = Integer.parseInt(prop.getProperty("BrowserType"));

public static String getProperty(String property) {
return prop.getProperty(property);
}

public static Properties getProperties() {
Properties prop = new Properties();
try {
FileInputStream file = new FileInputStream("prop1.properties");
prop.load(file);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
return prop;
}
}

#引用

1
2
3
4
@Test
public void doTest(){

System.out.Println(GlobalSetting.baseURL);
}

Selenium抓图并保存到TestNG报告中

Posted on 2014-05-19 | In Selenium Webdriver |

这里不讲解怎么在Eclipse安装配置TestNG,网上一搜一大把,大家自己去实践一下。
在这里主要说一下用Java来实现Selenium Webdriver的截图功能和把截图写到TestNG的报告中。

Capture screenshot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Capture screenshot
public String captureScreenShot()

{
String dir = "screenshot";
String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
String time = new SimpleDateFormat("HHmmss").format(new Date());
String screenShotPath = dir + File.separator + date + File.separator + time + ".png";

try
{
File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File(screenShotPath));
screenShotPath = screenShotPath.substring(screenShotPath.indexOf("\\"));
}
catch(IOException e)
{
screenShotPath = "Failed to capture screenshot: " + e.getMessage();
}
return screenShotPath;
}

Write to TestNG

1
2
3
4
5
6
public void writeToTestNG(String proMessage) {

String png = captureScreenShot();
Reporter.log("[" + logTime + "] " + proMessage);
String log = new File("screenshot").getAbsolutePath();
Reporter.log("<br/><img src=\"" + log + "/" + png + "\" />");

Call function

1
2
3
4
5
6
7
8
9
@Test
public void search()
{

openURL();
BaiduSearch yy = new BaiduSearch(driver);
yy.searchFor("searchTest");
writeToTestNG("testing ");
driver.quit();
}

Result

TestNG

IE浏览器Zoom和Protected Model Setting

Posted on 2014-04-04 | In Selenium Webdriver |

Selenium Webdriver在IE下跑脚本的时候要保证页面大小为100%,且要在IE internet options, selectSecurity tab and uncheck “Enable Protected Mode” for all security zone.

为了不手动来做这些设置,我们可以在Launch IE Driver的时候控制,如下:

1
2
3
4
5
6
7
8
9
10
System.setProperty("webdriver.ie.driver", "resource\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();

//zoom
ieCapabilities.setCapability("ignoreZoomSetting", true);

//Security
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

WebDriver driver = new InternetExplorerDriver(ieCapabilities);

1…333435…40
唐胡璐

唐胡璐

i just wanna live while i am alive

393 posts
42 categories
74 tags
RSS
LinkedIn Weibo GitHub E-Mail
Creative Commons
© 2022 唐胡璐
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4