我试图以不同的组合(有效-有效、有效-无效、无效-有效、无效-无效)传递用户名和密码,并将它们分配给字符串变量用户名和密码,并使用参数化类。
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
@SuppressWarnings("deprecation")
@RunWith(Parameterized.class)
public class FunctionalTestCaseActiTimeParameterization {
Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://localhost/login.do");
String userName;
String password;
public FunctionalTestCaseActiTimeParameterization(String userName, String password){
this.userName = userName;
this.password = password;
}
@Parameters
public static Object[][] getData(){
return new Object[][]{
{"admin","manager"},
{"admin","test"},
{"test","manager"},
{"test","test"}
};
}
@Before
public void openApplication(){
selenium.start();// start interaction with proxy server
selenium.open("/");// to open application
selenium.windowMaximize();// to maximize the window
selenium.windowFocus();// to focus on current window
}
@After
public void closeApplicaton() throws InterruptedException{
Thread.sleep(5000);
selenium.close();// close window
selenium.stop();// stop interaction with server
}
@Test
public void mainTestMethodLoginLogout() throws InterruptedException{
selenium.type("//input[@id='username']", userName);
selenium.type("//input[@type='password']", password);
Thread.sleep(3000);
}
}
并且我没有收到任何错误或警告(我使用了参数化,但没有在函数内部使用,因此对如何在类型函数内部使用它感到有点困惑, 运行代码时失败(没有错误)。
如果有人能指出我正确的方向,我会很高兴。
最佳答案
我认为二维数组方法是一个错误,因为进一步观察,您需要将您的 {username:password} 组合与预期输出相关联。
也许期望只需要是一个 boolean 值(“OK”与“Not OK”),在这种情况下您可以使用 List<MyTestSpec>
哪里MyTestSpec
包括{用户名,密码,wasOK} - 但您可能有其他需求,例如检查成功登录重定向到不同用户级别的个人资料页面,检查不同类型的登录失败。
您确实应该从拥有一个单独的、不同的 @Test
开始针对每个场景并使每个场景尽可能全面和详细。
只有在您通过了失败的测试并且锁定了您的期望后,才需要重构您的 Selenium API 逻辑(创建辅助函数等)。
请注意,无论使用 Selenium 的 RC 还是 WebDriver API,这同样适用。
关于java - 如何使用 junit 将数据从二维参数数组传递到 selenium rc 中的 type() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35127976/