使用 classes 的自制库作为其他 class 文件中的函数
Using a homemade library of classes as functions in other class files
我是 Java 的新手,如果这是一个愚蠢的问题,请原谅我,但请相信我,我真的找不到可靠的答案。
这就是我正在使用的:
所以我正在测试一个程序,保持它的维护和更新的最简单方法是创建我自己的 "buttons" 库。库中的所有内容都是小函数,例如 "enterValidCredentials" 和 "clickLoginButton".
那么让我们来看看我的测试用例。在一个完美的世界中,我将能够:
public class progressCheck {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://mail.google.com/");
enterValidCredentials;
clickLoginButton;
}
}
enterValidCredentials 和 clickLoginButton 存在于我的 classes 库中。我很清楚那不会像上面写的那样工作。从字面上看,正确的方法是什么?
如果有帮助的话,我的 enterValidCredentials class 看起来像这样:
public class loginPageButtons {
private WebDriver driver;
Actions actions = new Actions(driver);
public class enterValidCredentials { // This class enters in a valid username and valid password on the login page.
public void enterValidCredentials2() {
driver.findElement(By.cssSelector("input[type=\"text\"]")).clear();
driver.findElement(By.cssSelector("input[type=\"text\"]")).sendKeys("XXXXXXXX");
driver.findElement(By.cssSelector("input[type=\"password\"]")).clear();
driver.findElement(By.cssSelector("input[type=\"password\"]")).sendKeys("XXXXXXXX");
}
}
我的所有其他功能都遵循相对相似的结构(当然,取决于它们的功能)。
您可以使用单元测试来检查 类 的单一功能。
创建单元测试最常用的库是JUnit。
如果您使用 ide(如 IntelliJ 或 Eclipse)运行,则可以使用与 运行 主方法完全相同的简单命令来完成测试。
如果您需要创建对象的模拟,您可以使用像 Mockito 这样的库(但还有许多其他有效的替代方法)。
注意:mock 是一个对象,它与复杂对象具有相同的接口,难以在测试环境中使用(例如数据库连接、文件处理程序,网络处理程序)。
这是一个例子,我试着想象你的代码和可能的测试。我假设 clickLoginButton returns 是一个整数,只是为了显示可能的断言语句。
示例:
@Test
public static void testCredentials() {
WebDriver driver = new FirefoxDriver();
driver.get("http://mail.google.com/");
EnterValidCredentials enterValidCredentials = new EnterValidCredentials(); // Or create a mock if necessary
// Set values if necessary
int returnValue = enterValidCredentials.clickLoginButton();
assertEquals(returnValue, 1);
}
我是 Java 的新手,如果这是一个愚蠢的问题,请原谅我,但请相信我,我真的找不到可靠的答案。
这就是我正在使用的:
所以我正在测试一个程序,保持它的维护和更新的最简单方法是创建我自己的 "buttons" 库。库中的所有内容都是小函数,例如 "enterValidCredentials" 和 "clickLoginButton".
那么让我们来看看我的测试用例。在一个完美的世界中,我将能够:
public class progressCheck {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://mail.google.com/");
enterValidCredentials;
clickLoginButton;
}
}
enterValidCredentials 和 clickLoginButton 存在于我的 classes 库中。我很清楚那不会像上面写的那样工作。从字面上看,正确的方法是什么?
如果有帮助的话,我的 enterValidCredentials class 看起来像这样:
public class loginPageButtons {
private WebDriver driver;
Actions actions = new Actions(driver);
public class enterValidCredentials { // This class enters in a valid username and valid password on the login page.
public void enterValidCredentials2() {
driver.findElement(By.cssSelector("input[type=\"text\"]")).clear();
driver.findElement(By.cssSelector("input[type=\"text\"]")).sendKeys("XXXXXXXX");
driver.findElement(By.cssSelector("input[type=\"password\"]")).clear();
driver.findElement(By.cssSelector("input[type=\"password\"]")).sendKeys("XXXXXXXX");
}
}
我的所有其他功能都遵循相对相似的结构(当然,取决于它们的功能)。
您可以使用单元测试来检查 类 的单一功能。
创建单元测试最常用的库是JUnit。
如果您使用 ide(如 IntelliJ 或 Eclipse)运行,则可以使用与 运行 主方法完全相同的简单命令来完成测试。
如果您需要创建对象的模拟,您可以使用像 Mockito 这样的库(但还有许多其他有效的替代方法)。
注意:mock 是一个对象,它与复杂对象具有相同的接口,难以在测试环境中使用(例如数据库连接、文件处理程序,网络处理程序)。
这是一个例子,我试着想象你的代码和可能的测试。我假设 clickLoginButton returns 是一个整数,只是为了显示可能的断言语句。
示例:
@Test
public static void testCredentials() {
WebDriver driver = new FirefoxDriver();
driver.get("http://mail.google.com/");
EnterValidCredentials enterValidCredentials = new EnterValidCredentials(); // Or create a mock if necessary
// Set values if necessary
int returnValue = enterValidCredentials.clickLoginButton();
assertEquals(returnValue, 1);
}