使用 Java 绑定在 Selenium 中自动化本地化 Web 应用程序的方法
我正在使用 selenium 2.0 和 Java 自动化 Web 应用程序的测试用例。我的应用程序支持多种语言。一些测试用例要求我验证 UI 中显示的文本,例如成功/错误消息等。我正在使用属性文件来存储我在 UI 测试中引用的任何文本,目前只有英文。例如, locale_english.properties(见下文)包含所有英文引用。我将为不同的语言环境创建多个这样的属性文件,例如 locale_chinese.properties、locale_french.properties 等。对于英语以外的区域设置,其相应的属性文件将具有表示本机字符的 UTF-8 字符(例如 \u30ed)(见下文)。所以如果我想测试中文 UI,我会加载“locale_chinese.properties”而不是“locale_english.properties”。我将使用 JDK 中的 native2ascii 或其他方式将本机字符转换为非英语语言环境。我测试了 Selenium API 可以很好地处理非英语语言环境的 UTF-8 字符
---locale_english.properties------
user.login.error= Please verify username/password
---locale_chinese.properties------
user.login.error= \u30ed\u30ef\u30eg\u30eh\u30ed
and so on.
问题是我的 locale_english.properties 正在增长并失去控制。管理一个区域设置的单个属性文件变得越来越困难,更不用说管理多个区域设置了。有没有更好的方法来处理 Java 中的本地化,特别是在像我这样的情况下?
谢谢!
I am automating test cases for a web application using selenium 2.0 and Java. My application supports multiple languages. Some of the test cases require me to validate the text that appears in the UI like success/error messages etc.I am using a properties file to store whatever text I am referring in my tests from the UI, currently only english. For example there is locale_english.properties(see below) that contains all references in english. I am going to have multiple properties files like this for different locales like locale_chinese.properties,locale_french.properties and so on. For locales other than english, their corresponding properties file would have UTF-8 characters (e.g \u30ed) representing the native characters(see below). So If I want to test say Chinese UI, I would load "locale_chinese.properties" instead of "locale_english.properties". I am going to convert the native characters for non-english locale using perhaps native2ascii from JDK or some other way.I tested that Selenium API works well with UTF-8 characters for non-english locales
---locale_english.properties------
user.login.error= Please verify username/password
---locale_chinese.properties------
user.login.error= \u30ed\u30ef\u30eg\u30eh\u30ed
and so on.
The problem is that my locale_english.properties is growing and going out of control. It is becoming hard to manage a single properties file for one locale let alone for multiple locales. Is there a better way of handling localization in Java, particularly in situations like I am in?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是对的,管理文件存在问题,但您也认为这是最好的方法。有些事情就是很难:-(
Selenium(至少是 Selenium RC API)确实支持 Unicode 输入和输出,我们有很多测试来输入和确认 C# 中的西里尔文和简单中文字符。由于 Java 字符串的核心是 Unicode (就像 C# 一样),我希望您可以简单地在 Notepad++ 等 UTF-8 友好的编辑器中创建文件,然后将它们直接读入字符串并直接在 Selenium API 中使用它们。
You're right that there is a problem managing the files, but you're also right that this is the best approach. Some things are just hard :-(
Selenium (at least the Selenium RC API) does indeed support Unicode input and output, we have lots of tests that enter and confirm Cyrillic and Simple Chinese characters from C#. Since Java strings are Unicode at the core (just like C#), I expect you could simply create the file in a UTF-8-friendly editor like Notepad++ and read them straight into strings and use them directly in the Selenium API.
这是我如何为有兴趣的人解决了这个问题。
This is how I solved the issue for those who are interested.
出于多种原因,数据库会更好地工作,例如增长、中心位置、保存在应用程序之外,并且可以在应用程序之外进行编辑和维护。我们使用了一个包含列的表:
id(int)自动递增
id_text -- 此列和其他列都是 varchar ...除了最后 2 列的日期时间
郎
翻译
创建者
更新者
创建日期
Updated_date
id 是文本的简短英文描述 - 例如“hello”或“error1msg”,是地图中的键。
在java中有一个函数来获取特定文本的文本......以及一个应用程序级别属性 - 默认语言(通常是en,但最好保持可配置)
函数将扫描已加载的散列映射以查找所需的语言 - 说“ch”
如果未找到该语言的相应翻译,我们将返回默认语言翻译,如果未找到,则我们将返回“[”+ id“]”,以便测试人员知道数据库中缺少某些内容 - 可以转到网络屏幕编辑翻译表并添加它。
a database would work better for many reasons, like growth, central location, kept outside of app and can be edited and maintained outside of app. We used a table with columns:
id (int) auto increment
id_text -- this and other columns are varchar ... except for date time for last 2
lang
translation
created_by
updated_by
created_date
updated_date
An id is a short english description of the text - like 'hello' or 'error1msg', the key in your map.
In java had a function to get the text for a particular text ... and a app level property - default language (usually en but good to keep it configurable)
Function would scan already loaded hashmap for language asked for - say "ch"
If corresponding translation was not found for this language we would return the default language translation and if that was not founf then we would return "[" + id "]" so the tester knows something is missing in data base - can go to web screen to edit translation table and add it.