CREATE PROCEDURE Myproc
Friday, December 13, 2024
Execute store procedure with out param dynamicly
A simple recorder based on selenium
As known, selenium automating web applications for testing purposes. Below code will present an idea to record operations performed on the browser using selenium and javascript. The key is using javascript to listen the onlick and onchange event of document element. And then according to the return information to identify what action the tester did and what element the action performed.
/** JavaScript to register listeners for click & change events. */
private static final String SCRIPT = "var callback = arguments[arguments.length - 1];"
+ "function record(category)"
+ "{"
+"var result = new Array();"
+"result[0] = category;"
+"result[1] = event.srcElement.tagName;"
+"result[2] = event.srcElement.id;"
+"result[3] = event.srcElement.name;"
+"result[4] = event.srcElement.className;"
+"result[5] = event.srcElement.type;"
+"result[6] = event.srcElement.value;"
+"result[7] = event.srcElement.getAttribute('gwt_id');"
+"result[8] = event.srcElement.getAttribute('control_id');"
+"result[9] = event.srcElement.innerText;"
+ "callback(result);"
+ "}"
+ "function click()"
+ "{"
+ "record('click');"
+ "}"
+ "function enter()"
+ "{"
+ "record('enter');"
+ "}"
+ "document.onclick = click;"
+ "document.onchange = enter;";
/** Constant definition for tab name index. */
protected static final int TAG_NAME_INDEX = 1;
/** Constant definition for ID index. */
protected static final int ID_INDEX = 2;
/** Constant definition for name index. */
protected static final int NAME_INDEX = 3;
/** Constants definition for class property index. */
protected static final int CLASS_INDEX = 4;
/** Constant definition for value index. */
protected static final int VALUE_INDEX = 6;
/** Constant definition for GWT ID index. */
protected static final int GWT_ID_INDEX = 7;
/** Constant definition for control id index. */
protected static final int CONTROL_ID_INDEX = 8;
/** Constant definition for inner text index. */
protected static final int INNER_TEXT_INDEX = 9;
driver.manage().timeouts().setScriptTimeout(5000, TimeUnit.SECONDS);
Object result = ((JavascriptExecutor) webDriver).executeAsyncScript(SCRIPT);
if (result instanceof List<?>) {
@SuppressWarnings("unchecked")
List<String> list = (List<String>) result;
String classAttribute = list.get(CLASS_INDEX);
ParseResult result = ParseResult.FAILURE;
if (list[0] == "click" && !StringUtils.isEmpty(classAttribute) && classAttribute.contains("Label")) {
String tabCaption = list.get(INNER_TEXT_INDEX);
String xpath = String.format("//div[@class='%s'][text()='%s']/parent::div", list.get(CLASS_INDEX), tabCaption);
WebElement element = driver.waitForXPath(xpath);
String parentClass = element.getAttribute("class");
if (StringUtils.isNotBlank(parentClass) && parentClass.contains("TabLayoutPanelTabInner")) {
ICommand command = new Command(COMMAND_NAME, tabCaption);
result = new ParseResult(command);
}
}
}
Parsing a command based script file with Regex
In script file, the command format is:
Transfer XML to HTML by JS based on XSL
XSL stands for Extended Stylesheet Language. The reason why the World Wide Web Consortium began developing XSLs was due to the demand for XML based style sheet languages. XSLT stands for XSLT transformation, which is a W3C standard. Just as CSS is a style sheet for HTML, XSL is a style sheet for XML.
1 Prepare XML file cdcatalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
2 Prepare xsl file cdcatalog.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="artist" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
3 Prepare javascript:
<html>
<body>
<script type="text/javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cdcatalog.xml")
// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cdcatalog.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
Parse XML by XPath
XPath is a language used to search for information in XML documents. XPath can be used to traverse elements and attributes in XML documents.
XPath is a major element of the W3C XSLT standard, and both XQuery and XPointer are built on top of the XPath expression.
Therefore, understanding XPath is the foundation of many advanced XML applications.
1 Prepare xml file books.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
2 Prepare HTML file XPathTest.html using XPath
<html>
<body>
<script type="text/javascript">
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("books.xml")
path="/bookstore/book/title"
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);
for (i=0;i<nodes.length;i++)
{
document.write(nodes[i].childNodes[0].nodeValue);
document.write("<br />");
}
}
</script>
</body>
</html>
C# calls the C++ code
- Prepare the C++ code:
1 create the solution 'TestConsole' by VS.
2 In this solution, add a new project CppDeno with mode 'Win32 Console Application' and application type 'DLL'
3 In file CppDemo.cpp add the code as below:
extern "C" __declspec(dllexport) int Add(int a,int b) {
return a + b;
}
4 compile this simple project.
- Static call the C++ dll:
1 Under solution 'TestConsole', add new project 'TestDemo'
2 In program.cs file, add below code to Main method:
Console.WriteLine(Add(1, 2));
Console.Read();
3 Copy the CppDemo.lib & CppDemo.dll to debug folder of this project
4 Press key 'F5' to run
- Dynamic call C++ code:
1 Create new class NativeMethod.cs, the code as below:
using System;
using System.Runtime.InteropServices;
namespace TestDemo
{
internal class NativeMethod
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
public static extern bool FreeLibrary(int hModule);
}
}
2 add delegate definition to Program.cs:
delegate int Add(int a, int b);
3 update method Main as below:
static void Main(string[] args) {
//1. 动态加载C++ Dll
int hModule = NativeMethod.LoadLibrary(@"c:CppDemo.dll");
if (hModule == 0) return;
//2. 读取函数指针
IntPtr intPtr = NativeMethod.GetProcAddress(hModule, "Add");
//3. 将函数指针封装成委托
Add addFunction = (Add)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(Add));
//4. 测试
Console.WriteLine(addFunction(1, 2));
Console.Read();
}
Summery for synchronization in .NET
The following tables summarize the .NET tools available for coordinating or synchronizing the actions of threads:
1 Simple Blocking Methods
| Construct | Purpose |
| Sleep | Blocks for a given time period |
| Join | Waits for another thread to finish |
2 Locking Constructs
| Construct | Purpose | Cross Process? | Speed |
| Lock | Ensures just one thread can access a resource, or section of code. | No | Fast |
| Mutex | Ensures just one thread can access a resource, or section of code. Can be used to prevent multiple instances of an application from starting. | Yes | Moderate |
| Semaphore | Ensures not more than a pecified number of therads can access a resource, or section of code. | Yes | Moderate |
(Synchronization Context are also provided, for automatic locking)
3 Signaling Constructs
| Construct | Purpose | Cross Process? | Speed |
| EventWaitHandle | Allows a thread to wait until it receives a signal from another therad. | Yes | Moderate |
| Wait & Pulse | Allows a thread to wait until a custom blocking condition is met. | No | Moderate |
4 Non-Blocking Synchronization Constructs
| Construct | Purpose | Cross Process? | Speed |
| Interlocked | To perform simple non-blocking atomic operations. | Yes (Assuming shared memory) | Very fast |
| volatile | To allow safe non-blocking access to individual fields outside of a lock. | Yes (Assuming shared memory) | Very fast |
-
As known, selenium automating web applications for testing purposes. Below code will present an idea to record operations performed on the ...
-
Macros Fundamentally, macros are a way of writing code that writes other code, which is known as metaprogramming. The term macro refers to ...
-
HashMap The concept of HashMap is present in almost all programming languages like Java, C++, Python, it has key-value pairs and through ke...