Author Archives: Shang Erxin

About Shang Erxin

My undergraduate degree is Electrical Engineering and Automation. Graduate degree is Power Electronics and Power Drives. Personal favorite programming languages are C, C++, C# and Python. Currently I works as an architect/ software engineer/ programmer in Micron. During the free time, I also work as a free-lance programmer. Have good knowledge of, WCF, WPF, WF, Silverlight, Django, wxPython, jquery, SQL Server, SQLite, Mongo DB, Mobile development with Mono and Unity. If you want to find a free-lance programmer to solve problems with these techniques feel free to contact me.

Tips for resolve pip install error for some of the python 2.7 package on window

Pip is a wide used python package management tool, it works properly most of the time. But occasionally there will be a few error when install some special package which may required local build after download.
Here are two common ways to resolve these kinds of error

1. Use a prebuild binary, the most common library could be download from here. But the links seems dead for a while.

2. Install the necessary Cpp compiler for python 2.7 on window from here

After install the compiler rerun the pip install again

Good luck!

Handling None Standard File Upload with TruClient

First let’s explain what is the “none standard file upload” for a website. Let a user upload a file is a very common use case for a website. The most common way to implement this would be

  1. <input type="file"/>

 

Then post the file content with form to the server. This is the standard way to upload a file with HTML. The standard way have several limitation. The most significant is difficult to customize the style for the upload file control.

So the none standard file upload come up with this requirement. The main idea is let the web designer customize the looking as they preferred. After the user click the control then dynamically create an input element or trigger a hidden input click in the background. This make the upload control looks much better, but it lead the feature hard for automation UI test. The common automation tools such as selenium(webdriver) and jmeter do not provide a elegant way to handle none standard upload. The reason is due to the input element is dynamically created which nearly impossible to locate it with CSS selector or XPath.

In TruClient 12.55+ which provide a easy way to handle none standard file upload.

Here is a simply code snippet for demo none standard file upload with jsfiddle at http://jsfiddle.net/erxin/o4menymo/

None standard file upload code sample

None standard file upload code sample

1. Drag a “General Object Action” step
2. Select the aim object which the user will click to upload the file
3. Change the step type from “Click” to “Upload”
4. Assign the right file path to the “Path” parameter
5. Done.
none-standard-upload

Currently the none standard file upload step only support in TruClient Chrome and IE. The Firefox version will coming soon.

Enjoy!

Tip for Easily Backup Local Script Files in TruClient Lite Chrome

Currently TruClient Lite Chrome doesn’t support batch script operations. If the user want to back up all the local scripts, then they have to manually download the scripts one by one.
There are two ways to back up the scripts

  1. All the scripts are saved in the user data directory. So back up the user data directory will back up the script indirectly.
  2. We could use a hack way to download all the scripts as a single zip file.

Steps to back up user data folder

  1. Check the path from the command line of the shortcut of your TruClient Lite Chrome
    1. --user-data-dir=<profile dir> --extensions-on-chrome-urls --silent-debugger-extension-api --disable-web-security --no-first-run
  2. Back up the folder <profile dir>

Steps to download all the scripts as a single zip file

  1. Start the TruClient Lite
  2. Navigate to chrome://extensions/ to open the extension management pages for Chrome
  3. Check the Developer mode
    extension-enable-dev-mode
  4. Click background page link for TruClient Lite
    extension-page
  5. Run the below code on console, the scripts will be saved
    1. var zip = new JSZip(); localforage.iterate((v, k) => { if(k.startsWith('/__scripts__/') && k.indexOf(':metadata:') === -1){ zip.file(k.split('/').pop()+'.zip', v); } }).then(()=>{ a = document.createElement('a'); a.href = window.URL.createObjectURL(zip.generate({type:"blob"})); a.download = "scripts.zip"; a.dispatchEvent(new MouseEvent("click")); });
  6. Restart TruClient Lite

Polyfill for Utf8toUtf16 for JavaScript

As we know the TextEncoder will drop support utf-16 and other legacy encoding types from Mozilla. Google also confirmed it and Chrome will only remain support utf-16 encoding.

Here is a polyfill implementation for convert utf-8 string to utf-16le

  1. function utf8ToUtf16(s) {
  2. var a = new Uint8Array(s.length * 2),
  3. view = new DataView(a.buffer);
  4. s.split('').forEach(function (c, i) {
  5. //the third parameter equal to true indicate little ending
  6. view.setUint16(i * 2, c.charCodeAt(0), true);
  7. });
  8. return a;
  9. }

 

Reference:
https://bugzilla.mozilla.org/show_bug.cgi?id=1257877
https://www.chromestatus.com/feature/5630760492990464

Hook All the Functions in JavaScript Files

During my daily development, I encounter a request to add code snippets before and after each function call for all the JavaScript files. I have figure out three ways to do this

1. Overwrite the Function.prototype.call, this will only effect the directly call the call method from a function object such as foo.call(…). The call method will not triggered with directly call function with brace, such as foo(…)

disadvantage: not cover all the function even normal function calls

2. Use custom or use some AOP JS library to hook specify function. This is also could be extended by use a recursive method to hook all the function object in a give scope such as window in browser.

such as hooker lib, https://github.com/rcorp/hooker

jquery AOP plugin

advantage: easy to use

disadvantage: not cover all of the functions in the JS files, such as anonymous functions are not included.

3. Use some script to parse all the JS files and inject codes before and after all the functions.

This article is mainly to talk about the tool which I created for automatic inject the codes before and after the function calls.

It is called hookjs, which is written in python. It could be checkout from my github repo(https://github.com/shangerxin/PersontalTools). The binary version could be download from the bin folder.

$ hookjs -h

usage: hookjs.exe [-h] [-p PATH] [-s START] [-e END]                   [-f [BLACK_FILES [BLACK_FILES ...]]]                   [-d [BLACK_DIRS [BLACK_DIRS ...]]]

A command line JavaScript hook tool for inject start, end codes into every JavaScript functions. Currently only support uncompressed EMCScipt 5. Any errors will be output into the error.log file.

optional arguments:

-h, –help   show this help message and exit

-p PATH, –path PATH  The path to the JavaScript file or directory

-s START, –start START  The start code snippet which will be injected at the                         begin of each function

-e END, –end END     The end code snippet which will be injected at the end                         of each function

-f [BLACK_FILES [BLACK_FILES ...]], –black-files [BLACK_FILES [BLACK_FILES ...]]                         Use regex expression to define the black files list, the files will not be hooked

-d [BLACK_DIRS [BLACK_DIRS ...]], –black-dirs [BLACK_DIRS [BLACK_DIRS ...]]                         Use regex expression to define the black dirs list, the directory and sub directory will not be searched

Created by Edwin, Shang(Shang, Erxin), License under GNU GPLv3. Version 1.0.0

Example:

Inject codes to a JS files, the original JS content

  1. x = findMax(1, 123, 500, 115, 44, 88);
  2.  
  3. (function(){
  4. })();
  5.  
  6. function findMax() {
  7. var i;
  8. var max = -Infinity;
  9. for (i = 0; i < arguments.length; i++) {
  10. if (arguments[i] > max) {
  11. max = arguments[i];
  12. }
  13. }
  14. return max;
  15. }

 

run command

  1. hookjs.exe -p e:\demo.js -s console.log("start"); -e console.log("end");
  1. x = findMax(1, 123, 500, 115, 44, 88);
  2.  
  3. (function(){console.log(start);
  4. console.log(end);})();
  5.  
  6. function findMax() {console.log(start);
  7. var i;
  8. var max = -Infinity;
  9. for (i = 0; i < arguments.length; i++) {
  10. if (arguments[i] > max) {
  11. max = arguments[i];
  12. }
  13. }
  14. console.log(end);return max;
  15. console.log(end);}

 

Limitations:

The tool is not works with compressed JS file such as jquery-min etc. To add hook for these kinds of JS files, it required to format the JS first then use the tool the inject the JS codes.

Current it is not design for support arrow function syntax which is added in EMCAScript 6

Have fun.