gpt4 book ai didi

java - 使用Selenium自动进行翻译测试

转载 作者:行者123 更新时间:2023-12-01 17:50:38 24 4
gpt4 key购买 nike

我想在页面上测试翻译

我已经编写了页面的类,并创建了测试文件,在其中使用JUnit测试页面。

我已经为语言创建了属性文件“ en.properties”,并且该文件放置在我的Java项目中的“ src / main / resources / languages”文件夹下。

然后我将翻译后的文本添加为​​“ key = value”


登录=登录
密码=密码
button.login =登录


在测试文件中,我编写了打开文件并从文件中读取值的方法

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);
}


并且此方法单击语言按钮以更改语言,然后应检查翻译,但出现错误

@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(系统找不到指定的文件)


这是我的完整路径

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

最佳答案

我们可以使用ClassLoader从资源文件夹访问文件。请尝试下面的代码来解决您的文件未找到异常

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);
}

关于java - 使用Selenium自动进行翻译测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com