简体   繁体   English

使用 Selenium 自动测试翻译

[英]Automated testing of translations using Selenium

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.我已经编写了我的页面类,并创建了测试文件,我在其中使用 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.我为语言创建了属性文件“ en.properties ”,这个文件放在我的 Java 项目中,在“src/main/resources/languages”文件夹下。

Then i added translated text in like "key=value"然后我添加了翻译文本,如“key=value”

  • login=login登录=登录

  • password=password密码=密码

  • button.login=login button.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) java.io.FileNotFoundException: src\\main\\resources\\languages\\en.properties(系统找不到指定的文件)

Here is my full path这是我的完整路径

D:\\Intellij IDEA\\pageobjectseleniumtest\\src\\main\\resources\\languages D:\\Intellij IDEA\\pageobjectseleniumtest\\src\\main\\resources\\languages

we can access file from the resources folder by using ClassLoader.我们可以使用 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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM