ArticleDirectory
- Problem description
- Solve the problem
- Review
I introduced how to use ironruby as the script language to compile the automated test script in my automated testing framework using codedui of vs2010, however, an encoding problem was found during the script writing process using ironruby. Here we will introduce you to you that the encoding of the main script file is required when writing the script.
Problem description
I called Microsoft. visual Studio. testtools. uitesting. find the DLL button and click the function, but always reports an error, saying that the control cannot be found, but in C #CodeThere is no problem in writing similar code. After a problem occurs, I searched for it online, but there were not many resources and no results. Therefore, I submitted a bug on the ironruby codeplex website. For details, see:Encode error?The description is as follows:
I want to use visualstudio uitest Do My auto UI test, But I Don ' T find window and button.
(Attachment Is Microsoft. visualstudio. testtools. UI
Testing. dll)
Code:
Require " Microsoft. visualstudio. testtools. uitesting. dll "
Include Microsoft: visualstudio: testtools: uitesting: wpfcontrols
W = Wpfwindow. New
W. searchproperties [ " Name " ] = " Hi! " ;
Puts W. searchproperties [ " Name " ]
Result: Too Many Rows exist.
CT: Hello
- To simplify the application, I created an application. CS file in C #, which contains an application class, which has a test method, passed in a string parameter, and returned directly:
Public Class Application
{
Public StringTest (StringA)
{
ReturnA;
}
}
- Use Notepad ++ to create main. RB, which mainly calls the application. Test method: putsApplication. New. Test ("hello ")
After running, it does not display "hello", but several garbled characters.
Analyze problems
I searched some content related to character encoding on the Internet, many of which introduced character encoding conversion, such as result = iconv. iconv ("gb2312", "UTF-16", a), I tried to convert multiple characters in both the RB script and C #, and found that it still cannot be parsed correctly.
At this moment, I suddenly remembered this question I asked a ruby expert in the company after I encountered this problem last week. The following is the main content of the email:
- Title: Is there any problem with Unicode processing in Ruby?
- Q: What I use now is. net Ruby implementation ironruby, now has a problem, I don't know if there are any in ruby, the problem is: directly hash in ironruby code. there is no problem attaching Chinese characters to new, but there is a problem. net class library, which has a property similar to hash. I paid it a Chinese character in the ironruby code, but it was garbled when it was obtained. Why?
- Reply: There is no encoding concept in ruby1.8 and it is a character sequence. So what do you give him? He doesn't care.The ruby file is encoded, which directly determines the encoding of your character sequence.. I don't know what encoding. NET is. The two sides should be consistent.
I proposed a hypothesis,Is it because of file encoding?
Solve the problem
Now that the assumption is put forward, the next step is to verify my assumption. Because I am not familiar with character encoding, So I went online to search some, understand the following Utf-8, gb2312, and C # is Unicode, So I then tried the following steps:
- Change the file to the UTF-8 format in Notepad ++, as shown in:
- Add the parameter-ku to ir.exe execution.
IR-Ku-X: showclrexceptions main. Rb
After running it again, I found everything was so beautiful!
Review
It took me a day to solve this problem. In general, the problem is not difficult, but I need to know something about Ruby's character encoding. I didn't notice that this will also cause problems, to solve this bug, you can share the following points:
- Asking for help is the fastest and most convenient method when you encounter unfamiliar problems and have no idea.
- One of the solutions to the problem is to make assumptions and then verify them.
- When using the new technology, it is best to study it systematically. For example, I just spent half a day reading ironruby unleashed.w.gov.cn to learn about ir.exe's kernel-kkcode. if no, I have changed the file category and do not want to increase the ir.exe parameter.
- Knowledge Point: file encoding directly affects Ruby's character encoding
You are welcome to reprint it. Please note: Reprinted fromZhou jingen [http://zhoujg.cnblogs.com/]