I want to test translation on page
I have written class of my page, and created test file where i test my page using JUnit.
And i have created property file " en.properties " for language and this file is placed inside my Java project, under the “src/main/resources/languages” folder.
Then i added translated text in like "key=value"
login=login
password=password
button.login=login
In test file i wrote method that opens the file and reads the value from the file
public String getTranslation(String key, String language) throws IOException {
Properties prop = new Properties();
FileInputStream input = new FileInputStream("src/main/resources/languages/" + language + ".properties");
prop.load(new InputStreamReader(input, Charset.forName("UTF-8")));
input.close();
return prop.getProperty(key);
}
And this method clicks on the language button to change language, and then it should checks translation but i get the error
@Test
public void changeLanguageEng(){
mainPage.clickLanguageButton("Eng");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Assert.assertEquals(mainPage.getHeadingText(),getTranslation("heading","en"));
Assert.assertEquals(mainPage.getLanguageButtonText(),getTranslation("button.language","en"));
Assert.assertEquals(mainPage.getLoginButtonText(),getTranslation("button.login","en"));
Assert.assertEquals(mainPage.getLoginFieldText(),getTranslation("login","en"));
Assert.assertEquals(mainPage.getPasswordFieldText(),getTranslation("password","en"));
Assert.assertEquals(mainPage.getErrorText(),getTranslation("error","en"));
} catch (IOException e) {
e.printStackTrace();
}
}
java.io.FileNotFoundException: src\\main\\resources\\languages\\en.properties (The system cannot find the file specified)
Here is my full path
D:\\Intellij IDEA\\pageobjectseleniumtest\\src\\main\\resources\\languages
we can access file from the resources folder by using ClassLoader. please try below code to resolve your file not found exception
public String getTranslation(String key, String language) throws IOException, FileNotFoundException {
Properties prop = new Properties();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("languages/languages.properties");
prop.load(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
inputStream.close();
return prop.getProperty(key);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.