Monday 29 December 2014

Internationalization Automation Testing using WebDriver & Java

Internationalization, a subset of Globalization also known as i18n.

With the increasing competition in software industry, Organizations are targeting global markets to spread the business and influence of the product. This means working beyond the geography, culture, language and to work with clients to make sure that application/software has proper handling in terms of readable, view-able, writable and functional perspectives. So, the question here is – Do we need to develop separate application / software for each language and country? The answer is NO.

In internationalization development team removes the hard coded strings and put them into the resource file, there will be different resource files for all different languages. And, by replacing the default language resource file with target language resource file will activate that locale on the application. This is the most used option in development for internationalization development.

The purpose of telling the process of development here was to make you all aware that we can follow the same strategy in automation testing of i18n applications. Developers use these resource files for different language provided by linguist experts in a key-value format. Here key for all locale are same with different value based on language.

Let’s understand the approach in little bit detail. As all of you would be aware of that computer program or code only understand the Latin-1 or Unicode characters. So, all the extracted values from resource files should be translated. Characters of different languages are categorized in single-byte and double-byte characters set. Languages like French, German contains single-byte characters and need not to translate, but languages like Chinese, Korean and Japanese contains double-byte characters. So, to use them in program or code we need to convert into Unicode.

To convert double-byte characters into Unicode, JDK provides a utility called native2ascii. In this post we will use the same for conversion.

Practical Implementation:

Double-byte characters to Unicode conversion:
Below command can used on CMD for conversion. JDK must be installed into the system.

native2ascii -encoding utf8 japanese_native.txt japanese_ascii.txt

Now, we will do this stuff programmatically using Java.


import java.io.File;

public class RunCommandLine {

    public static void runNative2Ascii(String languageFile, String outputFile){
 Runtime runtime = Runtime.getRuntime();
 Process process;

 try{
     System.out.println("native2ascii -encoding utf8 \"" 
    + new File(languageFile) + "\" \"" + new File(outputFile) + "\"");
    process = runtime.exec("cmd /c native2ascii -encoding utf8 \""
    + new File(languageFile) + "\" \"" + new File(outputFile) + "\"");
 }catch(Exception ex){
           ex.printStackTrace();
 }
   }
}

Sample Japanese native language file and output file after Unicode conversion:












The Java class RunCommandLine.java run the native2ascii command programmatically and Unicode characters of Japanese native language will be saved into output.txt file. To read those Unicode characters find below the code:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadTextFile {

 public static String readParsedResourceFile(String fileName){
  InputStream iStream = null;
  InputStreamReader iStreamReader = null;
  BufferedReader bReader = null;
  StringBuffer sBuffer = new StringBuffer();
  String content = null;
  String resultString = "";

  try{
   iStream = new FileInputStream(new File(fileName));
   iStreamReader = new InputStreamReader(iStream);
   bReader = new BufferedReader(iStreamReader);

   while((content = bReader.readLine()) != null){
    sBuffer.append(content);
   }
   content = sBuffer.toString();
   content = content.substring(6, content.length());
  }catch(Exception ex){
   ex.printStackTrace();
  }finally{
   try{
    if(iStream != null) iStream.close();
    if(iStreamReader != null) iStreamReader.close();
    if(bReader != null) bReader.close();
   }catch(Exception ex){
    ex.printStackTrace();
   }

  }
  Matcher m = Pattern.compile("\\\\u([0-9A-Fa-f]{4})").matcher(content);

  while (m.find()) {
   try {    
    int cp = Integer.parseInt(m.group(1), 16);
    char[] chars = Character.toChars(cp);
    String rep = new String(chars);    
    resultString += rep; 
   } catch (NumberFormatException e) {
    e.printStackTrace();
   }
  }
  return resultString;
 }
}


Now, we will write the script to see the actual result. In the following we are launching google in Japanese domain and typing Japanese text to the browser which is actually Unicode characters saved in output.txt file. Find below code for the same:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.blogger.util.ReadTextFile;
import com.blogger.util.RunCommandLine;

public class GoogleI18N {
 WebDriver driver;
 
 @BeforeTest
 public void setup(){  
  driver.manage().window().maximize();
 }
  
 @Test(priority=0)
 public void launchURL(){
  driver.get("http://www.google.jp");
 }
 
 @Test(priority=1)
 public void searchInDetuchLanguage(){
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     RunCommandLine.runNative2Ascii(""
  + ".\\resources\\japanese.txt", ".\\resources\\output.txt");
     driver.findElement(By.xpath("//div[@id='sb_ifc0']//input"))
     .sendKeys(ReadTextFile.readParsedResourceFile(".\\resources\\output.txt"));
 }
}


Output:





















Hope this article gives you a rough idea on how to proceed with internationalization testing.

Stay tuned to see more updates.

Please like and comment if you like the post.

1 comment:



  1. I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
    Very interesting blog.
    webdrive-enterprise-crack
    idm-crack
    disk-drill-pro-crack
    airmail-crack
    fxsound-pro-crack
    avs-audio-converter-crack
    passfab-for-rar-crack

    ReplyDelete